VISUAL QUICKPRO GUIDE
PHP and MySQL for
Dynamic Web Sites 4th Edition
Instructors Notes
Chapter 2: Programming with PHP
Copyright 2012 by Larry Ullman
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Learning Outcomes
Identify how key pieces of an HTML form relate to PHP
Choose the proper HTML form method
Handle HTML form data in a PHP script
Use common conditionals
Recognize and use the standard operators
Validate form data in several ways
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Learning Outcomes
Identify what arrays are
Recognize the superglobal arrays
Create arrays using different approaches
Access every element in an array
Sort arrays
Convert between arrays and strings
Use loops
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
An HTML Form
<form action="handle_form.php" method="post">
<p><label>Name: <input type="text" name="name" size="20"
maxlength="40" /></label></p>
<p><label>Email Address: <input type="text" name="email" size="40"
maxlength="60" /></label></p>
<p><label for="gender">Gender: </label><input type="radio"
name="gender" value="M" /> Male <input type="radio" name="gender"
value="F" /> Female</p>
<p><label>Age:
<select name="age">
<option value="0-29">Under 30</option>
<option value="30-60">Between 30 and 60</option>
<option value="60+">Over 60</option>
</select></label></p>
<p><label>Comments: <textarea name="comments" rows="3"
cols="40"></textarea></label></p>
<p align="center"><input type="submit" name="submit" value="Submit
My Information" /></p>
</form>
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Choosing a Method
GET
The standard method for
all server requests
Data appended to the
URL
Can be bookmarked
User can click Back
Used for requesting
information
POST
Data is not visible in the
URL
Much larger limit on the
amount of data that can
be submitted
Can send files
Users see warnings if
they click Back
Used for requesting action
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Handling a Form
Use $_REQUEST['name']
Or use $_GET['name'] and $_POST['name'],
depending upon the forms method value
Always load the form through a URL!
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Handling a Form
<?php # Script 2.2 - handle_form.php
// Create a shorthand for the form data:
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$comments = $_REQUEST['comments'];
// Print the submitted information:
echo "<p>Thank you, <b>$name</b>, for the following comments:<br />
<tt>$comments</tt></p>
<p>We will reply to you at <i>$email</i>.</p>\n;
?>
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Common Problems
Failure to load the form through a URL
Incorrect reference to the PHP script (e.g., location or
name)
Case-sensitivity issue with PHP variables
Incorrect element names in the HTML form
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Conditionals
if (condition) {
// Do this!
}
if (condition) {
// Do this!
} else {
// Do that!
}
if (condition1) {
// Do this!
} elseif (condition2) {
// Do that!
} else {
// Do whatever!
}
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
What is true?
$var, if $var has a value other than 0, an empty
string, FALSE, or NULL
isset($var), if $var has any value other than NULL,
including 0, FALSE, or an empty string
TRUE, true, True, etc.
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Operators
Symbol
Meaning
Type
==
is equal to
comparison
!=
is not equal to
comparison
<
less than
comparison
>
greater than
comparison
<=
less than or equal to
comparison
>=
greater than or equal to
comparison
not
logical
&&
and
logical
AND
and
logical
||
or
logical
OR
or
logical
XOR
and not
logical
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Validating Form Data
Never trust external data!
Use isset() to confirm variable has a value
Use !empty() to confirm variable has a non-empty
value
Check the datas type, when appropriate, for
example, using is_numeric()
Check the datas value, when appropriate.
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Introducing Arrays
Arrays hold multiple values in one variable
Series of key-value pairs
Indexed arrays use numbers for keys
Associative arrays use strings
Indexed arrays begin at 0 by default
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Accessing Arrays
// Use array notation to access individual elements:
$band = $artists[0];
echo $states['MD'];
// You cant do this:
echo "My list of states: $states";
// Or this:
echo "IL is $states['IL']."; // BAD!
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Superglobal Arrays
$_GET
$_POST
$_REQUEST
$_SERVER
$_ENV
$_SESSION
$_COOKIE
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Creating Arrays
$band[] = 'Jemaine';
$band[] = 'Bret';
$band[] = 'Murray';
$band['fan'] = 'Mel';
$band['fan'] = 'Dave'; // New value
$fruit[2] = 'apple';
$fruit[2] = 'orange'; // New value
$states = array (
'IA' => 'Iowa',
'MD' => 'Maryland'
);
$artists = array ('Clem Snide', 'Shins', 'Eels');
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Looping Through Arrays
foreach ($array as $value) {
// Do something with $value.
}
// Or:
foreach ($array as $key => $value) {
echo "The value at $key is $value.";
}
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Sorting Arrays
sort(), by value, dropping the keys
asort(), by value, maintaining the keys
ksort(), by key, maintaining the values
rsort(), sort() in reverse order
arsort(), asort() in reverse order
krsort(), ksort() in reverse order
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Arrays and Strings
$array = explode (separator, $string);
$string = implode (glue, $array);
$s1 = 'Mon-Tue-Wed-Thu-Fri';
$days_array = explode ('-', $s1);
// $days_array now a five-element array, with Mon indexed at 0.
$s2 = implode (', ', $days_array);
// $s2 now a comma-separated list of days: Mon, Tue, Wed, Thu, Fri.
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
While Loops
while (condition) {
// Do something.
}
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
For Loops
for (initial expression; condition; closing expression) {
// Do something.
}
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
LEARN THE QUICK AND EASY WAY!