Search code examples
phploopscontinuephp-5.4

PHP Continue - removed support for variable numeric argument (e.g. continue $num)


http://php.net/manual/en/control-structures.continue.php

Changelog says that as of 5.4, following change happened: Removed the ability to pass in variables (e.g., $num = 2; continue $num;) as the numerical argument.

Why on earth would they do that?

So, basically, this is now invalid:

for ($i = 0; $i < 10; $i++) {
    $num = 5;
    continue $num;
}

Am I understanding this correctly? Why would they do that? I just cannot think of a reason.


Solution

  • $i = 0;
    while ($i++ < 5) {
        echo "Outer<br />\n";
        while (1) {
            echo "Middle<br />\n";
            while (1) {
                echo "Inner<br />\n";
                continue 3;
            }
            echo "This never gets output.<br />\n";
        }
        echo "Neither does this.<br />\n";
    }
    

    here in the above example from PHP Manual the continue skips the echo "This never gets output.<br />\n"; and echo "Neither does this.<br />\n"; statement and the continue 3; denotes the loop number to continue

    for ($i = 0; $i < 10; $i++) {
        $num = 5;
        continue ;
        echo $num;
    }
    

    the above will skip the printing of $num