LoginSignup
1
0

More than 3 years have passed since last update.

PHPのWhile文にif文を書く!

Posted at

PHPの学習中のアウトプットとして書きます!

例えば変数$xを定義し、変数$xを用いて2〜100までの偶数をechoする場合!

index.php

 $i = 2;              //変数定義
 while ($i <= 100){    //$iが100になるまでループ
   if ($i % 2 == 0){    //if分で$iを2で割った時余り0になる数字をechoする
    echo $i;
    echo '<br>';
    }
   $i++;               //$iが100になるまで1を足す
 }

結果

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

このようになります!!

以上です!!!

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