HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [211]
A is larger than or equal to B
A<= B
A is less than or equal to B
Note that PHP determines the variable type dynamically, so comparisons between numeric and string values may cause problems. It’s best to explicitly force variables to the type you want if you’re not sure. For example, if you want to ensure that the variable $a is an integer before you compare it to the value 4, you could use this condition:
(integer)$a == 4
This will force the variable $a to be read as an integer. You can also use this technique (called typecasting) to force a variable to other types: float, string, or boolean.
Taking the middle road
Another variation of the if structure allows you to check multiple conditions. As an example, look at the highMidLow.php page featured in Figure 3-4.
Figure 3-4: Now there are three possible comments, thanks to the else if structure.
If the roll is 1 or 2, the program reports Low. If the roll is 3 or 4, it says Middle; and if it’s 5 or 6, the result is High. This if has three branches. See how it works; you can add as many branches as you wish.
High, middle, or low?
$roll = rand(1,6);
print << alt = “$roll“ /> HERE; if ($roll > 4){ print “ } else if ($roll <= 2){ print “ } else { print “ } // end if ?> The if statement is the only part of this program that’s new. It’s not terribly shocking. 1. Begin with a standard condition. Check whether the roll is greater than 4. If so, say High. If the first condition is true, the computer evaluates the code in the first section and then skips the rest of the while loop. 2. Add a second condition. The else if section allows me to add a second condition. This second condition (roll <= 2) is evaluated only if the first condition is false. If this condition is true, the code inside this block will be executed (printing the value Low). You can add as many else if sections as you want. As soon as one is found to be true, the code block associated with that condition executes, and the program leaves the whole else system. 3. Include an else clause to catch stragglers. If none of the previous conditions are true, the code associated with the else clause operates. In this case, the roll is lower than 4 and higher than 2, so report that it’s in the Middle. An especially important application of the if structure is unique to server-side programming. Up to now, many of your PHP programs required two separate files: an HTML page to get information from the user and a PHP program to respond to that code. Wouldn’t it be great if the PHP program could determine whether it had the data or not? If it has data, it will process it. If not, it just produces a form to handle the data. That would be pretty awesome, and that’s exactly what you can do with the help of the if statement. Figure 3-5 shows the first pass of ownForm.php. Figure 3-5: On the first pass, ownForm.php produces an HTML form. The interesting thing happens when the user submits the form. The program calls itself! This time, though, ownForm recognizes that the user has sent some data and processes that information, giving the result shown in Figure 3-6. Figure 3-6: Now the same program processes the data! This program doesn’t really require anything new, just a repurposing of some tools you already know. Take a look at the following code:

High!
\n”;Low
\n”;Middle
\n”;
Building a program that makes its own form