PHP else if statement


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:


	      	<?php
			if ($a > $b) {
			    echo "a is bigger than b";
			} elseif ($a == $b) {
			    echo "a is equal to b";
			} else {
			    echo "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.


Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.


example


		  	<?php
       
		       $Age = 23; 
		       if ($Age < 10) {
		               print "You're under 10";
		       } elseif ($Age < 20) {
		               print "You're under 20";
		       } elseif ($Age < 30) {
		               print "You're under 30";
		       } elseif ($Age < 40) {
		               print "You're under 40";
		       } else {
		               print "You're over 40";
		       }
		    ?>
		  

example


		  	<?php
       
		        $name = "Jack";
				  if($name == "")
				  {
				    print("You have no name.");
				  }
				  elseif(($name == "leon") OR ($name == "Leon"))
				  {
				    print("Hello, Leon!");
				  }
				  else
				  {
				    print("Your name is '$name'.");
				  }
		    ?>
		  

example


		  	<?php
       
		        /* Incorrect Method: */
				if ($a > $b):
				    echo $a." is greater than ".$b;
				else if ($a == $b): // Will not compile.
				    echo "The above line causes a parse error.";
				endif;


				/* Correct Method: */
				if ($a > $b):
				    echo $a." is greater than ".$b;
				elseif ($a == $b): // Note the combination of the words.
				    echo $a." equals ".$b;
				else:
				    echo $a." is neither greater than or equal to ".$b;
				endif;

		    ?>
		  





PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion