Logic and Loops

I l @ ve RuBoard

Logic and Loops

Controlling 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.

Logic

PHP 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.

graphics/03fig08.gif

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.

graphics/03fig09.gif

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.

Iteration

PHP 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:

  1. What value to start the loop from ”in this case, 1 ( $count = 1 ).

  2. How the value is tested . In this case, if the value reaches 10, the loop stops ( $count <= 10 ).

  3. How the value is altered as it loops. In this case, the value is incremented in each loop ( $count++ ).

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.

graphics/03fig10.gif

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

Condition

Meaning

==

Equal to

!=

Not equal to

<

Less than

>

Greater than

>=

Greater than or equal to

<=

Less than or equal to

===

Equal to and of the same data type

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.

graphics/03fig11.gif

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.

graphics/03fig12.gif

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


PHP Programming for Windows
PHP Programming for Windows (Landmark (New Riders))
ISBN: 0735711690
EAN: 2147483647
Year: 2002
Pages: 99

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net