Re: ifelse/else

From: Date: Mon, 19 May 2003 10:25:50 +0000
Subject: Re: ifelse/else
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-148146@lists.php.net to get a copy of this message
On Mon, 19 May 2003 19:59:42 +1000, you wrote: >I need some advice where I have to test the existence of a value up to five >times. there is a limit to the number of times you can use 'else' or >'ifelse'. I need the code to check a match to a certain value, if not jump >to next if statement. if it matches, it needs to exit the next if statements >totally. elseif would work. Alternatively, you can use a switch() statement: switch ($a){ case 1: echo '<p>$a == 1</p>'; break; case 2: echo '<p>$a == 2</p>'; break; case 3: echo '<p>$a == 3</p>'; break; default : echo '<p>$a == ' . $a . '</p>'; break; } The default: block activates when $a matches none of the case: blocks. Note the break at the end of each case block. If you don't use it, you'll get fallthrough, which can be useful sometimes (remove the breaks to see what I mean). if..elseif..else and switch..case..break..default are functionally equivalent. If you don't need fallthrough, which you use comes down to preference and readability.

« previous php.general (#148146) next »