Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 116 Decisions & Validation. Overview Constants Boolean conditions Decision logic Form validation 2.

Similar presentations


Presentation on theme: "CSCI 116 Decisions & Validation. Overview Constants Boolean conditions Decision logic Form validation 2."— Presentation transcript:

1 CSCI 116 Decisions & Validation

2 Overview Constants Boolean conditions Decision logic Form validation 2

3 Defining Constants A constant contains information that does not change during program execution Constant names – Do not begin with a dollar sign – Do use all uppercase letters Use the define() function to create a constant define("CONSTANT_NAME", value); Examples – define("PI", 3.141592); – define("STATE_ABBR", "WA"); 3

4 Using Constants define("PI", 3.141592); $radius = 5.0; $circumference = 2 * PI * $radius; echo "PI is ". PI. " "; echo "The circumference is $circumference"; 4 Note: Unlike variables, you cannot include a constant name within the quotations that surround a text string.

5 Boolean Variables A Boolean variable is either true or false $is_registered = TRUE; print " Registered: $is_registered "; Often used in decision making if($is_registered)… 5 TRUE prints as 1 FALSE prints as 0

6 Boolean Conditions Boolean conditions evaluate to true or false Use relational and logical operators Used in decision logic 6 if($isSunny AND $dayOfWeek >= 6) { print "Off to the beach!"; }

7 Relational Operators 7

8 2 > 3 3 <= 2 2 == 3 - 1 2 * 3 < 2 + 3 8 2 != 3 3 == "3" 3 === "3" 3 - 2 >= 2-- Evaluate the following boolean conditions:

9 Logical Operators Logical operators are used for comparing two boolean expressions Return TRUE or FALSE 9

10 Operator Precedence Pre-increment/decrement Arithmetic operators (*, /, %, +, -) Relational operators (<, <=, ==, !=) Logical operators (AND, OR) – AND has precedence over OR Assignment operators (=, +=, etc.) Post-increment/decrement 10

11 Logical Operators 2 5 2 + 1 >= 3 AND 3 > 2 2 3 3 * 2 > 6 OR 2 != 2 OR 5 < 6 3 - 2 != 1 AND 2 * 3 >= 3+ 2 - 5 3 == 2 + 1 OR 3 % 2 <= 2 - 1 11 Evaluate the following boolean conditions:

12 Practice Evaluate the following boolean conditions given that $x=1, $y=2, and $z=3 $x <= $y $x == $y OR $z > $y $y != $z AND $y >= $x $y + $x == $z $x - $y * $z < $z / $y $z % $y != $x && $y >= $z || $x <= $z $x $x * $y 12

13 if Statements Used to execute one or more statements if a boolean condition is true if(boolean condition) { statements; } If the boolean condition is false, the statements do not execute 13

14 if Statements $age = 21; $day = “Monday”; if($age == 21) { print “Let’s party!”; print “It’s dancing time”; } print “Smile”; 14

15 if Statements $age = 21; $day = “Monday”; if($age == 21 && $day == “Friday” || $day == “Saturday”) { print “Let’s party!”; } print “Smile”; 15

16 if Statements $age = 21; $day = “Monday”; if($age == 21 && ($day == “Friday” || $day == “Saturday”)) { print “Let’s party!”; } print “Smile”; 16

17 if...else Statements An else clause executes when the condition in an if...else statement evaluates to false Syntax: if (conditional expression) { statement(s); } else { statement(s); } 17

18 if...else Statements $day = “Monday”; if($day == “Saturday”) { print “Let’s party!”; } else { print “Good night!”; } 18 Curly braces are not required when there is only one statement after the if or else, however, it’s still a good idea to use them.

19 Nested if Statements One decision-making statement may be contained within another 19 if($day == “Friday” || $day == “Saturday”) { if($age >= 21) { print “Let’s party!”; } Notetheindentation

20 switch Statements Executes a specific set of statements depending on the value of an expression Compares the value of an expression to a value in a case label – case label can be followed by one or more statements – Each set of statements is usually followed by the keyword break – The default label contains statements that execute when the value of the expression does not match any other case label 20

21 switch Statements Syntax switch (expression) { case label: statement(s); break; case label: statement(s); break;... default: statement(s); } Example switch ($day) { case 1: print “Monday”; break; case 2: print “Tuesday”; break;... default: print “Invalid”; } 21

22 Decision Practice Write a statement that prints “even” if a variable num is even. (Hint: Use the modulus operator.) Write a statement that prints “even” if a variable num is even, and “odd” otherwise. Write a statement that prints “A” for a grade of 90-100, “B” for 80-89, “C” for 70-79, “D” for 60-69 and “F” otherwise. Write a switch statement that takes a number and prints the corresponding month, e.g. “January” for 1. 22

23 Comparing Strings $cartoon1 = "Minnie Mouse"; $cartoon2 = "Mickey Mouse"; if ($cartoon1 == $cartoon2) echo " Same cartoon. "; else echo " Different cartoons. "; 23

24 Comparing Strings (continued) $cartoon1 = "Pluto"; $cartoon2 = "pluto"; if ($cartoon1 < $cartoon2) echo "$cartoon1 comes before $cartoon2"; else echo "$cartoon2 comes before $cartoon1"; 24

25 ASCII Numeric representations of English characters ASCII values range from 0 to 256 Lowercase letters are represented by the values 97 (“a”) to 122 (“z”) Uppercase letters are represented by the values 65 (“A”) to 90 (“Z”) Lowercase letters have higher values than uppercase letters, so they are evaluated as being “greater” than uppercase letters 25

26 String Comparison Functions The strcasecmp() function performs a case-insensitive comparison of strings – strcasecmp(“DAISY", “daisy")  true The strcmp() function performs a case-sensitive comparison of strings – strcmp(“DAISY", “daisy")  false 26

27 Conditional Operator The conditional operator executes one of two expressions, based on the results of a conditional expression Syntax: conditional expr ? expr1 : expr2; 27 evaluates if conditional expression is true evaluates if conditional expression is false

28 Conditional Operator $grade = 2.5; ($grade >= 2.0) ? $result = "You pass!" : $result = "You fail."; echo $result; 28

29 Handling Form Submissions Form data is submitted in name=value pairs GET method passes form data by appending a question mark (?) and name=value pairs to the URL 29

30 Handling Form Submissions A user can bypass JavaScript form validation on the client – If get is used, they can type in a URL – If post is used, they can construct a transmission using HTTP headers Always use PHP code to validate submitted data 30

31 Validating Submitted Data Use isset() or empty() functions to ensure that a variable contains a value – if (isset($_GET[‘amount’])) – if (!empty($_GET[‘amount’])) Use is_numeric() to test whether a variable contains a numeric string – if (is_numeric($_GET[‘amount’])) 31

32 isset() and empty() isset($var)empty($var) $var = 0;true $var = ‘hello’;truefalse $var = null;falsetrue $var = ‘’;true falsetrue 32 used to validate text fields; do not use if 0 (zero) is valid used to validate text fields; do not use if 0 (zero) is valid used to validate check boxes, radio buttons, and select lists used to validate check boxes, radio buttons, and select lists

33 Data Validation if(is_numeric($_POST[‘year’]) AND strlen($_POST[‘year’] == 4) AND $_POST[‘year’] >= 2010) { $year = $_POST[‘year’] ; } else { $valid = false; print “ Invalid year. ”; } 33

34 34 Enter your credit card number: <?php if(isset($_POST["ccnumber"])) { $num = $_POST["ccnumber"]; $num = str_replace("-", "", $num); $num = str_replace(" ", "", $num); if(is_numeric($num)) echo "Your credit card number is $num."; else echo "Your credit card number is not valid."; } ?> create the form check to see if a value was entered strip out dashes and spaces check to see if what’s left is numeric


Download ppt "CSCI 116 Decisions & Validation. Overview Constants Boolean conditions Decision logic Form validation 2."

Similar presentations


Ads by Google