Re: problems with switch()

From: Date: Sat, 08 Jul 2000 03:40:47 +0000
Subject: Re: problems with switch()
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-5505@lists.php.net to get a copy of this message
?> emits an implicit semicolon. That means that you may omit the semicolon from the last statement in every PHP block (it may be the one and only statement in case of a one-statement block, of course). In turn, this means that PHP sees your code as something like this: $x = 1; if ($x==1) { ; ... } else { ; ... } ; Which is perfectly valid (a semicolon is a valid empty statement). However, your switch code is being seen by PHP as something like this: $x = 1; switch ($x) { ; <-- quite invalid, expecting a case/default block, not a statement case 1: ... break; default: ... } ; So, this should explain why it doesn't work. Zeev > <? $x = 1 ?> > <? switch ($x) { ?> > <? case 1: ?> > x = 1 > <? break ?> > <? default: ?> > x != 1 > <? } ?> On Fri, 7 Jul 2000, J.R. Lillard wrote: > i'm running php 4.0. most of my code looks something like this: > > <html> > <body> > <? $x = 1 ?> > <? if ($x == 1) { ?> > x = 1 > <? } else { ?> > x != 1 > <? } ?> > </body> > </html> > > i know that may not be the easiest thing for the parser to parse, but it's > easier for me to read it. basically i'm going in and out of php code > through the block of the if() statement. but if i do something like this: > > <html> > <body> > <? $x = 1 ?> > <? switch ($x) { ?> > <? case 1: ?> > x = 1 > <? break ?> > <? default: ?> > x != 1 > <? } ?> > </body> > </html> > > i get this error: > > Parse error: parse error, expecting T_CASE' or > T_DEFAULT' or `'}'' > > referencing the line right after the switch() statement. but if i do > this: > > <html> > <body> > <? $x = 1 ?> > <? > switch ($x) { > case 1: > echo "x = 1"; > break; > default: > echo "x != 1"; > } > ?> > </body> > </html> > > it works just fine. is there any reason i can't jump in and out of php > code in the block of a switch() statement? it works just fine with if()s, > while()s, and for()s. > > -jr > > > -- Zeev Suraski <zeev@zend.com> http://www.zend.com/

« previous php.general (#5505) next »