[PHP]FizzBuzzゲームをifとforを使わないで作った結果ｗｗｗ

#1.[range](https://www.php.net/manual/ja/function.range.php)で配列生成して[array_filter](https://www.php.net/manual/ja/function.array-filter.php)で抽出してから[array_fill_keys](https://www.php.net/manual/ja/function.array-fill-keys.php)で文字を埋めて[array_replace](https://www.php.net/manual/ja/function.array-replace.php)で合体して[array_map](https://www.php.net/manual/ja/function.array-map.php)で要素を出力

```fizz_buzz.php
<?php
$array = range(0, 100);

$fizz_array = array_fill_keys(array_filter($array, function($value) {
	return $value % 3 === 0;
}), 'Fizz');

$buzz_array = array_fill_keys(array_filter($array, function($value) {
	return $value % 5 === 0;
}), 'Buzz');

$fizz_buzz_array = array_fill_keys(array_filter($array, function($value) {
	return $value % 3 === 0 && $value % 5 === 0;
}), 'FizzBuzz');


$result = array_replace($array, $fizz_array, $buzz_array, $fizz_buzz_array);
unset($result[0]);

array_map($result, function($value){
	echo $value . '<br>';
});
```
ちなみにarray_filterの代わりに

```php
range(0, 100, 3); // rangenの第3引数を指定すると要素の増加数をしていできる。
```
でも可。

#2.クラスを使う#
```fizzbuzz.php
<?php
class FizzBuzz {
    const FIZZ = 3;
    const BUZZ = 5;
    public static $counter = 0;
    private $number;
    public function __construct () {
        self::$counter++;
        $this->number = self::$counter;
    }

    public function build() {
    	return self::getFizzBuzz() ? 'FizzBuzz' :
    		self::getFizz() ?  'Fizz':
    			self::getBuzz() ? 'Buzz' : $this->number;
    }

    private function getFizzBuzz() {
        return $this->number % self::FIZZ === 0 && $this->number % self::BUZZ === 0;
    }

    private function getFizz() {
        return $this->number % self::FIZZ === 0;
    }

    private function getBuzz() {
        return $this->number % self::BUZZ === 0;
    }
}

$array = array_fill(1, 100, 'Inori Minase');

array_walk($array, function(&$value){
    $fizz_buzz_incetance = new FizzBuzz();
    $value = $fizz_buzz_incetance->build();
});

$obj = new ArrayObject($array);
$it = $obj->getIterator();

while ($it->valid()){
    echo $it->current() . "</br>";
    $it->next();
}
```

FizzBuzzCounterクラスは、インスタンスが生成されるたびにカウンターを1づつ増やしていくよ。

#まとめ#
困ったら[公式ドキュメント](https://www.php.net/manual/ja/intro-whatis.php)を読もう！

#おまけ#
##chain of responsibility##

```php
<?php
class FizzBuzzClient {
	public static $counter = 0;
    private $number;
	public function __construct () {
		self::$counter++;
        $this->number = self::$counter;
	}

	public function getNumber() {
		return $this->number;
	}
}

abstract class Handler {
	private $next;

	public function setNext(Handler $next) {
		$this->next = $next;
	}

	public function request (FizzBuzzClient $client) {
		$this->resolve($client) ? $this->done() : $this->request($client);
	}

	abstract protected function resolve(FizzBuzzClient $client);

	abstract protected function done (FizzBuzzClient $client);

}

class NoHandler extends Handler {
	protected function resolve (FizzBuzzClient $client) {
		return false;
	}

	protected function done (FizzBuzzClient $client) {
		return false;
	}
}

class FizzBuzzHandler extends Handler {
	protected function resolve (FizzBuzzClient $client) {
		return $client->getNumber % 3 === 0 && $client->getNumber % 5;
	}

	protected function done (FizzBuzzClient $client) {
		return 'FizzBuzz';
	}
}

class FizzHandler extends Handler {
	protected function resolve (FizzBuzzClient $client) {
		return $client->getNumber % 3 === 0;
	}

	protected function done (FizzBuzzClient $client) {
		return 'Fizz';
	}
}

class BuzzHandler extends Handler {
	protected function resolve (FizzBuzzClient $client) {
		return $client->getNumber % 3 === 0 && $client->getNumber % 5;
	}

	protected function done (FizzBuzzClient $client) {
		return 'Buzz';
	}
}

class LastHandler extends Handler {
	protected function resolve (FizzBuzzClient $client) {
		return true;
	}

	protected function done (FizzBuzzClient $client) {
		return $client->getNumber;
	}
}

$array = array_fill(1, 100, 'Inori Minase');

$no_handler = new NoHandler();
$fizz_buzz = new FizzBuzzHandler();
$fizz = new FizzHandler();
$buzz = new BuzzHandler();
$last = new LastHandler();

$no_handler->setNext($fizz_buzz)->setNext($fizz)->setNext($buzz)->setNext($last);

foreach ($array as $key => $value) {
$fizz_buzz_client = new FizzBuzzClient();
	$no_Handler->request($fizz_buzz_client);
}
```
ここまで書いたけどメモリ足りなくて断念した。

#おまけ2##
「何って...俺はただif文を使っただけだが...?」ｷｮﾄﾝ

```php
<?php
for ($i = 1; $i <= 100; $i++) {
	if ($i % 3 == 0 && $i % 5 == 0) {
		echo 'FizzBuzz</br>';
	} elseif ($i % 3 == 0) {
		echo 'Fizz</br>';
	} elseif ($i % 5 == 0) {
		echo 'Buzz</br>';
	} else {
		echo $i .'</br>';
	}
}
```