<?php class Fizzbuzz { const FIZZ = 3; const BUZZ = 5; function exec($limit_number) { for ($i = 1; $i < $limit_number; $i++) { echo $this->judge($i); } } function judge($num) { if ($num % self::FIZZ === 0 && $num % self::BUZZ === 0) { return "fizzbuzz<br>"; } if ($num % self::FIZZ === 0) { return "fizz<br>"; } if ($num % self::BUZZ === 0) { return "buzz<br>"; } return "{$num}<br>"; } } $fb = new Fizzbuzz(); $fb->exec(100);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>FizzBuzz</title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <meta name="generator" content="Geany 0.18" /> </head> <body> <?php require getcwd() . '/Fizzbuzz.class.php'; ?> <?php $fiz = new Fizzbuzz(); ?> <?php $nums = $fiz->getFizzBuzz(); ?> <p> <?php foreach ($nums as $num) { ?> <?php echo $num; ?> <br /> <?php } ?>