Shorthand PHP if statement

Within most PHP scripts if statements are used quite often so wouldn’t it make sense if you could make this as fast and as efficient as possible? You’ll be glad to know that there is such a thing as the PHP if shorthand syntax which lets you write simple if statements using less code.

Normal PHP if statement

This is an example of a normal PHP if statement.

[php]
if ($variable > 0) {
echo "More than 0";
} else {
echo "Not more than 0";
}
[/php]

Shorthand PHP if statement

And this is the shorter PHP if statement syntax.

[php]
($variable > 0 ? "More than 0" : "Not more than 0")
[/php]

The shorthand PHP if syntax can be done on a single line making it easier to use and much quicker to type.