Conditional statements

PHP allows you to choose what action to take based upon the result of a condition. This condition can be anything you choose, and you can combine conditions together to make for actions that are more complicated. Here is a working example:

PHP evaluates if statements left to right, meaning that it first checks whether $Age is greater or equal to 18, then checks whether $Age is less than 50. The double ampersand, &&, means that both statements must be true if the print "You're in the prime of your life\n" code is to be executed - if either one of the statements is not true for some reason, "You're not in the prime of your life" is printed out instead.

As well as &&, there is also || (the pipe symbol printed twice) which means "OR". In this situation, if any of the conditions being checked in an if statement is true, the whole statement is considered true.

You have a vast array of ways to check statements, of which we have just looked at < (less than) and <= (less than or equal to), and >= (greater than or equal to). We will be looking at the complete list later, but first I want to mention one key check: ==, or two equals signs put together. That means, "is equal to". Therefore 1 == 1 is true, and 1 == 2 is false.

Did you notice that if statements use the code blocks I mentioned earlier? The code to be executed if the statement is true is in its own block (remember a block starts with { and finishes with }), and the code to be executed otherwise is in an else block. This stops PHP from trying to execute both the true and false actions.

One key thing to note is that PHP practises "if statement short-circuiting" - this is where PHP will try to do as little conditional work as possible, so it stops checking conditional statements as soon as it knows the result for sure. For example:

if ($Age > 10 && $Age < 20)

If $Age is 8, the first check ($Age > 10) will fail, so PHP will not even bother checking it against twenty. This means you can, for example, check whether a variable is set and whether it is set to a certain value - if the variable is not set, PHP will short-circuit the if statement and not check its value, which is good because if you check the value of an unset variable, PHP will flag an error!

A helpful addition to if statements is the elseif statement, which allows you to chain commands together in a slightly more intelligent way. Here is an example script:

You could reach the same effect by having lots of if statements, however this is a slightly neater way of doing things. The downside of this system is that the $Age variable needs to be checked repeatedly.

If you only have one line of code to execute, you can do without the braces entirely. Given that there is no speed difference between using braces and not using braces, it's usually down to a readability issue.

So, these two code chunks are the same:

if ($foo) {
echo $bar;
}
if ($foo) echo $bar;

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!