Short If Statement in PHP

PHP – One Line If Statement – Short If Statement – Condensed If Statement

I always have to look this up, so here it is for easy reference. Sometimes is makes the code look less cluttered if you can express and if in one line.

The one line if statement is also known as the ternary operator if you want to look it up in the PHP documentation. It has also been called the short if statement and the one line if statement.

$x = ($myvalue == 10) ? "the value is 10": "the value is not 10";

In the line of code above, if the value of the variable $x is equal to 10, the string “the value is 10″ is printed out. If the value of $x is anything other than 10, the string “the value is not 10″ is printed out.

Leave a comment