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 ↓
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…
Only this:
if($varisset){
switch($foo){…}
}else{
switch($foo){…}
}
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