When writing PHP, I don’t know why but I keep having to research the PHP one line if statement again and again. I just can’t seem to remember it. Totally bizarre.
There are many names for this construct. The one liner if 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 single line if statement. If you are like me, and continually having to look this up – here you go:
One Line If Statement Example
[php]
$myvalue = 99;
$x = ($myvalue == 99) ? "x is 99": "x is not 99";
echo("<br>".$x);
[/php]
So if you were to run the above code, you’d find that because the value of $x had been initially set to 99, that the code would print out :
x is 99
Anything else would cause the program to print out :
x is not 99