初めてPHPを書いてみたんですが、こんな感じで問題ないでしょうか。
動作は確認済みです。
PHPは情報もバージョンもたくさんありすぎて、どれを参考にして良いのか選ぶのが難しいです。
バージョンによって、非推奨の関数や書式などがあるんだと思いますが、それも良く分かりません。
あと、if
とforeach
が入れ子になっているところとか、インデントとか、これで良いでしょうか。
...
使っているPHPのバージョンは5.4.x, 5.5.x, 5.6.xです。
よろしくお願いします。
- 書いてみたコード
lang
1<?php 2 3// $json = file_get_contents("..."); 4$json = '{"status":200,"user":"argius","data":[{"aaa":1},{"bbb":2}]}'; 5$obj = json_decode($json); 6 7$status = $obj->{"status"}; 8$user = $obj->{"user"}; 9$data = $obj->{"data"}; 10 11 ?> 12<!DOCTYPE html> 13<html lang="ja"> 14<head> 15<meta charset="utf-8"/> 16<title>test</title> 17</head> 18<body> 19<?php if ($status === 200) : ?> 20<p>ユーザー:<?php print $user; ?></p> 21<p>データ</p> 22<table> 23<thead> 24<th width="85">key</th> 25<th width="75">value</th> 26</thead> 27<tbody> 28<?php 29foreach ($data as $row) { 30 foreach ($row as $key => $value) { ?> 31<tr> 32<td><?php print $key; ?></td> 33<td><?php print $value; ?></td> 34</tr> 35<?php 36 } 37} 38 ?> 39</tbody> 40</table> 41<?php else : ?> 42<p>予期しないエラーです。(ステータス: <?php print $status; ?>)</p> 43<?php endif; ?> 44</body> 45</html>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/02/20 09:08
2016/02/20 09:18
2016/02/20 12:55