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 an ‘”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.

//Assign a string value to $x
$x = ($myvalue == 10) ? "the value is 10": "the value is not 10";

Let’s say you decide to print the value of $x after the above statement has run.
If the value of the variable $myvalue is equal to 10, the string “the value is 10” will be printed out. But if the value of $myvalue is anything other than 10, the string “the value is not 10” will be printed out.