Shorthand PHP

PHP is a very flexible langauge – there is often more than one way to do the same thing. Some argue this is a problem with the language, which makes it difficult to work with, but others argue it makes it flexible.

Ternary Operator

Hidden down the Comparison Operators page of the PHP manual is a helpful way to handle when you just need a simple yes/no variable.

For example, if a POST variable exists, I want to create a variable with its value, but if it doesn’t exist, I want the same variable to be false.

Normally you’d write it like this

if ($_POST['email'])
{
    $email = $_POST['email];
}
else
{
    $email = false;
}

Great, but that’s quite a few lines of code for something that’s reasonably easy to understand and not very complicated. Using the ternary operator, we can shrink that down.

$email = ($_POST['email']) ? $_POST['email'] : false;

Look how much nicer that is! Start by asking to declare the variable, then in brackets do what you’d normally do for the if statment. Then a question mark, followed by the what you’d like the variable to be set to if the statment is true, then a colon and what happens if it’s false.

However, if you’re using PHP 5.3 or above, you can shrink it down further.

$email = ($_POST['email']) ?: false;

If the condition is true, it’ll set the variable to whatever the test is. There’s a gotcha to look out for, if you use a function such as isset() it’ll return 1 rather than the value of the variable.

You don’t have to define a variable though, you can just print it.

echo ($_POST['email']) ?: false;

Which brings me onto the use of PHP short tags.

Short Tags

There never seems to be a difinitive answer on whether these are accepted, as you can turn the feature off. I’d only recommend using them if you have control over the servers you’re pushing to.

A short tag is a way of reducing the PHP tag and echoing out the contents. So using the example above, you could take out much of the first bit to have the same result.

<?=($_POST['email']) ?: false?>

if brackets

Sometimes you can’t use the ternary operator I mentioned first, because what you need to do is too complex or you feel that looking at it makes it look more complicated than it is. But you can reduce the number of lines you need to make it work.

if ($jam == 'strawberry')
{
    $special_offer = 'Buy one get one free!';
}
else
{
    $ideas = 'Why not condsider strawberry jam?';
}

This wouldn’t work using the ternary operator, because it defines different variables. You might have something more complicated, but if it’s just a few lines then it can be nicer to remove the brackets.

if ($jam == 'strawberry')
    $special_offer = 'Buy one get one free!';
else
    $ideas = 'Why not condsider strawberry jam?';

That way, it’s still easy to read but more concise. Optionally you can add a fifth line endif; if you find it makes it easier to read.

Functions as arrays

This is only available in PHP 5.4 and newer, so you might have to wait to use it. Normally if you wanted to run an array produced by a function through a foreach, you’d have to define it first.

$cats = get_all_the_cats();
foreach ($cats as $cat)
{
    echo 'Miaow to '.$cat;
}

But now you can skip the first line and use the function in the foreach.

foreach (get_all_the_cats() as $cat)
{
    echo 'Miaow to '.$cat;
}

And of course using what we learnt earlier, we can remove the brackets too.

foreach (get_all_the_cats() as $cat)
    echo 'Miaow to '.$cat;

Optionally adding endforeach; to make it easier if you wish.