0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP 初心者 foreach文

Posted at

foreach..配列の内容をループ形式で取り出せる

コロン構文...配列分の波括弧をコロンで終わらせる。閉じる場合は"endforeach;"
メリット...HTML分を書く際スッキリして書くことが出来る。

HTML文を書く前提としてコロン構文は外せない。課題だったHTMLも含めたコードを書くことが出来たので既存のコードをもう少し要素を付け加えていきたい。

後一番先頭の配列は以前に連想配列と学んだけど、ドットインストールではその辺は触れなかったから気になる。調べておこう

<?php

//配列
$game = [

  "kamimaho" =>100,
  "suisouginka"=>90,
  "pararero"=>80,
];

//foreach...上記の配列を引っ張ってきてくれる

foreach ($game as $key =>$value){
  echo $key."は".$value."点です" ;
}

コロン構文
foreach ($game as $key =>$value):
  echo $key."は".$value."点です" ;
endforeach;
?>

//HTMLのリストで表示させる記述
<ul>
  <?php foreach ($game as $key =>$value): ?>
  <li><?php echo $key."は".$value."点です" ; ?></li>
<?php endforeach; ?>
</ul>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?