How we use if...else and else if statement in php?

(PHP 4, PHP 5, PHP 7, PHP 8)

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) {
    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: 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.

/* 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;
?>

Vladimir Kornea

15 years ago

The parser doesn't handle mixing alternative if syntaxes as reasonably as possible.

The following is illegal (as it should be):

if($a):
    echo $a;
else {
    echo $c;
}
?>

This is also illegal (as it should be):

if($a) {
    echo $a;
}
else:
    echo $c;
endif;
?>

But since the two alternative if syntaxes are not interchangeable, it's reasonable to expect that the parser wouldn't try matching else statements using one style to if statement using the alternative style. In other words, one would expect that this would work:

if($a):
    echo $a;
    if($b) {
      echo $b;
    }
else:
    echo $c;
endif;
?>

Instead of concluding that the else statement was intended to match the if($b) statement (and erroring out), the parser could match the else statement to the if($a) statement, which shares its syntax.

While it's understandable that the PHP developers don't consider this a bug, or don't consider it a bug worth their time, jsimlo was right to point out that mixing alternative if syntaxes might lead to unexpected results.

mparsa1372 at gmail dot com

1 year ago

The if...elseif...else statement executes different codes for more than two conditions.

Syntax:

if (condition) {
  code to be executed if this condition is true;
} elseif (condition) {
  code to be executed if first condition is false and this condition is true;
} else {
  code to be executed if all conditions are false;
}

Example:
Output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!":

$t = date("H");

if (

$t < "10") {
  echo
"Have a good morning!";
} elseif (
$t < "20") {
  echo
"Have a good day!";
} else {
  echo
"Have a good night!";
}
?>

qualitycoder

7 years ago

The reason 'else if' (with a space) works with traditional syntax and not colon syntax is because of a technicality.

  if($var == 'Whatever') {

  } else if(

$var == 'Something Else') {

  }

?>

In this instance, the 'else if' is a shorthand/inline else statement (no curly braces) with the if statement as a body. It is the same things as:

  if($var == 'Whatever') {

  } else {
      if(

$var == 'Something Else') {

      }
  }

?>

If you were to write this with colon syntax, it would be:

  if($var == 'Whatever'):

  else:
      if(

$var == 'Something Else'):

      endif;
  endif;

?>

What is the difference between if and if

In PHP we have the following conditional statements: if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if...elseif...else statement - executes different codes for more than two conditions.

What is ELSEIF/ELSE if in PHP?

elseif/else if. (PHP 4, PHP 5, PHP 7) elseif, as its name suggests, is a combination of ifand else. Like else, it extends an ifstatement to execute a different statement in case the original ifexpression evaluates to FALSE.

What is the use of if statement in PHP?

The if statement is used to execute a block of code only if the specified condition evaluates to true. This is the simplest PHP's conditional statements and can be written like: The following example will output "Have a nice weekend!" if the current day is Friday:

What is the difference between IF statement and else statement?

If statement is a condition statement, which is used for checking only one condition at a time, It can be true or false at a time. Else is a condition statement, It is used with If statement for checking only two conditions, It happens when if the condition goes to a false condition.

What are the 4 PHP condition statements?

PHP Conditional Statements.
The if statement..
The if...else statement..
The if... elseif....else statement..
The switch... case statement..

What is the difference between if else and ELSE IF?

Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

How do I do an if statement in PHP?

Syntax.
if (condition1){.
//code to be executed if condition1 is true..
} elseif (condition2){.
//code to be executed if condition2 is true..
} elseif (condition3){.
//code to be executed if condition3 is true..
} else{.

Does else if work in PHP?

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.