PHP FizzBuzz Logic

廉价感情. 提交于 2019-12-01 13:41:05

The program fizzbuzz prints 'fizz' if a number is divisible by 3, 'buzz' if a number is divisible by 5, and 'fizzbuzz' if a number is divisible by both.

Your program is not checking if the numbers are equal to 0, instead it is using the modulo operator to check if the remainders are 0.

$i%3==0 means number is divisible by 3

$i%5==0 means number is divisible by 5

$i%5==0 && $i%3==0 means the number is divisible by both

<?php
array_map(function($l) {
  echo $l . PHP_EOL;
}, array_map(function($i) {
  $is_fizz = ($i % 3) === 0;
  $is_buzz = ($i % 5) === 0;
  return (!$is_fizz && !$is_buzz) ? $i : 
    ($is_fizz ? 'Fizz' : '') . ($is_buzz ? 'Buzz' : '');
}, range(1 , 100)));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!