if else
suggest changeAn if statement runs a block when its condition is met. Add else to run a different block when the condition is not met.
if ($a > $b) {
echo "a is greater than b";
} else {
echo "a is NOT greater than b";
}
PHP Manual - Control Structures - Else
The ternary operator as shorthand syntax for if-else
The ternary operator picks one of two values. It is a short form of a simple if/else. The manual lists it with the comparison operators.
This is the example from above using a ternary expression and variable values: $a=1; $b=2;
echo ($a > $b) ? "a is greater than b" : "a is NOT greater than b";
Outputs: a is NOT greater than b.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents