DePaul University
SNL 262
Web Page Design
Enhancing HTML With PHP
Web Wizard’s Chapter 2
Instructor: David A. Lash
David Lash
Objectives
To learn how to pass data from HTML forms to
PHP scripts
Learn how to access PHP variables
Learn to manipulate numeric and string variables
Using these variables to receive HTML form data
David Lash
2
PHP Where are we?
So far we learned that
PHP
PHP
PHP
PHP
PHP
enhances HTML (probably not sure how)
uses <?php ?> tags
can be mixed in with HTML
uses a print statement
runs before HTML page is returned and interpreted by browser
<htm> <head><title> My title </title> </head>
<body>
<font color=blue>
Well hello out there
<?php
print “I said <br> Hello”;
?> </font> </body> </html>
What good is that?
David Lash
3
Putting labels on data w/ variables
When you receive form data, you need some
way to label the data.
For example, when said
<input type=text name=mysuff size=20>
Your saying call whatever the type mystuff
You can give data names in PHP too
A variable name is a label used within a script
NOTE! Don’t worry
to refer to the data.
even though it’s a
variable we are not
doing “Math”
N a m e of v ar ia b le
$co st = 4.2 5;
$m ont hs = 12 ;
David Lash
V ar ia b le s n ew v al u e
Here we are setting
arbitrary names and
values for variables
4
within PHP
Assigning New Values to Variables
You can assign new values to variables:
$days = 3;
$newdays = 100;
$days = $newdays;
At the end of these three lines, $days and
$newdays both have values of 100.
David Lash
5
Think about them this way
3
100
$days
$newdays
$days = 3;
$newdays = 100;
$days = $newdays;
Put 3 -> $days
Put 100 -> $newdays
Put whatever is in $newdays
Into $days
David Lash
6
Selecting Variable Names
Almost any character set but must:
Use a dollar sign ($) as the first character
Use a letter or an underscore character (_) as the
second character.
Note:Try to select variable names that help
describe their function. For example $counter is
more descriptive than $c or $ctr.
Note2: You cannot use spaces in variable
names, so $happy day is not good but
$happy_day is.
David Lash
7
Using Variables and print
That is, to print out the value of $x, write the
following PHP statement:
6
print ("$x");
For example:
$age=6;
print ("Bryant is $age years old.");
$bryant
Would output:
Bryant is 6 years old.
David Lash
8
A Full Example ...
1. <html>
2. <head> <title>Variable Example </title>
</head>
3. <body>
12
4. <?php
5.
$first_num = 12;
$first_num
6.
$second_num = 356;
7.
$temp = $first_num;
8.
$first_num = $second_num;
9.
$second_num = $temp;
10.
print ("first_num= $first_num <br>
second_num=$second_num");
11. ?> </body> </html>
David Lash
356
$second_num
$temp
9
Another way to think about this
1. <html>
2. <head> <title>Variable Example </title>
</head>
3. <body>
$first_num $second_num $temp
4. <?php
5.
$first_num = 12;
12
6.
$second_num = 356;
356
7.
$temp = $first_num;
12
8.
$first_num = $second_num;
356
9.
$second_num = $temp;
12
10.
print ("first_num= $first_num <br>
second_num=$second_num");
11. ?> </body> </html>
David Lash
10
Objectives
To learn how to store and access data in PHP
variables
To understand how to create and manipulate
numeric and string variables
To learn how to pass data from HTML forms to
PHP scripts
David Lash
11
Using Arithmetic Operators
You can use operators such as a plus sign (+) for
addition and a minus sign (–) for subtraction to build
mathematical expressions.
For example,
<?php
$apples = 12;
$oranges = 14;
$total_fruit = $apples + $oranges;
print ("The total number of fruit is $total_fruit");
?>
These PHP statements would output
“The total number of fruit is 26.”
David Lash
12
Common PHP Numeric Operators
Table 2.1 Common PHP Numeric Operators
Operator
Effect
Example
Result
+
Addition
$x = 2 + 2;
$x is assigned 4.
-
Subtraction
$y is assigned 2.
/
Division
$y = 3;
$y = $y – 1;
$y = 14 / 2;
*
Multiplication
$y is assigned 16.
%
Remainder
$z = 4;
$y = $z * 4;
$y = 14 % 3;
David Lash
$y is assigned 7.
$y is assigned 2.
13
A Full Example
1. <html>
2. <head> <title>Variable Example </title> </head>
3. <body>
4. <?php
5.
$columns = 20;
6.
$rows = 12;
7.
$total_seats = $rows * $columns;
8.
9.
$ticket_cost = 3.75;
10.
$total_revenue = $total_seats * $ticket_cost;
11.
12.
$building_cost = 300;
13.
$profit = $total_revenue - $building_cost;
14.
15.
print ("Total Seats are $total_seats <br>");
16.
print ("Total Revenue is $total_revenue <br>");
17.
print ("Total Profit is $profit");
18. ?> </body> </html>
David Lash
14
A Full Example ...
1. <html>
2. <head> <title>Variable Example </title> </head>
3. <body>
4. <?php
5.
$columns = 20;
6.
$rows = 12;
7.
$total_seats = $rows * $columns;
8.
9.
$ticket_cost = 3.75;
10.
$total_revenue = $total_seats * $ticket_cost;
11.
12.
$building_cost = 300;
13.
$profit = $total_revenue - $building_cost;
14.
15.
print ("Total Seats are $total_seats <br>");
16.
print ("Total Revenue is $total_revenue <br>");
17.
print ("Total Profit is $profit");
18. ?> </body> </html>
See http://webwizard.aw.com/~phppgm/C2/numops.php
David Lash
15
What happens if …
If use a variable without assigning a value to it?
• Ans: It will have no value (called a null value).
•If use a null value in an expression may not get an error
•Instead PHP will try to complete the expression evaluation.
•For example, will output x= y=4.
<?php
$y = 3;
$y=$y + $x + 1;
print ("x=$x y=$y");
?>
David Lash
$x has null value uses 0
16
OK, we got to do a little math
Just like 5th grade math …
Need Operator precedence rules
define the order in which the operators are
evaluated. For example,
$x = 5 + 2 * 6;
The value of $x is either 42 or 17 depending on
order of evaluation.
Since multiplication evaluated before addition
operations, this expression evaluates to 17.
David Lash
17
PHP Precedence Rules
PHP follows the precedence rules listed below.
it evaluates operators within parentheses.
Next it evaluates multiplication and division operators.
Finally it evaluates addition and subtraction operators.
First
For example, the first two statements evaluate to
80 while the last to 180.
$x
= 100 - 10 * 2;
$y = 100 - (10 * 2);
$z = (100 - 10) * 2;
David Lash
18
A Full Example
Note: This example may not seem like much … but
suppose the values of $grade1, $grade2, $grade3
came from an input form (instead of being set here).
1. <html>
2. <head> <title>Expression Example </title> </head>
3. <body>
4. <?php
5.
$grade1 = 50;
6.
$grade2 = 100;
7.
$grade3 = 75;
8.
$average = ($grade1 + $grade2 + $grade3) / 3;
9.
print ("The average is $average");
10. ?> </body> </html>
David Lash
19
Wait … Why am I learning these Variables?
Don’t worry, this is not a “math” class
Later
will so you how to use these variables to
“receive” input from HTML forms.
For now just looking at how to manipulate
variables with pre-defined values
We looked at using variables to hold numbers…
Variables can hold strings of characters too …
Note: Why do you want to hold character strings? Consider input from a text
box or textarea? Input to your program is like to be characters (not numbers).
E.g., their comments
David Lash
20
Working with PHP String Variables
Character strings hold character
E.g., customer names, addresses, product
names, descriptions.
You enclose strings in quote marks (i.e., “ “ )
Consider the following example.
$name="Christopher";
$preference="Milk
Shake“;
$name -> “Christopher
$preference -> “Milk Shake”
David Lash
21
A String is not a number
You cannot add, subtract, multiple, etc
variables that hold strings
What would “Dave” / “Smith” be?
If use string as number won’t get error
PHP will try to convert to a 0
For example, will output ”y=1”
(not an error).
<?php
$x ="banana";
$sum = 1 + $x;
print ("y=$sum");
?>
David Lash
22
One thing you can do is ..
You can concatenate strings together
Think of this as combining them.
For example, combine 2 separate string variables into one.
The concatenate operator
For example,
$fullname = $firstname . $lastname;
$fullname will receive the string values of $firstname and
$lastname connected together.
For example,
$firstname = "John";
$lastname = "Smith";
$fullname = $firstname . $lastname;
print ("Fullname=$fullname");
Would output Fullname=JohnSmith
David Lash
23
Another way to concatenate
You can also use double quotation marks to
create
concatenation directly,
For example,
$Fullname2
= "$FirstName $LastName";
This statement has the same effect as
$Fullname2 = $FirstName . " " . $LastName;
David Lash
24
The strlen() Function
Most string functions require you to send
them one or more arguments.
Arguments are input values that functions
use in the processing they do.
Often functions return a value to the script
based on the input arguments. For example
$ le n
=
s tr l en ( $n am e );
V a r ia b l e o r v a lu e to w o rk w it h
R e c e iv e s th e n u m b e r o f
c h a ra c t e rs in $ n a m e
N a m e o f f u n c t io n
David Lash
25
The strlen() Function Example
<?php
$comments = "Good Job";
$len = strlen($comments);
print ("Length=$len");
?>
This PHP script would output “Length=8”.
Note: Why would I do this? Suppose input comes in from
text area, can tell how many characters they typed in.
David Lash
26
Objectives
To learn how to store and access data in PHP
variables
To understand how to create and manipulate
numeric and string variables
To learn how to pass data from HTML forms to
PHP scripts
David Lash
27
Recall HTML Input Forms
HTML Forms and not part of PHP language but
important way to send data to scripts
Text Box
Radio Buttons
Check Box
Select Box
Text Area
Submit/Reset button
David Lash
28
Starting And Ending HTML Forms
You can create HTML forms by using the HTML
<form> and </form> tags.
P r ogr am to sta r t w hen for m i s subm i tte d.
<form a ction="http:/ /webwi zard.aw.com/~ phppgm/progra m.php"
Form a t to
se nd da ta.
m ethod="post">
</form>
.
.
.
P l ac e fo rm e le m en ts be tw ee n
< for m > a nd < / for m > ta gs .
For m s e nd w ith < /for m >
David Lash
29
Another Full Script Example
1.<html>
2.<head> <title> A Simple Form </title> </head>
3.<body>
4.<form action="http://webwizard.aw.com/~phppgm/First.php"
method="post" >
5. Click submit to start our initial PHP program.
6. <br> <input type="submit" value="Click To Submit">
7. <input type="reset" value="Erase and Restart">
8. </form>
9. </body> </html>
This script contains
the line:
print “A simple initial script”;
David Lash
30
A Full Example ...
The previous code can be executed at
http://webwizard.aw.com/~phppgm/C2/form1.html
Question: What do you think you would get if First.php was not there?
David Lash
31
So now lets send data to a PHP
Script
To receive HTML form input into a PHP script:
Use $_POST[ ] and “get” the form element’s name argument.
For example, if form uses the following:
<input type="radio" name="contact" value="Yes">
Then form-handling PHP script could “get” the value of form
variable contact using
$my_var= $_POST[“contact”];
contact is set here and
received here
If user clicks radio button, then $my_var would = “Yes”
David Lash
32
Slow that down a little ...
To receive data use a special variable called
$_POST.
Enclose in square
bracket and then quotes
$my_var=
$_POST[“name”];
Name of HTML form
variable (note do not use $)
Special PHP Global variable. Technically it
is an associative array (covered in chptr 5.)
PHP variable name that you want to receive
the HTML form input.
David Lash
33
Full Example
Suppose your HTML form uses the following:
» Enter email address: <input type="text" size="16"
maxlength="20" name="email">
Then can receive input as follows:
These must
1. <html>
match
2. <head><title> Receiving Input </title> </head>
exactly
3. <body>
4. <font size=5>Thank You: Got Your Input.</font>
5. <?php
6.
$email = $_POST[“email”];
6.
print ("<br>Your email address is $email");
7.
8.
print ("<br> Contact preference is $contact");
9. ?>
David Lash
34
A Full Example ...
The previous code can be executed at
http://webwizard.aw.com/~phppgm/C2/Form4Radio_NG.html
David Lash
35
Sending email from PHP scripts
Sometimes it is useful to send email from
a PHP script:
PHP
uses mail() that by default sends e-mail via the
Simple Mail Transfer Protocol (SMTP).
mail(to_address, subject, message, extra_headers);
Specify the
destination
email
address.
Specify the
subject line of the
e-mail.
David Lash
Specify the
Text of the email
Specify additional
email headers.
36
Full Example, of sending email
Suppose your HTML form uses the following:
» Enter email address: <input type="text" size="16"
maxlength="20" name="email">
Then can receive input as follows:
1. <html>
2. <head><title> Receiving Input </title> </head>
3. <body>
4. <?php
5. $email = $_POST[“email”];
6. $contact = $_POST[“contact”];
7. print “<font size=5>Thank You: Got Your Input.</font>”;
8.
print ("<br>Your email address is $email");
9.
print ("<br> Contact preference is $contact");
10. ?>
David Lash
37
How send email again?
<html><head><title>Survey Form</title></head> <body>
<h1>Car Survey</h1>
<p><form METHOD="POST"
ACTION="http://condor.depaul.edu/~dlash/mailme.php">
<br>Name:<input type=text name="name" size="30" ><br>
What kind of options do you want on your car?
<select name="extras" size="3" multiple>
<option selected> Electric windows </option>
<option> AM/FM Radio </option>
<option> Turbo Charger </option>
<option> Automatic Transmission </option>
</select>
<p><input TYPE="reset" VALUE="Erase">
<input TYPE="submit" VALUE="Submit">
</form></body></html>
See http://condor.depaul.edu/~dlash/website/exampleform.html
David Lash
38
Email PHP Script ...
<?php
$fname = $_POST["fname"];
$extras = $_POST["extras"];
$dest='[email protected]';
$subject = "New Survey Data from $fname";
$message = "name=$fname \n extras=$extras";
$extra='From: [email protected]';
mail( $dest, $subject, $message, $extra );
print '<html> <head> <title> Email results
</title></head>';
print '<body> <font color=blue size=4> ';
print "Just sent email to $dest with form data from
$fname";
print "<br> subject=$subject";
print "</font></body></html>";
?>
David Lash
39
Another Example Average …
<html>
<head>
<title>Survey Form</title>
</head>
<body>
<h1>Class Survey</h1>
<FORM METHOD="POST" ACTION="average.php">
<br>Pick A Number: <BR><input type=text name=num1>
<br>Pick A Number 2: <BR><input type=text name=num2>
<br>Pick A Number 3: <BR><input type=text name=num3>
<br><input type="submit" value="Submit">
<input type="reset" value="Erase">
</form>
</body>
</html>
http://condor.depaul.edu/~dlash/extra/Webpage/examples/average.html
David Lash
40
The PHP Code …
<html> <head> <title> Guess the Dice </title>
<body>
<Font size=5 color=black> Your Averages Are:</font>
<form action="guessdice2.php" method=post>
<?php
$num1 = $_POST[num1];
$num2 = $_POST[num2];
$num3 = $_POST[num3];
$aver = ($num1 + $num2 + $num3 ) / 3;
print "<font size=4 color=blue>";
print "num1 = $num1 ";
print "num2 = $num2 ";
print "num3 = $num3 ";
print "</font> <br> <font size=4 color=red>";
print "<br>aver = $aver";
print "</font> ";
?>
<br>
</form> </body> </html>
David Lash
41
Summary
To learn how to store and access data in PHP
variables
To understand how to create and manipulate numeric
and string variables
$x = $y + 2;
$apple = $x + 5;
$first = “George”;
$last = “Bush”;
$full = “$first W. $last”;
To learn how to pass data from HTML forms to PHP
scripts
Receive as follows:
$name=$_POST[‘name’];
David Lash
42
© Copyright 2026 Paperzz