PHP Conditional Statement(if else & php switch case): Conditionals alter program flow. They enable you to ask questions about certain things and respond to the answers you get in different ways. Conditionals are central to dynamic web pages—the goal of using PHP in the first place—because they make it easy to create different output each time a page is viewed. There are three types of non-looping conditionals: the if statement, the switch statement, and the? operator. By non-looping, I mean that the actions initiated by the statement take place and program flow then moves on, whereas looping conditionals (which we’ll get to shortly) execute code over and over until a condition has been met.
The if Statement
One way of thinking about program flow is to imagine it as a single-lane highway that you are driving along. It’s pretty much a straight line, but now and then you encounter various signs telling you where to go.
In the case of an if statement, you could imagine coming across a detour sign that you have to follow if a certain condition is TRUE. If so, you drive off and follow the detour until you return to where it started and then continue on your way in your original direction. Or, if the condition isn’t TRUE, you ignore the detour and carry on driving (see Figure 4-1).
The contents of the if a condition can be any valid PHP expression, including equality, comparison, tests for 0 and NULL, and even the values returned by functions (either builtin functions or ones that you write).
The actions to take when an, if a condition is TRUE, are generally placed inside curly braces, { }. However, you can ignore the braces if you have only a single statement to execute. But if you always use curly braces, you’ll avoid having to hunt down difficult-to-trace bugs, such as when you add an extra line to a condition and it doesn’t get evaluated due to lack of braces. (Note that for space and clarity, many of the examples in this book ignore this suggestion and omit the braces for single statements.) In Example 4-19, imagine that it is the end of the month and all your bills have been paid, so you are performing some bank account maintenance.
Example 4-19. An if statement with curly braces
<?php if ($bank_balance < 100) { $money = 1000; $bank_balance += $money; } ?>
In this example, you are checking your balance to see whether it is less than $100 (or whatever your currency is). If so, you pay yourself $1,000 and then add it to the balance. (If only making money were that simple!) If the bank balance is $100 or greater, the conditional statements are ignored and program flow skips to the next line (not shown).
In this book, opening curly braces generally, start on a new line. Some people like to place the first curly brace to the right of the conditional expression; others start a new line with it. Either of these is fine because PHP allows you to set out your whitespace characters (spaces, newlines, and tabs) any way you choose. However, you will find your code easier to read and debug if you indent each level of conditionals with a tab.
The else Statement
Sometimes when a conditional is not TRUE, you may not want to continue on to the main program code immediately but might wish to do something else instead. This is where the else statement comes in. With it, you can set up a second detour on your highway, as in Figure 4-2.
With an if … else statement, the first conditional statement is executed if the condition is TRUE. But if it’s FALSE, the second one is executed. One of the two choices must be executed. Under no circumstance can both (or neither) be executed. Example 4-20 shows the use of the if … else structure.
Example 4-20. An if … else statement with curly braces
<?php if ($bank_balance < 100) { $money = 1000; $bank_balance += $money; } else { $savings += 50; $bank_balance −= 50; } ?>
In this example, now that you’ve ascertained that you have $100 or more in the bank, the else statement is executed, by which you place some of this money into your savings account.
As with if statements, if your else has only one conditional statement, you can opt to leave out the curly braces. (Curly braces are always recommended, though. First, they make the code easier to understand. Second, they let you easily add more statements to the branch later.)
The elseif Statement
There are also times when you want a number of different possibilities to occur, based upon a sequence of conditions. You can achieve this using the elseif statement. As you might imagine, it is like an else statement, except that you place a further conditional expression prior to the conditional code. In Example 4-21, you can see a complete
if … elseif … else construct.
Example 4-21. An if … elseif … else statement with curly braces
<?php if ($bank_balance < 100) { $money = 1000; $bank_balance += $money; } elseif ($bank_balance > 200) { $savings += 100; $bank_balance −= 100; } else { $savings += 50; $bank_balance −= 50; } ?>
In the example, an elseif statement has been inserted between the if and else statements. It checks whether your bank balance exceeds $200 and if so, decides that you can afford to save $100 of it this month. Although I’m starting to stretch the metaphor a bit too far, you can imagine this as a multi-way set of detours (see Figure 4-3).
An else statement closes either an if … else or an if … else
if … else statement. You can leave out a final else if it is not
required, but you cannot have one before an elseif; neither can you
have an elseif before an if statement.
You may have as many elseif statements as you like. But as the number of elseif
statements increases, you would probably be better advised to consider a switch statement if it fits your needs. We’ll look at that next.