Playing
01. Fun with Ternery Operators in PHP

Episodes

0%
Your progress
  • Total: 5m
  • Played: 0m
  • Remaining: 5m
Join or sign in to track your progress

Transcript

00:00
We're going to be looking at ternary operators within PHP and how they can be really useful, save you a little bit of time, and clean up your code.
00:08
We're also going to look though at how they can get a little bit messy, a little bit hard to understand, and how just generally you need to apply some common sense when using them. Okay, so let's take a look at an example of checking if something is set in the query
00:25
string and then we'll output a result, otherwise we'll do something else. Normally, you would say something like, if is set, get name, which is perfectly acceptable. So let's go and put this into the URL, so we'll say name equals Billy. So if the name is set, we're going to go ahead and echo out, get name, otherwise we're going
00:53
to echo out, no name. So let's see what this does. We can see it echoes out Billy, and if we don't supply a name, we get no name. Pretty straightforward.
01:07
So how can we make this or turn this into one line, which is fairly easy to understand? Well we can scrap all of this, and our end result is to echo something. So we can start by saying echo, we can then do the comparison, so in this case it would be the return value of is set, whether it's true or false, so we can say that.
01:31
Then what we do is we use a question mark. This is kind of like the block that you would run if this evaluated to true. In this case, we're just going to write get name. So you can imagine it's echo this if this returns true, if this evaluates to true.
01:50
Now otherwise, so this colon here is our else part of it. We want to output the string no name. So in both of these cases, we're just doing what it says at the start of the line. This is just our comparison operator.
02:04
So this is the equivalent to what we just wrote over many more lines. So we can see that this works in exactly the same way. We get no name, and when we do supply a name, we output that name. So what we're now going to do is look at a more complex example of how things can get
02:20
a little bit messy, and this isn't the most complex ternary operator we're going to look at or ternary operation we're going to look at, but it does still look a little bit messy. So what we're going to do then is we're going to ask for a secret from the query string. So we're going to say secret equals dogs or cats, and cats is going to be the correct
02:40
secret. So we're just asking the user to provide the correct secret. So we want to output correct if the secret is correct. We want to output incorrect if the secret isn't, and we want to output no secret supplied
02:56
if we don't have a secret supplied. So we can do this by the following. We first of all want to check if get secret exists. If it does exist, we want to do a check here.
03:08
Otherwise, we want to say no secret. So the check in here, we can do another ternary operator, so we can embed them much like we can embed if statements. And in here what we're going to do is we're going to say get secret because at this point
03:25
we already know that's set, so it's not going to cause an error, and we're going to check that equals cats. If it does equal cats, we're going to say correct. If it doesn't, we're going to say incorrect.
03:38
So you can see that this line by just glancing at it is pretty difficult to understand. I'm not saying you shouldn't do this. There may be circumstances where this would be better to be used just to clean up code, but really it's kind of a fine line between making things as short and efficient as possible
03:56
or just making it plain unreadable. So you can see here if we switch over, we get incorrect for dogs. For cats, we get correct, and then if we get rid of all of this, you can see we get no secret.
04:10
So that's done exactly the same thing as if we were to write lots of if statements. So now let's look at another example, and this is kind of the way I most use ternary operators. Let's say in our database somewhere we have a timestamp.
04:26
So this could be something like 2.15, 10.15 at 9 o'clock. Now this could be a null value. So let's just say it's a nullable value in the database, and we're saying well this could be null or it could contain this string here.
04:43
So let's keep it as this string for now. What we're going to do is we're going to echo out timestamp, and then we're going to place the question mark and the colon right next to each other, and then we're going to say no timestamp.
04:56
Now what this is doing is it's essentially saying does this contain a faulty value, which includes null. It could be false, zero, null, whatever. If it does, output this.
05:09
Otherwise output this. So it's a much cleaner way to do this, and this is a really nice clean way to output things based on the fact that they're true or false or exist or not. So now you can see if we go ahead and refresh it, we actually get that timestamp output.
05:25
However, if this was a null, a false, or a zero value, for example if it's a database it would be a null, you can see that we get no timestamp output. So I really love this way of doing things. It makes code a lot cleaner, and if you kind of learn what this means, it's pretty obvious
05:42
as soon as you look at this what we're trying to attempt. So that's a little introduction on ternary operators with PHP. They're really great for making short, efficient code, but beware if they start to get really long, you should probably just use another method of checking.
1 episode 5 mins

Overview

How ternery operators can clean up your code and speed up development.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!