Missing semi-colon after break
Its rather interesting to know why your program behaves the way it does.
The general syntax of break
in PHP is:
break Expression;
The expression is optional, but if present its value tells how many nested
enclosing structures are to be broken out of.
break 0;
and break 1;
are same as break;
Your code is equivalent to
if($a==3)
break print"$a ";
now the print
function in PHP always return 1
. Hence it is equivalent to
if($a==3)
break 1;
so when $a
is 3
you print its value and break.
Its advisable to use braces to enclose the body of a conditional or a loop even if the body has single statement. In this case enclosing the if
body in braces:
if($a==3) {
break
}
print"$a ";
would have given a syntax error: PHP expects a ;
but finds a }
All of the above applies to the PHP continue
as well. So the program
for($a=0;$a<10;++$a)
{
if($a==3)
continue
print"$a ";
}
also prints 3
for a similar reason.