🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

CP476 Internet Computing-week-2-2.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

CP476 Internet Computing Week 2 – 2 PHP – control structure and functions Shaun Gao, Ph.D., P.Eng. Agenda Control structures Conditional Statements Switch Statements While- and Do While-loops For-loops Functions User defined functions Output control functions PHP Control Structures In PHP we have th...

CP476 Internet Computing Week 2 – 2 PHP – control structure and functions Shaun Gao, Ph.D., P.Eng. Agenda Control structures Conditional Statements Switch Statements While- and Do While-loops For-loops Functions User defined functions Output control functions PHP Control Structures In PHP we have the following conditional statements: if statement if...else statement if...elseif...else statement switch statement Syntax of if statement: if (condition) { code to be executed if condition is true; } PHP Control Structures –cont. Syntax of if…else statement – inherited from C if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } Example: $t = date("H"); #https://www.php.net/manual/en/function.date.php if ($t < "20") { echo "Have a good day!"; } else { echo "Have a good night!"; } Demo 1: PHP Control Structures –cont. Syntax of if...elseif...else statement: if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if first condition is false and this condition is true; } else { code to be executed if all conditions are false; } Demo 2: PHP Control Structures PHP also support conditional expressions – inherited from C The syntax of conditional expressions in PHP. condition ? if_true_expr : if_false_expr If the condition is true, then the if_true_expr will be evaluated. Otherwise, if_false_expr will be evaluated. Example: $v = 1; $r = (1 == $v) ? 'Yes' : 'No'; // $r is set to 'Yes' $r = (3 == $v) ? 'Yes' : 'No'; // $r is set to 'No' echo (1 == $v) ? 'Yes' : 'No'; // 'Yes' will be printed Demo 3: PHP Control Structures - switch Syntax of switch statements – inherited from C switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break;... default: code to be executed if n is different from all labels; } Demo 4: PHP – loop PHP has the following loop types: while - loops through a block of code as long as the specified condition is true do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true for - loops through a block of code for a specified number of times foreach - loops through a block of code for each element in an array PHP – loop – cont. PHP offers while-loops and do while-loops. – inherited from C While loop syntax: while (condition is true) { code to be executed; } Example: $x = 1; while($x

Use Quizgecko on...
Browser
Browser