I'm trying to make the fizzbuzz.php assignment from PHP and MySQL Web Development 5th Edition, page 193. I have typed it exactly as it is in the book, but I get a (T_CONSTANT_ENCAPSED_STRING) parse error (line 9) when I run it.
I have tried replacing yeild with echo, but then I get an improper function use error on line 27 (the foreach function).
I have tried escaping the " with \ but that gives me a syntax error, unexpected string.
I have tried using ' instead of " but get the enacapsed string error.
<?php
function fizzbuzz($start, $end)
{
$current = $start;
while ($current <= $end)
{
if ($current%3 == 0 && $current%5 == 0)
{
yield "fizzbuzz";
}
elseif ($current%3 == 0)
{
yield "fizz";
}
elseif ($current%5 == 0)
{
yield "buzz";
}
else
{
yield $current;
}
$current++;
}
}
foreach(fizzbuzz(1, 20) as $number)
{
echo $number.'<br />';
}
?>
Changing yeild to echo returns a string of numbers and fizz buzz strings but they are not in the order they are supposed to be and there is still a function error at line 27.
I may have something mistyped, but I've checked it over and over, and this is how it's written in the book.