★ wanayoo — archive 1999 http://www.php.net/manual/control-structures.elseif.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Control Structures
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Control Structures
* if
* else
* elseif
* Alternative syntax for control structures
* while
* do..while
* for
* foreach
* break
* continue
* switch
* require
* include
* require_once
* include_once
Manual: elseif
View the source code for this pageSearch the site



Previous page
 else
 Updated
Mon, 07 Aug 2000
Alternative syntax for control structures 
Next page


elseif

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:


if ($a > $b) {
    print "a is bigger than b";
} elseif ($a == $b) {
    print "a is equal to b";
} else {
    print "a is smaller than b";
}
     

There may be several elseifs within the same if statement. The first elseif expression (if any) that evaluates to true would be executed. In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.

The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE.


User Contributed Notes: elseif


jvanlomm@calpoly.edu
13-Feb-2000 07:56
The 'else' statement will be associated with the closest 'if'. For example,
<PRE>
if (cond1)
if (cond2)
stmt2;
else if (cond3)
stmt3;
</PRE>
executes 'stmt3' only if both 'cond1' and 'cond2' are not met. In other words, it should really be
<PRE>
if (cond1)
if (cond2)
stmt2;
else if (cond3)
stmt3;
</PRE>



RBiffl@BlackletterSoftware.com
12-Apr-2000 01:57
Correction to above: stmt3 will execute if cond1 and cond3 are true and cond2 is false. Second indenting is correct, but it's a good example of why to use curly braces.


 About Notes


Previous page
 else
 Updated
Mon, 07 Aug 2000
Alternative syntax for control structures 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.