As a general rule, code is like writing: the more concise it is, the better. Fewer characters tend to mean lower errors, and fewer bugs. To this end, there are a few syntax shortcuts that are very useful.

if shortcut – no braces

If your if statement has only one directive, like the following:

if ($x) { echo “This directive”; }

You can drop the braces:

if ($x) echo “This directive”;

if-else shortcut

You’re probably used to the long, formal version of the if-else statement in PHP:

if ($x == 5) { $y = 3 } else { $y = 2 }

While there is nothing wrong with this statement, it’s more efficient to use the shortened version, using the ternary operator:

($x == 5) ? $y = 3 : $y = 2;

echo shortcut

echo has a shortcut too, so long as the shortcut for the PHP tag itself is supported on the server. This is particularly useful if you are echoing out small snippets as variables. So rather than:

You have chosen <?php echo $foo; ?> widgets.

You can use:

You have chosen <?=$foo?> widgets.

Enjoy this piece? I invite you to follow me at to learn more.