spacer

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

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

An Introduction to PHP

Vice President of Risk Technology - READY TO HIRE! (NYC)
Next Step Systems
US-NY-New York

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.8 Assigning values to variables

<?php
$YourName = "Leon";
$Today = date(" l F d, Y");
$CostOfLunch = 3.50;
$DaysBuyingLunch = 4;
?>
<html>
<head>
<title>Listing 1-8</title>
</head>
<body>
Today's Date:
<?php
/*
** print today's date
*/
print("<h3>$Today</h3>\n");

/*
** print message about lunch cost
*/
print("$YourName, you will be out ");
print($CostOfLunch * $DaysBuyingLunch);
print(" dollars this week.< br>\n");
?>
</body>
</html>

The third and fourth assignments are putting numerical data into variables. The value 3.5 is a floating-point, or fractional, number. PHP calls this type a double, showing some of its C heritage. The value 4 in the next assignment is an integer, or whole number.

After printing some HTML code, another PHP code block is opened. First the script prints today's date as a level-three header. Notice that the script passes some new types of information to the print function. You can give string literals or string variables to print, and they will be sent to the browser.

When it comes to variables, PHP is not so lenient with case. Today and today are two different variables. Since PHP doesn't require you to declare variables before you use them, you can accidentally type today when you mean Today and no error will be generated by default. If variables are unexpectedly empty, check your case. You can also catch these sorts of errors by configuring PHP to warn you of uninitialized variables. See Chapter 15's description of error reporting.

The script next prints Leon, you will be out 14 dollars this week. The line that prints the total has to calculate it with multiplication, using the * operator.

1.8 Receiving User Input

Manipulating variables that you set within your script is somewhat interesting, but hardly anything to rave about. Scripts become much more useful when they use input from the user. When you call PHP from an HTML form, the form fields are turned into variables. Listing 1.9 is a form that calls Listing 1. 10, a further modifica-tion of our example script.

Listing 1.9 HTML form for lunch information

<html>
<head>
<title>Listing 1-9</title>
</head>
<body>
<form action="1-10.php" method="post">
Your name:
<input type="text" name="YourName">< br>

1.8 Receiving User Input 23

Cost of a lunch:
<input type="text" name="CostOfLunch">< br>
Days buying lunch:
<input type="text" name="DaysBuyingLunch">< br>
<input type="submit" value="Compute">
</form>
</body>
</html>

Listing 1.9 is a standard HTML form. If you have dealt at all with CGIs, it will look familiar. There are three form fields that match up with the variables from our previous example. Instead of simply putting data into the variables, we will provide a form and use the information the user types. When the user presses the submit button, the script named in the ACTION attribute will receive the three form fields, and PHP will convert them into variables (see Figure 1. 3).

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

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