control structures. the if conditional condition must go within parentheses statements come after...
TRANSCRIPT
Control Structures
The if Conditional
Condition must go within parentheses Statements come after the curly brace
Example:
if (empty ($name)) {
print ‘<p>please enter a name.</p>’
}
Form
Code Example for the if Conditional 6.2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Registration</title> </head> <body> <?php // Script 6.2 - handle_reg.php // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // In case register_globals is disabled. $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $email = $_POST['email']; $password = $_POST['password']; $confirm = $_POST['confirm']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $color = $_POST['color']; print '<p>Registration Results:</p>'; // Validate the first name. if (empty ($first_name)) {
print '<p>Please enter your first name.</p>'; } // Validate the last name. if (empty ($last_name)) {
print '<p>Please enter your last name.</p>'; } // Validate the email address. if (empty ($email)) {
print '<p>Please enter your email address.</p>'; } // Validate the password. if (empty ($password)) {
print '<p>Please enter your password.</p>'; } ?> </body> </html>
Using else 6.3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Registration</title> </head> <body> <?php // Script 6.3 - handle_reg.php - second version after Script 6.2 // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // In case register_globals is disabled. $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $email = $_POST['email']; $password = $_POST['password']; $confirm = $_POST['confirm']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $color = $_POST['color']; print '<p>Registration Results:</p>'; // Validate the first name. if (empty ($first_name)) { print '<p>Please enter your first name.</p>'; }
// Validate the last name. if (empty ($last_name)) { print '<p>Please enter your last name.</p>'; } // Validate the email address. if (empty ($email)) { print '<p>Please enter your email address.</p>'; } // Validate the password. if (empty ($password)) { print '<p>Please enter your password.</p>'; } // Validate the month. if (is_numeric ($month)) { $birthdate = $month . '-'; } else { print '<p>Please select the month you were born.</p>'; } // Validate the day. if (is_numeric ($day)) { $birthdate .= $day . '-'; } else { print '<p>Please select the day you were born.</p>'; } // Validate the year. if (is_numeric ($year)) { $birthdate .= $year; } else { print '<p>Please enter the year you were born as four digits.</p>'; } print "You entered your birthday as $birthdate"; ?> </body> </html>
PHP Operators
Operator Usage Type== Equality Comparison
!= Inequality Comparison
AND/&& And logical
OR/l l Or logical
. Concatenation string
.= Concatenates to the values of a
variable
Concatenation and assignment
Example on operators 6.4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>Registration</title> </head> <body> <?php // Script 6.4 - handle_reg.php - third version after Scripts 6.2 and 6.3 // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // In case register_globals is disabled. $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $email = $_POST['email']; $password = $_POST['password']; $confirm = $_POST['confirm']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $color = $_POST['color']; print '<p>Registration Results:</p>'; // Validate the first name. if (empty ($first_name)) { print '<p>Please enter your first name.</p>'; }
// Validate the last name. if (empty ($last_name)) { print '<p>Please enter your last name.</p>'; } // Validate the email address. if (empty ($email)) { print '<p>Please enter your email address.</p>'; } // Validate the password. if (empty ($password)) { print '<p>Please enter your password.</p>'; } // Check the two passwords for equality. if ( $password != $confirm ) { print '<p>Your confirmed password does not match the original password.</p>'; } // Validate the month. if (is_numeric ($month)) { $birthdate = $month . '-'; } else { print '<p>Please select the month you were born.</p>'; } // Validate the day. if (is_numeric ($day)) { $birthdate .= $day . '-'; } else { print '<p>Please select the day you were born.</p>'; } // Validate the year. if (is_numeric ($year)) { $birthdate .= $year; } else { print '<p>Please enter the year you were born as four digits.</p>'; } // Check that they were born before 2004. if ($year >= 2004) { print '<p>Either you entered your birth year wrong or you come from the future!</p>'; } print "You entered your birthday as $birthdate"; ?> </body> </html>
Using elseif 6.6 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>Registration</title> </head> <body> <?php // Script 6.6 - handle_reg.php - fifth version after Scripts 6.2, 6.3, 6.4 and 6.5 // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // In case register_globals is disabled. $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $email = $_POST['email']; $password = $_POST['password']; $confirm = $_POST['confirm']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $color = $_POST['color']; print '<p>Registration Results:</p>'; // Validate the first name. if (empty ($first_name)) { print '<p>Please enter your first name.</p>'; } // Validate the last name. if (empty ($last_name)) { print '<p>Please enter your last name.</p>'; } // Validate the email address. if (empty ($email)) { print '<p>Please enter your email address.</p>'; }
// Validate the password. if (empty ($password)) { print '<p>Please enter your password.</p>'; } // Check the two passwords for equality. if ($password != $confirm) { print '<p>Your confirmed password does not match the original password.</p>'; } // Validate the month. if (is_numeric ($month)) { $birthdate = $month . '-'; } else { print '<p>Please select the month you were born.</p>'; } // Validate the day. if (is_numeric ($day)) { $birthdate .= $day . '-'; } else { print '<p>Please select the day you were born.</p>'; } // Validate the year. if ( is_numeric ($year) AND strlen ($year) == 4 ) { // Check that they were born before 2004. if ($year >= 2004) { print '<p>Either you entered your birth year wrong or you come from the future!</p>'; } else { $birthdate .= $year; } // End of 2nd conditional.
} else { // Else for 1st conditional. print '<p>Please enter the year you were born as four digits.</p>';
} // End of 1st conditional. // Validate the color. if (!$color) { print '<p>Please select your favorite color.</p>'; }
print "You entered your birthday as $birthdate"; // Get the horoscope sign. if ($month == 1) { // January if ($day <= 19) { $sign = 'Capricorn'; } else { $sign = 'Aquarius'; } } elseif ($month == 2) { // February if ($day <= 18) { $sign = 'Aquarius'; } else { $sign = 'Pisces'; } } elseif ($month == 3) { if ($day <= 20) { $sign = 'Pisces'; } else { $sign = 'Aries'; } } elseif ($month == 4) { if ($day <= 19) { $sign = 'Aries'; } else { $sign = 'Taurus'; } } elseif ($month == 5) { if ($day <= 20) { $sign = 'Taurus'; } else { $sign = 'Gemini'; } } elseif ($month == 6) { if ($day <= 19) { $sign = 'Gemini';
} else { $sign = 'Cancer'; } } elseif ($month == 7) { if ($day <= 22) { $sign = 'Cancer'; } else { $sign = 'Leo'; } } elseif ($month == 8) { if ($day <= 22) { $sign = 'Leo'; } else { $sign = 'Virgo'; } } elseif ($month == 9) { if ($day <= 22) { $sign = 'Virgo'; } else { $sign = 'Libra'; } } elseif ($month == 10) { if ($day <= 22) { $sign = 'Libra'; } else { $sign = 'Scorpio'; } } elseif ($month == 11) { if ($day <= 21) { $sign = 'Scorpio'; } else { $sign = 'Sagittarius'; } } elseif ($month == 12) { if ($day <= 21) { $sign = 'Sagittarius'; } else { $sign = 'Capricorn'; } } print "<p>You are a(n) $sign.</p>"; ?> </body> </html>
The Switch Conditional
The switch conditional takes only one possible condition which is the value of a variable. Example:
switch ($variable) {
case “value”:
statements;
break;
}
Switch Code Example 6.7 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>Registration</title> </head> <body> <?php // Script 6.7 - handle_reg.php - sixth version after Scripts 6.2, 6.3, 6.4, 6.5 and 6.6 // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // In case register_globals is disabled. $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $email = $_POST['email']; $password = $_POST['password']; $confirm = $_POST['confirm']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $color = $_POST['color']; print '<p>Registration Results:</p>'; // Validate the first name. if (empty ($first_name)) { print '<p>Please enter your first name.</p>'; } // Validate the last name. if (empty ($last_name)) { print '<p>Please enter your last name.</p>'; } // Validate the email address. if (empty ($email)) { print '<p>Please enter your email address.</p>'; }
// Validate the password. if (empty ($password)) { print '<p>Please enter your password.</p>'; } // Check the two passwords for equality. if ($password != $confirm) { print '<p>Your confirmed password does not match the original password.</p>'; } // Validate the month. if (is_numeric ($month)) { $birthdate = $month . '-'; } else { print '<p>Please select the month you were born.</p>'; } // Validate the day. if (is_numeric ($day)) { $birthdate .= $day . '-'; } else { print '<p>Please select the day you were born.</p>'; } // Validate the year. if ( is_numeric ($year) AND strlen ($year) == 4 ) { // Check that they were born before 2004. if ($year >= 2004) { print '<p>Either you entered your birth year wrong or you come from the future!</p>'; } else { $birthdate .= $year; } // End of 2nd conditional.
} else { // Else for 1st conditional. print '<p>Please enter the year you were born as four digits.</p>';
} // End of 1st conditional. // Validate the color. if (!$color) {
print '<p>Please select your favorite color.</p>'; } print "You entered your birthday as $birthdate"; // Get the horoscope sign. switch ($month) { case 1: if ($day <= 19) { $sign = 'Capricorn'; } else { $sign = 'Aquarius'; } break; case 2: if ($day <= 18) { $sign = 'Aquarius'; } else { $sign = 'Pisces'; } break; case 3: if ($day <= 20) { $sign = 'Pisces'; } else { $sign = 'Aries'; } break; case 4: if ($day <= 19) { $sign = 'Aries'; } else { $sign = 'Taurus'; } break; case 5: if ($day <= 20) { $sign = 'Taurus'; } else { $sign = 'Gemini'; } break; case 6: if ($day <= 19) { $sign = 'Gemini'; } else { $sign = 'Cancer';
} break; case 7: if ($day <= 22) { $sign = 'Cancer'; } else { $sign = 'Leo'; } break; case 8: if ($day <= 22) { $sign = 'Leo'; } else { $sign = 'Virgo'; } break; case 9: if ($day <= 22) { $sign = 'Virgo'; } else { $sign = 'Libra'; } break; case 10: if ($day <= 22) { $sign = 'Libra'; } else { $sign = 'Scorpio'; } break; case 11: if ($day <= 21) { $sign = 'Scorpio'; } else { $sign = 'Sagittarius'; } break; case 12: if ($day <= 21) { $sign = 'Sagittarius'; } else { $sign = 'Capricorn'; } break; } // End of switch. print "<p>You are a(n) $sign.</p>"; ?> </body> </html>
The for Loop
PHP supports three kinds of loops: for: performs specific statements for a determined
number of iterations. while: runs until a condition is false foreach: commonly used with arrays
Code Example 6.8 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>Register</title> </head> <body> <!-- Script 6.8 - register.php --> <p>Please complete this form to register: </p> <form action="handle_reg.php" method="post"> <p>First Name: <input type="text" name="first_name" size="20" /></p> <p>Last Name: <input type="text" name="last_name" size="20" /></p> <p>Email Address: <input type="text" name="email" size="20" /></p> <p>Password: <input type="password" name="password" size="20" /></p> <p>Confirm Password: <input type="password" name="confirm" size="20" /></p> <p>Date Of Birth: <select name="month"> <option value="">Month</option> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> <option value="4">April</option> <option value="5">May</option> <option value="6">June</option> <option value="7">July</option> <option value="8">August</option> <option value="9">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> <select name="day"> <option value="">Day</option> <?php
// Print out 31 days. for ($d = 1; $d <= 31; $d++) { print "<option value=\"$d\">$d</option>\n"; } ?> </select> <input type="text" name="year" value="YYYY" size="4" /></p> <p>Favorite Color: <select name="color"> <option value="">Pick One</option> <option value="red">Red</option> <option value="yellow">Yellow</option> <option value="green">Green</option> <option value="blue">Blue</option> </select></p> <p><input type="submit" name="submit" value="Register" /></p> </form> </body> </html>