2
2

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.

「for文」で繰り返しの命令をする

Posted at

for文の構造

for (ループカウントを制御) {
	繰り返す処理
}

for文のループカウンタ

for (初期値; 終了条件; 増分値)

1から5まで表示するfor文


<?php

for ($i=1; $i <= 5; $i++) { 
	echo $i;
}

?>  /// 12345


連続した5日分の日付を表示する


<?php

for ($i=1; $i <= 5; $i++) { 
	echo "3月{$i}日<br />";
}

?>

実行結果
php_007.png

break文でループを抜ける

下の例では、$iが2より大きくなった時点でbreak文が実行されるので「123」と表示されます。


<?php

for ($i=1; $i <= 5; $i++) { 
	echo $i;
	if ($i > 2){
		break;
	}
}

?> /// 123

今日から5日分の日付を表示する

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?