190 likes | 307 Views
CSC 3084: Web Development and Programming. Chapter 8: How to Code Control Statements. Control Statements. We have already seen examples of many control statements, so first we’ll very quickly review the basic syntax of the ones we have seen and look at some of the quirks of PHP
E N D
CSC 3084:Web Development and Programming Chapter 8: How to Code Control Statements
Control Statements • We have already seen examples of many control statements, so first we’ll very quickly review the basic syntax of the ones we have seen and look at some of the quirks of PHP • Remember that the following statements in PHP work fundamentally the same as in Java 7: if, if-else, while, for, do, switch, conditional operator ( ?: )
The Equality and Identity Operators • The equality operators are: ==, != and < > • You can use either != or <> to test for inequality • The identity operators are: === and !== • The equality operators perform type conversion (type coercion), whereas the identity operators do not • That is, the two operands are converted as necessary so that the two values are of the same type and can be compared • The relational operators are: <, >, <= and >=
Unusual Results with the Equality Operator • null == '' True; NULL is converted to the empty string. • null == false True; NULL is converted to FALSE. • null == 0 True; NULL is equal to any value that evaluates to FALSE. • false == '0' True; empty strings and “0” are converted to FALSE. • true == 'false' True; all other strings are converted to true. • 3.5 == "\t3.5 mi" True; the string is converted to a number first. • INF == 'INF' False; the string “INF” is converted to 0. • 0 == '' True; the empty string is converted to 0. • 0 == 'harris' True; any non-numeric string is converted to 0.
Relational Operators • Comparing strings to numbers with the relational operators: • 1 < '3' true • '10' < 3 false • Comparing strings with the relational operators: • 'apple' < 'orange' true • 'apple' < 'appletree' true • 'Orange' < 'apple' true • '@' < '$' false
Unusual Results with the Relational Operators • 0 <= 'test' True; the string "test" is converted to 0. • '' < 5 True; the empty string is converted to 0. • false < true True; FALSE is considered less than TRUE. • null < true True; NULL is converted to FALSE.
Logical Operators • &&, || and !, just as in Java/C/C++/etc. • Order of precedence for conditional expressions • Order Operators 1 ! 2 <, <=, >, >=, <> 3 ==, !=, ===, !== 4 && 5 || • Use braces to enclose your if-statements and other control statements and parentheses to change the order of operators in Boolean expressions
The Conditional Operator • (conditional_expression) ? value_if_true : value_if_false • Most often used in assignment statements • Also available in Java/C/C++/etc. • Examples: • $message = ($age >= 18) ? 'Can vote' : 'Cannot vote'; • $value = ($value >= $max_value) ? 1 : $value + 1; • return ($number > $highest) ? $highest : $number; • Bound a value in a given range: • $value = ($value > $max) ? $max : (($value < $min) ? $min : $value);
Example Switch Statement for a Controller if (isset($_POST['action'])) { $action = $_POST['action']; } else if (isset($_GET['action'])) { $action = $_GET['action']; } else { $action = 'list_products'; }
Example Switch Statement for a Controller (cont.) switch ($action) { case 'list_products': // Get the current category ID $category_id = $_GET['category_id']; if (!isset($category_id)) { $category_id = 1; } // Get product and category data $category_name = get_category_name($category_id); $categories = get_categories(); $products = get_products_by_category($category_id); // Display the product list include('product_list.php'); break;
Example Switch Statement for a Controller (cont.) case 'delete_product': // Get the IDs $product_id = $_POST['product_id']; $category_id = $_POST['category_id']; // Delete the product delete_product($product_id); // Display the Product List for the current category header("Location: .?category_id=$category_id"); break;
Example Switch Statement for a Controller (cont.) case 'delete_product': // Get the IDs $product_id = $_POST['product_id']; $category_id = $_POST['category_id']; // Delete the product delete_product($product_id); // Display the Product List for the current category header("Location: .?category_id=$category_id"); break; etc. • Remember to use break statements as needed • You should also include a default case at the end
Example While-Loop $total = 0; $count = 0; while ($count < 100) { $number = mt_rand(0, 100); $total += $number; $count++; } $average = $total / $count; echo 'The average is: ' . $average;
Example Do-Loop $max = -INF; $min = INF; $count = 0; do { $number = mt_rand(0, 100); $max = max($max, $number); $min = min($min, $number); $count++; } while ($count < 10); echo 'Max: ' . $max . ' Min: ' . $min;
Example For-Loop $number = 18; for ($i = 1; $i < $number; $i++) { if ($number % $i == 0) { echo $i . ' is a factor of ' . $number . '<br />'; } }
Example For-Loop • This loop algorithmically generates a drop-down list of interest rates • It uses the “alternate” for-loop syntax <label>Interest Rate:</label> <select name="rate"> <?php for ($v = 5; $v <= 12; $v++) : ?> <option value="<?php echo $v; ?>" <?php echo $s; ?>> <?php echo $v; ?> </option> <?phpendfor; ?> </select><br />
Break and Continue Statements • The break and continue statements let you exit a loop early or end a loop iteration early, respectively • Use these sparingly! Instead of break statements, try to craft your loop condition so that it terminates when you want • Instead of continue statements, use an if-statement to skip the rest of a loop iteration • Break and continue statements can be used in any kind of loop
Break Statement Example while (true) { $number = mt_rand(1,10); if ($number % 2 == 0) { break; } } echo $number; // $number is between 1 and 10 // and even
Continue Statement Example $number = 1; while ($number <= 10) { if ($number % 3 == 0) { $number++; continue; } echo $number . '<br />'; $number++; } // Only displays 1, 2, 4, 5, 7, 8, and 10