Re: Question about using if elseif ...
| From: | Analysis & Solutions | Date: | Thu, 18 Jul 2002 21:37:14 +0000 |
| Subject: | Re: Question about using if elseif ... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-108341@lists.php.net to get a copy of this message | ||
On Thu, Jul 18, 2002 at 02:28:23PM -0700, Fargo Lee wrote:
> When viewing examples of using if and elseif I often see the example ending
> with an else like ...
>
> if($a == '1'){
> echo '1';
> } elseif ($a == '2'){
> echo '2';
> } else {
> echo '0';
> }
On a side note, switch would be more efficient:
switch ($a) {
case 1:
echo '1';
break;
case 2:
echo '2';
break;
default:
echo 'other';
}
Also use nesting / indenting to make your code more readable:
if ($a == '1') {
echo '1';
}
> Is there any problem with leaving out the last else and just ending it with
> an elseif such as ...
>
> if($a == '1'){
> echo '1';
> } elseif ($a == '2'){
> echo '2';
> }
That all depends on the circumstances. Both have their places.
--Dan
--
PHP classes that make web design easier
SQL Solution | Layout Solution | Form Solution
sqlsolution.info | layoutsolution.info | formsolution.info
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409