if else

suggest change

An 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents