Download presentation
Presentation is loading. Please wait.
Published byAron Maxwell Modified over 9 years ago
1
Control Structures
2
The if Conditional Condition must go within parentheses Statements come after the curly brace Example: if (empty ($name)) { print ‘ please enter a name. ’ }
3
Form
4
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"> Registration <?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 ' Registration Results: '; // Validate the first name. if (empty ($first_name)) {
5
print ' Please enter your first name. '; } // Validate the last name. if (empty ($last_name)) { print ' Please enter your last name. '; } // Validate the email address. if (empty ($email)) { print ' Please enter your email address. '; } // Validate the password. if (empty ($password)) { print ' Please enter your password. '; } ?>
6
Using else 6.3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Registration <?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 ' Registration Results: '; // Validate the first name. if (empty ($first_name)) { print ' Please enter your first name. '; }
7
// Validate the last name. if (empty ($last_name)) { print ' Please enter your last name. '; } // Validate the email address. if (empty ($email)) { print ' Please enter your email address. '; } // Validate the password. if (empty ($password)) { print ' Please enter your password. '; } // Validate the month. if (is_numeric ($month)) { $birthdate = $month. '-'; } else { print ' Please select the month you were born. '; } // Validate the day. if (is_numeric ($day)) { $birthdate.= $day. '-'; } else { print ' Please select the day you were born. '; } // Validate the year. if (is_numeric ($year)) { $birthdate.= $year; } else { print ' Please enter the year you were born as four digits. '; } print "You entered your birthday as $birthdate"; ?>
8
PHP Operators OperatorUsageType ==EqualityComparison !=InequalityComparison AND/&&Andlogical OR/l lOrlogical.Concatenationstring.=Concatenates to the values of a variable Concatenation and assignment
9
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"> Registration <?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 ' Registration Results: '; // Validate the first name. if (empty ($first_name)) { print ' Please enter your first name. '; }
10
// Validate the last name. if (empty ($last_name)) { print ' Please enter your last name. '; } // Validate the email address. if (empty ($email)) { print ' Please enter your email address. '; } // Validate the password. if (empty ($password)) { print ' Please enter your password. '; } // Check the two passwords for equality. if ( $password != $confirm ) { print ' Your confirmed password does not match the original password. '; } // Validate the month. if (is_numeric ($month)) { $birthdate = $month. '-'; } else { print ' Please select the month you were born. '; } // Validate the day. if (is_numeric ($day)) { $birthdate.= $day. '-'; } else { print ' Please select the day you were born. '; } // Validate the year. if (is_numeric ($year)) { $birthdate.= $year; } else { print ' Please enter the year you were born as four digits. '; } // Check that they were born before 2004. if ($year >= 2004) { print ' Either you entered your birth year wrong or you come from the future! '; } print "You entered your birthday as $birthdate"; ?>
11
Using elseif 6.6 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Registration <?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 ' Registration Results: '; // Validate the first name. if (empty ($first_name)) { print ' Please enter your first name. '; } // Validate the last name. if (empty ($last_name)) { print ' Please enter your last name. '; } // Validate the email address. if (empty ($email)) { print ' Please enter your email address. '; }
12
// Validate the password. if (empty ($password)) { print ' Please enter your password. '; } // Check the two passwords for equality. if ($password != $confirm) { print ' Your confirmed password does not match the original password. '; } // Validate the month. if (is_numeric ($month)) { $birthdate = $month. '-'; } else { print ' Please select the month you were born. '; } // Validate the day. if (is_numeric ($day)) { $birthdate.= $day. '-'; } else { print ' Please select the day you were born. '; } // Validate the year. if ( is_numeric ($year) AND strlen ($year) == 4 ) { // Check that they were born before 2004. if ($year >= 2004) { print ' Either you entered your birth year wrong or you come from the future! '; } else { $birthdate.= $year; } // End of 2nd conditional. } else { // Else for 1st conditional. print ' Please enter the year you were born as four digits. '; } // End of 1st conditional. // Validate the color. if (!$color) { print ' Please select your favorite color. '; }
13
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';
14
} 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 " You are a(n) $sign. "; ?>
15
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; }
16
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"> Registration <?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 ' Registration Results: '; // Validate the first name. if (empty ($first_name)) { print ' Please enter your first name. '; } // Validate the last name. if (empty ($last_name)) { print ' Please enter your last name. '; } // Validate the email address. if (empty ($email)) { print ' Please enter your email address. '; }
17
// Validate the password. if (empty ($password)) { print ' Please enter your password. '; } // Check the two passwords for equality. if ($password != $confirm) { print ' Your confirmed password does not match the original password. '; } // Validate the month. if (is_numeric ($month)) { $birthdate = $month. '-'; } else { print ' Please select the month you were born. '; } // Validate the day. if (is_numeric ($day)) { $birthdate.= $day. '-'; } else { print ' Please select the day you were born. '; } // Validate the year. if ( is_numeric ($year) AND strlen ($year) == 4 ) { // Check that they were born before 2004. if ($year >= 2004) { print ' Either you entered your birth year wrong or you come from the future! '; } else { $birthdate.= $year; } // End of 2nd conditional. } else { // Else for 1st conditional. print ' Please enter the year you were born as four digits. '; } // End of 1st conditional. // Validate the color. if (!$color) {
18
print ' Please select your favorite color. '; } 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';
19
} 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 " You are a(n) $sign. "; ?>
20
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
21
Code Example 6.8 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Register Please complete this form to register: First Name: Last Name: Email Address: Password: Confirm Password: Date Of Birth: Month January February March April May June July August September October November December Day <?php
22
// Print out 31 days. for ($d = 1; $d <= 31; $d++) { print " $d \n"; } ?> Favorite Color: Pick One Red Yellow Green Blue
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.