1

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.

BackupXfer
  • 21
  • 4

2 Answers2

2

This is too long for a comment.

I suspect that your version of PHP you are running this on, isn't able to support it, per testing your code online on http://sandbox.onlinephpfunctions.com/.

It threw back the same error when using PHP 5.0.4.

You will need to upgrade your server if this is on a local machine. If it is hosted, you will need to contact the hosting provider to see if a newer version of PHP is available to you.

Edit:

Per the manual on PHP.net under "Note":

In PHP 5, a generator could not return a value: doing so would result in a compile error. An empty return statement was valid syntax within a generator and it would terminate the generator.

Edit #2:

(From comments)

Thank you for your help, and the links to the php sandbox, that will help me in the future. I ran phpversion() and it returned 5.4.45. It's a school server, so I'll ask them if they can upgrade it, or install PEAR on my laptop. – BackupXfer

Using version 5.4.45 also returned the same error, per the new test link. This feature is only available in PHP 5.5.0 and higher.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thank you for your help, and the links to the php sandbox, that will help me in the future. I ran phpversion() and it returned 5.4.45. It's a school server, so I'll ask them if they can upgrade it, or install PEAR on my laptop. – BackupXfer Oct 27 '19 at 15:01
  • @BackupXfer (from your comment above) Using version 5.4.45 also returned the same error, [per the new test link](http://sandbox.onlinephpfunctions.com/code/c990ec4c01257d2e863787cc59d7bacf55765cb0). This feature is only available in PHP 5.5 and up. – Funk Forty Niner Oct 27 '19 at 15:07
  • 1
    I installed WAMPServer 3 as my localhost server with php version 7+ and fizzbuzz.php worked perfectly like as on the onlinephpfunctions website. I'm a student so all of this is both new and confusing, but I wanted to thank you for your help and the php sandbox link. I'll use it in the future when I run into problems to rule out version limitations. I've learned a lot just through this one question, I really appreciate your help, thanks. – BackupXfer Oct 27 '19 at 23:53
  • Hey that's good news and you are very much welcome. I'm glad that you got it working and the setup that you made to accomplish this, right on :) *Cheers* – Funk Forty Niner Oct 28 '19 at 01:16
  • 2
    A side note, there is another good website for testing PHP code across multiple versions: https://3v4l.org/ – Dharman Nov 01 '19 at 17:46
1

for php5

function fizzbuzz($start, $end)
{
    for ($i=$start; $i<=$end; $i++)
    {
        $str = '';
        if ($i % 3 == 0)
            $str = 'Fizz';
        if ($i % 5 == 0)
            $str .= 'Buzz';

        if (empty($str))
            $str = $i;

        echo $str.'<br>';

    }
}

fizzbuzz(1, 20);
Dharman
  • 30,962
  • 25
  • 85
  • 135
prophp
  • 91
  • 2
  • 4
  • If you are still using PHP 5 I strongly recommend to upgrade as soon as possible. This version is no longer supported. [Let Rasmus Lerdorf explain it to you](https://youtu.be/wCZ5TJCBWMg?t=2434) – Dharman Mar 21 '20 at 02:06