PHP Shorthand If And Else Assignments

What you don't hear as a beginner

When you are learning a scripting language such as PHP you will get the basics of assignment, evaluating and comparison operators such as == <= and != (equal, less than or equal, not equal). They are all you need to start writing code that can vary it's behaviour according to what inputs it is given. Before long though you will want quicker ways of doing things. In this blog post I'm going to start with the long way of doing assignments and then show you a much quicker way of doing it.

Assignments - The Long way round

When writing code, you will quite often want to assign a value to a variable based on a condition. For example..

//Make the variable $today hold the value Thursday $today = "Thursday"; //Test if the variable $today is Friday, //if it is then we can go to the pub for lunch if($today == "Friday"){ $goToThePubForLunch = true; } else { $goToThePubForLunch = false; } //returns false. We aren't going to the pub today :(

This does the job, but takes up more space that it really needs to. We have a variable duplicated and have used 5 lines when really 1 should do. It would be great to shorten it and make our code more readable.

The Answer - ?:

This is where the Ternary operator "?:" comes in.

The pseudocode for the ternary operator is (expr1) ? (expr2) : (expr3) which basically means "if expression 1 is true, then return expression2 and if expression1 is false return expression3. (An expression can be thought of as anything that has a value)

In the case of our first example expression1 would be

$today == "Friday"

expression2 would be true
and expression3 would be false

So we can rewrite the above as

$today = "Thursday"; $goToThePubForLunch = ($today == "Friday" ? true : false); //Returns false

This is much quicker and after using it a few times becomes as readable as the first version

Can we make it shorter?

Of course we can!

In the above example we are returning what is called a boolean variable. It is either true or false.

The comparison operators we are used to using such as "==" or "!=" return a boolean value when they are evaluated. By using that fact we can shorten the code to

$today = "Thursday"; $goToThePubForLunch = $today == "Friday"; //$today == "Friday" returns false //which is then assigned to $goToThePubForLunch

Why use ternary operators then?

Well, remember that ternary operators allow us to evaluate any expression? We can increase what we can do with 1 line by putting a function call or a variable with something other than a boolean value in expression2 or 3

$today = "Friday"; $whereAreWeHavingLunch = ($today == "Friday" ? getRandomPub() : "In The Office"); //returns a random pub near by

Here we have added a function that chooses a pub to have lunch at. It only gets called if today is friday otherwise we just have lunch "in the office".

There you go, a couple of quick ways to condense if else statements. For more info on ternary operators you can go to the php documentation site
php.net/manual/en/language.operators.comparison.php

Posted by:

Superdream

Previous Article

Back to Blog

Next Article

Discover More

want to talk?

Get in Touch