|
else
Often you'd want to execute a statement if a certain condition is
met, and a different statement if the condition is not met. This
is what <?php
The Related Examples ( Source code ) » control_structures.else Examples ( Source code ) » Animated Talking Captcha php class Examples ( Source code ) » Create a swf slideshow Examples ( Source code ) » Search the Web using Google API Examples ( Source code ) » Feed Validator Examples ( Source code ) » Get WhoIs Information for 270 different tld's Examples ( Source code ) » E-mail address validation class Examples ( Source code ) » HttpRequestPool Examples Examples ( Source code ) » FeedAggregator using HttpRequest Examples ( Source code ) » XmlRpcServer extends HttpResponse Examples ( Source code ) » Query the Google API using SOAP and PHP. Examples ( Source code ) » Handling OCI8 Errors Examples ( Source code ) » PL/SQL Errors in PHP Examples ( Source code ) » Getting Output with DBMS_OUTPUT Examples ( Source code ) » SAX Parser Examples ( Source code ) » Create report in PDF format from Crystal Report via PHP and COM Code Examples / Notes » control_structures.elserobbak
Yes, that code is clearly ambiguous. I would think that the code does the 'right thing' with it anyway. The else should bind to the nearest if. If you are going to nest ifs, use curly brackets and stay sane. <? if ($a == 1) { /* nested if */ if ($b == 2) echo '2'; } else /* other code */ ?> jsimlo
This generates a parser error: <? if ($a == 1): /* nested if */ if ($b == 2) echo '2'; else: /* other code */ endif; ?> The nested "if" binds the outer "else" and the colon then generates a parser error. As this "bug" is not going to be fixed (see http://bugs.php.net/bug.php?id=838), this could be an artful solution to this problem: <? if ($a == 1): /* nested if */ if ($b == 2) echo '2'; /* dummy expression */ ; else: /* other code */ endif; ?> cap
Often you can avoid large if/else statements in your code by using the ternary operator. For example: <?php echo "You have $i ". ($i==1 ? "message" : "messages"). " in your mailbox.\n"; ?> caliban darklock
If you're coming from another language that does not have the "elseif" construct (e.g. C++), it's important to recognise that "else if" is a nested language construct and "elseif" is a linear language construct; they may be compared in performance to a recursive loop as opposed to an iterative loop. <?php $limit=1000; for($idx=0;$idx<$limit;$idx++) { $list[]="if(false) echo \"$idx;\n\"; else"; } $list[]=" echo \"$idx\n\";"; $space=implode(" ",$list);| // if ... else if ... else $nospace=implode("",$list); // if ... elseif ... else $start=array_sum(explode(" ",microtime())); eval($space); $end=array_sum(explode(" ",microtime())); echo $end-$start . " seconds\n"; $start=array_sum(explode(" ",microtime())); eval($nospace); $end=array_sum(explode(" ",microtime())); echo $end-$start . " seconds\n"; ?> This test should show that "elseif" executes in roughly two-thirds the time of "else if". (Increasing $limit will also eventually cause a parser stack overflow error, but the level where this happens is ridiculous in real world terms. Nobody normally nests if() blocks to more than a thousand levels unless they're trying to break things, which is a whole different problem.) There is still a need for "else if", as you may have additional code to be executed unconditionally at some rung of the ladder; an "else if" construction allows this unconditional code to be elegantly inserted before or after the entire rest of the process. Consider the following elseif() ladder: <?php if($a) { conditional1(); } elseif($b) { conditional2(); } elseif($c) { conditional3(); } elseif($d) { conditional4(); } elseif($e) { conditional5(); } elseif($f) { conditional6(); } elseif($g) { conditional7(); } elseif($h) { conditional8(); } else { conditional9(); } ?> To insert unconditional preprocessing code for $e onward, one need only split the "elseif": <?php if($a) { conditional1(); } elseif($b) { conditional2(); } elseif($c) { conditional3(); } elseif($d) { conditional4(); } else { ....unconditional(); ....if($e) { conditional5(); } ....elseif($f) { conditional6(); } ....elseif($g) { conditional7(); } ....elseif($h) { conditional8(); } ....else { conditional9(); } } ?> The alternative is to duplicate the unconditional code throughout the construct. gwmpro
I am new to this language. It seems to me that only the semicolon ';' is required, the brackets '{}' are not if there is only one statement. The code segment below would be legal. <?php if ($a > $b) echo "a is bigger than b"; else echo "a is NOT bigger than b"; ?> mitch
At gwmpro at yahoo dot com The curly brace is not required however, for readability and maintenance, many developers would consider it bad style not to include them. ravi dot goglobium
another good example of using ternary operator is <?php $a = 20; $b = 30; $c = 70; echo ($a > $b && $a > $c) ? $a : ($b > $a && $b > $c) ? $b : $c; ?> |