PHP If else


If flow control allows execution of code fragments according to conditions. It divided into three categories:

  1. if
  2. if ... else
  3. else if

if

grammar:

if (expr) {
    Statement
}
example:

if ($x > $y)
    echo " x is greater than y ";

if ... else

grammar:

if (expr) {
    Statement1
} else {
    Statement2
}

It said that as long as expr set up, the implementation statement1, otherwise the implementation statement2.

example:

if ($x > $y)
    echo " x is greater than y ";
} else {
    echo " x is less than or equal to y ";
}

else if

grammar:

if (expr) {
    Statement1
} else if (expr2) {
    Statement2
} else {
    Statement3
}

It said that as long as expr1 set up, the implementation statement1, or test expr2, if expr2 set up the implementation of statement2. If expr2 does not hold, then statement3 is executed.

example:

if ($x > $y) {
    echo " x is greater than y ";
} else if ($x == $x) {
    echo " x equals y ";
} else {
    echo " x is less than y ";
}

Tip

else if can have multiple, in order to achieve more conditions of choice, but in most cases, more conditions, we can choose to achieve switch.




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook