| home / programming / php / corephp / 2 | [previous] |
|
|
/*
** print message about lunch cost
*/
print($_ REQUEST['YourName'] . ", you will be out ");
print($_ REQUEST['CostOfLunch'] *
$_ REQUEST['DaysBuyingLunch']);
print("dollars this week.< br>\n");
?>
</body>
</html>
Notice that in the first segment of the PHP script, I have eliminated the lines setting the variables, except for today's date. See how instead of using $CostOfLunch, I used $_ REQUEST[ 'CostOfLunch']? PHP collects all the variables sent by forms and cookies into a collection called _REQUEST. The technical name for this type of data is array, the subject of Chapter 5.
Try experimenting with the scripts by entering nonsense in the form fields. One thing you should notice is that if you put words where the script expects numbers, PHP seems to just assign them values of zero. The variables are set with a text string, and when the script tries to treat it as a number, PHP does its best to convert the information. Entering 10 Little Indians for the cost of lunch will be interpreted as 10.
PHP allows you to test conditions and execute certain code based on the result of the test. The simplest form of this is the if statement. Listing 1. 11 shows how you can customize the content of a page based on the value of a variable (see Figure 1. 4).
The Today variable is set with the name of today's weekday. The if statement evaluates the expression inside the parentheses as either true or false. The == operator compares the left side to the right side. If Today contains the word Friday, the block of code surrounded by curly braces ({ and }) is executed. In all other cases the block of code associated with the else statement is executed.
if($Today == "Friday")
{
print("Thank goodness it's Friday!");
}
else
{
print("Today is $Today.");
}
?>
</h1>
</body>
</html>
The last type of functionality in this brief introduction is looping. Looping allows you to repeat the execution of code. Listing 1. 12 is an example of a for loop. The for statement expects three parameters separated by semicolons. The first parameter is executed once before the loop begins. It usually initializes a variable. The second parameter makes a test. This is usually a test against the variable named in the first parameter. The third parameter is executed every time the end of the loop is reached (see Figure 1. 5).
The for loop in Listing 1. 12 will execute three times. The initialization code sets the variable count to be one. Then the testing code compares the value of count to three. Since one is less than or equal to three, the code inside the loop executes. Notice that the script prints the value of count. When you run this script, you will find that count will progress from one to three. The reason is that the third part of the for statement is adding one to count each time through the loop. The ++ operator increments the variable immediately to its left.
The first time through the loop, count is one, not two. This is because the increment of count doesn't occur until we reach the closing curly brace. After the third time through the loop, count will be incremented to four, but at that point four will not be less than or equal to three, so the loop will end. Execution continues at the command following the loop code block.
An Introduction to PHP was taken from Chapter 1 of the Prentice Hall title: Core_PHP Programming, 3rd Edition.
Created: March 27, 2003 URL: http://webreference.com/programming/php/corephp/2 |