I l @ ve RuBoard |
Logic and LoopsControlling a program's flow is vitally important. You might need to test some logic conditions (is the value true or false?) or loop through some values. Such elements have always been present in programming languages, and PHP is no exception. LogicPHP gives you several means of testing logic. When you test logic, you look at a value and ask if a condition of the value is or is not equal to a value: <?php $password = "a12b"; if($password == "a12b") { print("Password found"); } ?> This code uses the PHP if statement. Its syntax is straightforward. If a value equals the value you want, PHP returns a true value. This code asks if the variable $password is equal to a12b . If it is, a message is displayed, as shown in Figure 3.8. Figure 3.8. Testing true logic with the if statement.
What would happen if the variable $password were not equal to the value you are looking for? Using the preceding example, it would not display anything. However, PHP lets you handle false logic conditions as well: <?php $password = "3333"; if($password == "a12b") { print("Password found"); } else { print("Password not found"); } ?> If the value you are looking for is not found, you resort to the else condition. If the variable $password has the value you are looking for, the Password found message is displayed. However, if the value you are looking for is not found, the Password not found message is displayed. Setting the variable $password to something that is not found triggers the Password not found message, as shown in Figure 3.9. Figure 3.9. Testing false logic with the if else statement.
If you have lots of if statements to test, you could use the following: <?php $password = "bb22"; if($password == "aa11") { Print("Password for user Joe found"); } if($password == "bb22") { Print("Password for user Fred found"); } if($password == "cc33") { Print("Password for user Jack found"); } ?> The biggest problem with this approach is that PHP executes all the if statements even when a true condition is returned. To get around this, you can use nested if statements: <?php $password = "bb22"; If($password == "aa11") { Print("Password for user Joe found"); } elseIf($password == "bb22") { Print("Password for user Fred found"); } elseIf($password == "cc33") { Print("Password for user Jack found"); } ?> However, if you have a lot of nested if statements, your code can quickly become unreadable. A better approach is to use the switch and case statements: <?php $password = "bb22"; switch($password) { case "aa11": Print("Password for user Joe found"); break; case "bb22": Print("Password for user Fred found"); break; case "cc33": Print("Password for user Jack found"); break; } ?> Here you test each value of the variable $password in a case statement. If a matching value is found, the Password found message is displayed. Note that I used a break statement so that when a true condition is returned, it stops executing the switch statement. Without it, the switch statement would behave like our original if statements and continue to search all the way through. IterationPHP lets you loop through conditions using several means. All loop conditions let you loop through the code until a condition is met, such as until a count reaches a certain value. The first method you can use is the for loop: <?php for ($count = 1; $count <= 10; $count++) { print("$count"); print("<BR>"); } ?> The for loop breaks up into the following:
So this loop counts from 1 to 10 and then stops (see Figure 3.10). Figure 3.10. An incrementing for loop counting from 1 to 10.
A few important elements occur within the for loop. The first is the testing of logic. The preceding example sees whether the value is equal to or less than 10. You can, however, test for other conditions, as shown in Table 3.1. Table 3.1. Other Conditions
You can use these conditions not only with the for statement but with any statement that can test logic, such as the if statement. In the preceding for statement, note the way in which the loop is altered as it loops. You can either increment or decrement the loop. You decrement it using the following: $count-- Thus, you can alter the example as follows : <?php for ($count = 10; $count >= 1; $count--) { print("$count"); print("<BR>"); } ?> If you run the script, you can see that it now counts from 10 to 1, as shown in Figure 3.11. Figure 3.11. A decrementing for loop counting from 10 to 1.
PHP gives you another way to loop through code ”the while statement: <?php $count = 1; while ($count <= 10) { print("$count"); print("<BR>"); $count++; } ?> First, you set the count to a starting value ”in this case, 1: $count = 1; Next you loop until the count reaches 10: while ($count <= 10) { As with the for loop, you can use various forms of logic testing. Inside the loop of the while statement, the count is incremented: $count++; Of course, you can decrement the code in the same way you would with the for loop. If you run the script, you can see that the while loop counts from 1 to 10 before stopping, as shown in Figure 3.12. Figure 3.12. An incrementing while loop counting from 1 to 10.
PHP lets you determine how the loop is controlled through the use of the do while statement: <?php $count = 1; do { print("$count"); print("<BR>"); $count++; } while ($count <= 10); ?> This looks similar to the while loop. If you run this script, it will count from 1 to 10 before stopping. So why use this script instead of the while loop? The difference between the two statements is in the placement of the while statement. With do while loops, the value is altered (in this case, incremented), and then the value is tested. With the while statement, the loop's value is tested and then altered. It is important to note when a loop's value is tested and when it is altered. In the preceding do while loop, value 1 is never tested, because it is incremented to 2 before it can be tested. |
I l @ ve RuBoard |