1
0

More than 1 year has passed since last update.

【PHP初級㉜】for文応用4

Last updated at Posted at 2022-07-26

[問題]  (参照:http://www.cc.kyoto-su.ac.jp/~mmina/bp1/hundredKnocksPrimary.html)
1から20まで順に表示するが、5の倍数の場合は数字の代わりにbarと表示するプログラムを作成せよ。

コード

for($a = 1; $a <= 20; $a++){
    if($a%5 == 0){
        echo 'bar',PHP_EOL;
    }else{
        echo $a,PHP_EOL;
    }
}

結果

1
2
3
4
bar
6
7
8
9
bar
11
12
13
14
bar
16
17
18
19
bar
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