Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.

Similar presentations


Presentation on theme: "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1."— Presentation transcript:

1 Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, frmcclurg@gmail.com © Copyright 2010, All Rights Reserved 1

2 Chapter Four Decisions, Decisions http://webcert.kirkwood.edu/~fmcclurg/co urses/php/slides/chapter04a.conditions.ppt 2

3 “What is truth?” -- Pontius Pilate AD 30 PHP Logicals 3

4 Description: Comparison operators determine the relationship between two operands and return either a TRUE or FALSE Boolean value. Comparison Operators OperatorDescription == Equal To != Not Equal To < Less Than > Greater Than <= Less Than or Equal To >= Greater Than or Equal To 4

5 Description: Relational operators determine equality between two operands and return either a TRUE or FALSE Boolean value. Relational Operators Example: 0; // FALSE 1; // TRUE -1; // TRUE $a = 0; // FALSE (assignment) $a = 1; // TRUE (assignment) 1 == 1; // TRUE (equality) "1" == 1; // TRUE (equality) "1" === 1; // FALSE (identity) '1' != 'A'; // TRUE (inequality) 1 <> 2; // TRUE (same as "!=") Relational Operators 5

6 Description: Logical operators work with relational operators to form more complex expressions. Logical Operators OperatorSymbolDescription AND&& True if both operands are true OR|| True if one or more operands are true XOR n/aTrue if only one operand is true n/a ! True if false. False if true. 6

7 AND TRUEFALSE TRUE FALSE Definition: True if both operands are true. Truth Tables (Logical AND) T F F F 7 TRUE TRUE FALSE FALSE

8 OR TRUEFALSE TRUE FALSE Definition: True if at least one operand is true. Truth Tables (Logical OR) T F T T 8 TRUE FALSE

9 XOR TRUEFALSE TRUE FALSE Definition: True if only one operand is true.. Truth Tables (Logical XOR) T F T F 9 TRUE FALSE

10 ! Result TRUE FALSE Definition: Negation. True if false. False if true. Truth Tables (Logical NOT) T F 10 TRUE FALSE !

11 Description: The curly braces “{}” define the boundaries of a code block that contain zero or more statements. The braces also allow you to group code so that it corresponds with additional statements (e.g. if, else, etc.) Syntax: { // begin code block statement;... } // end code block “Good Grief! You block of code.” 11

12 Description: The “if” statement evaluates a condition to determine true or false. The “if” Flow Chart FALSE TRUE block of code When a condition is false, the block of code (or statement) skipped. When a condition is true, the block of code (or statement) executed. 12 Begin Execution Continue Execution if condition

13 Description: Execute a block of code if the condition is true. Syntax: if ( condition ) { statement; } The “if” Conditional Statement 13

14 Rules: 1. The condition can be any expression (even an assignment). 2. The curly braces “ {} ” are optional if there is only one statement. However, best practices or style guidelines may mandate the use of braces in all circumstances. The “if” Rules 14

15 Trace the following code: <?php $x = 5; if ( $x == 5 ) // true echo "true! "; if ( $x ) // true echo "true! "; if ( TRUE ) // true echo "true! "; // is number between 2 and 7? if ( ($x > 2) && ($x < 7) ) // true echo "true!"; ?> Example “if” Statements 15

16 Trace Output: <?php $x = 0; if ( $x == 0 ) // true echo "true!"; if ( $x ) // false echo "true!"; if ( ! $x ) // true echo "true!"; // is number outside range of -1 and 7? if ( ($x 7) ) // false echo "true!"; ?> More “if” Statement Examples 16

17 Trace Output: <?php $x = NULL; if ( $x ) // false echo "true!"; $str = ""; if ( $str ) // false echo "true!"; if ( TRUE || FALSE ) // true echo "true!"; if ( TRUE && FALSE ) // false echo "true!"; ?> Still More “if” Statement Examples 17

18 In order to speed up processing, PHP (and other languages) may “short circuit” some logical operations. This means that the second half of a statement may not get executed. Example: FALSE AND TRUE; // false FALSE AND FALSE; // false TRUE OR TRUE; // true TRUE OR FALSE; // true Short Circuit Operations 18

19 <?php if ( $x = 0 ) // assignment not equality! echo "Never reached "; if ( $x = 4 ) // assignment not equality! echo "Always reached "; // short-circuited "OR" statement if ( TRUE || ++$x ) // not incremented printf( "Always reached: %d ", $x ); // short-circuited "AND" statement if ( FALSE && ++$x ) // not incremented printf( "Never reached: %d ", $x ); printf( "\$x = %d ", $x ); ?> “if” Pitfalls to Avoid 19

20 Description: Avoid using bitwise operators in an “if” statement. However, when they are used, they function as a non short-circuited operators. Examples: <?php $a = 3; $b = 5; if ( TRUE | ++$a ) // not short-circuit { printf( "Always reached " ); printf( "\$a = %d ", $a ); // 4 } if ( FALSE & --$b ) // not short-circuit { printf( "Never reached " ); } printf( "\$b = %d ", $b ); // 4 ?> Bitwise operators (non short circuit-operators) 20

21 Logical Operator Examples: TRUE AND TRUE; // true TRUE AND FALSE; // false FALSE AND TRUE; // false FALSE AND FALSE; // false TRUE OR TRUE; // true TRUE OR FALSE; // true FALSE OR TRUE; // true FALSE OR FALSE; // false TRUE XOR TRUE; // false TRUE XOR FALSE; // true FALSE XOR TRUE; // true FALSE XOR FALSE; // false ! TRUE; // false ! FALSE; // true What is truth? (review) 21

22 When a condition is true, the “if code” (or statement) executed. When a condition is false, the “else code” (or statement) executed. Description: The “else” statement handles the “not if” condition. The “else” Flow Chart 22 if condition FALSETRUE “if” code “else” code else branch if branch Continue Execution Begin Execution

23 Description: Provides an alternate branch of execution when the “if” condition is false. Syntax: if ( condition ) statement1; else // if ( ! condition ) statement2; The “else” Statement Syntax 23

24 Example: <?php $isTrue = FALSE; if ( $isTrue ) // true printf("Nothing but the truth!"); else // false or fallback printf("That is a lie!"); ?> An “else” Statement Example 24

25 Description: Beware of the dangling “else”! An “else” is always associated with the nearest “if” regardless of paragraph indentation. Trace Output: <?php $day = 28; if ( $day <= 29 ) // true if ( $day == 29 ) // false echo "Today's a leap day "; else // note: this is dangling else! echo "$day greater than 29 "; ?> The “else” Pitfall 25

26 Description: A dangling “else” can be avoided by specifying brackets around the “if” that is associated with the “else”. Problem Resolved: <?php $day = 28; if ( $day <= 29 ) // true { if ( $day == 29 ) // false echo "Today's a leap day "; } else // dangling else fixed echo "$day greater than 29 "; ?> Note: To avoid the potential dangling “else” hazard, Best Practices or Style Guidelines may mandate the use of curly brackets regardless of the number of statements contained in the block of code. Avoiding the “else” Pitfall 26

27 Description: There are cases when you need to compare against a large number of alternatives. This may produce “marching” nested if/else code. <?php if ( $chr = 'a' ) $rot13 = 'n'; else if ( $chr = 'b' ) $rot13 = 'o'; else if ( $chr = 'c' ) $rot13 = 'p'; else if ( $chr = 'd' ) $rot13 = 'q'; else if ( $chr = 'e' ) $rot13 = 'r'; else if ( $chr = 'f' ) $rot13 = 's'; else... ?> Note: There are more elegant ways to code a ROT13 character lookup. Nested “if” Statements 27

28 Description: This statement provides a test for multiple conditions. It eliminates the need for a nested “if-else” statement. Syntax: if ( condition1 ) statement1; elseif ( condition2 ) // note: not spelled "elsif" statement2; elseif ( condition3 ) statement3;... else statementx; Note: The “elseif” statement may not have the same result as “else if” when using curly brackets. The “elseif” Syntax 28

29 Example: <?php $dollar = 0.50; if ( $dollar > 100000 ) echo "Category: Upper Class"; elseif ( $dollar > 50000 ) echo "Category: Middle Class"; elseif ( $dollar > 10000 ) echo "Category: Low Class"; else // fallback echo "Category: No Class"; ?> The “elseif” Example 29

30 Description: The ternary operator “?” and “:” is an abbreviated syntax for the “if” statement Syntax: condition ? trueExpr : falseExpr; Equivalent “if” Statement: if ( condition ) trueExpression; else falseExpression; Ternary (conditional) Operator 30

31 <?php $count = 4; $name = "cact"; // make singular/plural by appending $name.= ($count == 1) ? "us" : "i"; // make singular/plural by assignment $verb = ($count == 1) ? "is" : "are"; $plural = ($count != 1) ? "s" : ""; printf( "There %s %d %s plant%s.", $verb, $count, $name, $plural ); ?> Ternary Operator Example 31

32 Description: The “switch” provides an alternate construct for an if-elseif-else statement. Advantage: Fall-through capability. Disadvantage: Uses only constants. Can not use any conditional other than “==”. Conditionals “ ”, !=, etc. are not allowed. Pitfall: Don't forget the “break” statement! The “switch” Statement 32

33 <?php switch ( $variable ) { case constant1 : statement1; break; // optional case constant2 : statement2; break; // optional default : // optional statementx; } ?> The “switch” Syntax 33

34 <?php $year = 1960; $month = 'June'; printf( "%s %d has ", $month, $year ); switch ( $month ) { case 'September' : case 'April' : case 'June' : case 'November' : $len = 30; break; case 'February' : $len = isLeapYear($year) ? 29 : 28; break; default : $len = 31; } printf( "%d days", $len ); ?> A “switch” Example 34

35 to be continued... http://webcert.kirkwood.edu/~fmcclurg/c ourses/php/slides/chapter04b.looping.ppt


Download ppt "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1."

Similar presentations


Ads by Google