Connecting Tech Pros Worldwide Forums | Help | Site Map

Why no error?

Andrew G. Koptyaev
Guest
 
Posts: n/a
#1: Sep 23 '08
Why no error on this code?

<?php
for ($i=0;$i<5;++$i)
{
if ($i==2)
continue
print "$i\n";
}
?>

Output is "2".
May be true must be "continue;"?



703designs
Guest
 
Posts: n/a
#2: Sep 23 '08

re: Why no error?


That's weird and interesting. You're right, you need the semicolon.

On Sep 23, 1:20*pm, "Andrew G. Koptyaev" <kopty...@gmail.comwrote:
Quote:
Why no error on this code?
>
<?php
for ($i=0;$i<5;++$i)
{
*if ($i==2)
* continue
*print "$i\n";}
>
?>
>
Output is "2".
May be true must be "continue;"?
Michael Fesser
Guest
 
Posts: n/a
#3: Sep 23 '08

re: Why no error?


..oO(703designs)
Quote:
>That's weird and interesting.
It's documented behaviour. The example was taken from the manual and
describes a situation which you should avoid.

Maybe it becomes clearer with a little rearrangement, it's still the
same code:

<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2) {
continue print "$i\n";
}
}
?>

Since print is a special language construct and always returns 1, the
above is equivalent to

<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2) {
print "$i\n";
continue 1;
}
}
?>

http://www.php.net/manual/en/control...s.continue.php
Quote:
>You're right, you need the semicolon.
Yep.

Micha
Closed Thread