FizzBuzz - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of FizzBuzz.php

  1. <?php
  2.  
  3. /**
  4.  * @param int $i
  5.  * @return string
  6.  */
  7. function fb(int $i): string
  8. {
  9.     if (!$i) return $i;
  10.  
  11.     $fizz = $i % 3 === 0;
  12.     $buzz = $i % 5 === 0;
  13.  
  14.     if ($fizz && $buzz) {
  15.         return 'FizzBuzz';
  16.     }
  17.     if ($fizz) {
  18.         return 'Fizz';
  19.     }
  20.  
  21.     if ($buzz) {
  22.         return 'Buzz';
  23.     }
  24.     return $i;
  25. }
  26.  
  27. function fb2(int $i): string
  28. {
  29.     if (!$i) return $i;
  30.  
  31.     $res = '';
  32.  
  33.     if (!($i % 3)) $res = 'Fizz';
  34.     if (!($i % 5)) $res .= 'Buzz';
  35.     if (!$res) $res = $i;
  36.  
  37.     return $res;
  38. }
  39.  
  40. function run()
  41. {
  42.     for ($i = 0; $i <= 100; $i++) {
  43.         print $i . ': ' . fb2($i) . PHP_EOL;
  44.     }
  45. }
  46.  
  47. run();
  48.  
File Description
  • FizzBuzz
  • PHP Code
  • 24 Feb-2021
  • 677 Bytes
You can Share it: