0
0

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 1 year has passed since last update.

[PHP]繰り返し処理

Posted at

繰り返し処理

繰り返しの処理は、for文,while文で表現することが可能である。

for文

for文
for ($i=1; $i<=100; $i++){
echo $i;
}
//結果:1~100までの自然数が順番に表示される

上記for文において、

$i = 1 ・・・①(初期化)
$i<=100・・・②(ループの条件)
echo $i・・・③(繰り返す処理)
$i++   ・・・④(変数の更新)

~までを、②の条件を満たす限り続けていく

while文

while文
$i = 1
while ($i <= 100){
    echo $i;
    $i++;
}

for文と同じく、

$i = 1 ・・・①(初期化)
$i<=100・・・②(ループの条件)
echo $i・・・③(繰り返す処理)
$i++   ・・・④(変数の更新)

~までを、②の条件を満たす限り続けていく

なお、④を記載しないと、ループが無限に続いてしまうので注意が必要。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?