spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / php / corephp / 2 To page 1To page 2current page
[previous]

An Introduction to PHP

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


Listing 1.10 Computing the cost of lunch from a form

<?php
$Today = date(" l F d, Y");
?>
<html>
<head>
<title> Listing 1-10</title>
</head>
<body>
Today's date:
<?php
/*
** print today's date
*/
print("<h3>$Today</h3>\n");

/*
** 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.

1.9 Choosing Between Alternatives

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.

Listing 1.11 Conditional daily message

<html>
<head>
<title>Listing 1-11</title>
</head>
<body>
<h1>
<?php
/*
** Get today's day of the week
*/
$Today = date("l");

if($Today == "Friday")
{
print("Thank goodness it's Friday!");
}
else
{
print("Today is $Today.");
}
?>
</h1>
</body>
</html>

1.10 Repeating Code

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

Listing 1.12 Today's daily affirmation

<html>
<head>
<title>Listing 1-12</title>
</head>
<body>
<h1> Today's Daily Affirmation</h1>
Repeat three times:< br>
<?php
for($count = 1; $count <= 3; $count++)
{
print("< b>$ count</ b> I'm good enough, ");
print("I'm smart enough,");
print("and, doggone it, people like me!<br>\n");
}
?>
</h1>
</body>
</html>

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.

Authors:
By Leon Atkinson, Zeev Suraski.
ISBN:
0130463469
Retail Price:
$44.99 (Reg.)
Pages:
1104
Pub:

 

 

 

 

 

 


home / programming / php / corephp / 2 To page 1To page 2current page
[previous]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27, 2003
Revised: Sept. 1, 2003

URL: http://webreference.com/programming/php/corephp/2