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 3 years have passed since last update.

PHPでFizzBuzzの関数の作成

Posted at

##はじめに
この記事はPHPの学習を始めた方が関数の学習で行う
FizzBuzzの関数の作成について書いていきます。

##FizzBuzzの仕様
・数値が3の倍数の戻り値は”Fizz”
・数値が5の倍数の戻り値は”Buzz”
・数値が3の倍数であり5の倍数でもある場、戻り値は”FizzBuzz”
・上記のどれも満たさない場合は、その数値自体を戻り値にする

##FizzBuzzの関数の使い方
・最初の下記の1行を記述

$max = 100;

・1から $max までのループを作り、FizzBuzzの関数を実行
・FizzBuzzの関数の戻り値を表示

##PHP

test.php

<?php

   $max = 100;
   
   for ($num = 1; $num <= $max; $num++) {
       print fizzbuzz($num);
   }   
   
   function fizzbuzz($num) {
       
       if ($num % 15 == 0) {
          print "FizzBuzz" . PHP_EOL;
        }
       elseif ($num % 5 == 0) {
          print "Buzz" . PHP_EOL;
        }
       elseif ($num % 3 == 0) {
          print "Fizz" . PHP_EOL;
       }
       else {
          print $num . PHP_EOL;
       }
   }

?>

条件分岐のif文や繰り返しのfor文などを理解すれば
簡単に使えるように思います。

0
0
1

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?