Optional ‘case’ statements - possible in PHP?

I’m working on parsing HTML at present using XML_HTMLSax; as usual I need to skip a large number of the @case@ statements if a variable is not true. Is there a legal alternative to the following?

bc. $foo = ‘baz’;
switch($foo){
case ‘bar’:
print ‘BAA’;
break;
if ($varisset) {
case ‘moo’:
print ‘ANIMAL!’;
break;
case ‘brrrrr’:
print ‘COLD?’;
break;
}
} // switch

3 comments ↓

#1 Joseph Scott's Blog on 08.05.04 at 12:27 am

Answer To Peter Bowyer’s Question

Peter Bowyer asked a question on his blog about optional case statements in PHP. After a couple of minutes I came up with two possible solutions. I tried to post the solutions as a comment to the blog entry, but it kept asking for a login in order to…

#2 Keith on 08.05.04 at 3:46 pm

Only this:

if($varisset){
switch($foo){…}
}else{
switch($foo){…}
}

#3 Jamie on 05.14.05 at 7:10 pm

Or possibly (which would be quicker in the case that $foo was ‘baz’ most of the time):

$foo = ‘baz’;

switch($foo){

case ‘bar’:
print ‘BAA’;
break;

default:

if ($varisset) {

switch($foo) {

case ‘moo’:
print ‘ANIMAL!’;
break;

case ‘brrrrr’:
print ‘COLD?’;
break;

} // switch

}

} // switch

Leave a Comment