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

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

Full Transcript

CP476 Internet Computing Week 2 – 1 PHP –comparison, array. Shaun Gao, Ph.D., P.Eng. Agenda Comparisons Comparison operators Array operations Basics Foreach-loops Array Operators Variable scopes PHP - Comparison Operators – demo 1 PHP - Comparison Operators – demo 2 PHP - Comparison Operators – demo...

CP476 Internet Computing Week 2 – 1 PHP –comparison, array. Shaun Gao, Ph.D., P.Eng. Agenda Comparisons Comparison operators Array operations Basics Foreach-loops Array Operators Variable scopes PHP - Comparison Operators – demo 1 PHP - Comparison Operators – demo 2 PHP - Comparison Operators – demo 3 To compare strings `as strings' the strcmp function can be used (inherited from C language). PHP 7 introduced three-way comparisons () (after php version 7). Strcmp(“ABD”, “ABC”); → 1 Strcmp(‘aaa’, “aaa”); → 0 “F1” “G0” → -1 0.0 FALSE → 0 PHP - Comparison Operators – cont. PHP strcmp() Function  inherited from C Syntax: strcmp(string $string1, string $string2): int string1 string2 Required. Specifies the first string to compare Required. Specifies the second string to compare Return Values: < 0 if string1 is less than string2; > 0 if string1 is greater than string2; 0 if they are equal. PHP – Data Type verification APIs PHP provides three functions to test whether a value is or is not NAN, INF or –INF is_nan(value): bool (returns TRUE if value is NAN) is_infinite(value): bool (returns TRUE if value is INF or –INF) is_finite(value): bool (returns TRUE if value is neither NAN nor INF) PHP provides more functions for testing data types. is_int(mixed $value): bool is_float(mixed $value): bool is_null(mixed $value): bool … Demo 4-1 PHP - Arrays Indexed arrays - Arrays with a numeric index Associative arrays – an associative array is an abstract data type composed of a collection of (key, value) pairs. array(key1 => value1, key2 => value2, key3 => value3,... ) Another way to define an array in PHP array( … ) can be replaced with [ … ] Demo 4-2 – another way to define an array The value can be of any type, including arrays. array(key1 => array(k1=>v1, k2=>v2), key2 => value2, key3 => value3,... ) Demo 4-3 – value is an array PHP – Arrays – cont. The key is an integer or string Examples: $arr1 = [1 => " Peter ", 3 => 2009 , "a" => 101]; $arr2 = array (200846369 => array (" name " => " Jan Olsen ", " COMP518 " => 69, " COMP519 " => 52)); The size of an array can be determined using the count function: int count(Countable|array $value, int $mode = COUNT_NORMAL). mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array. print count ( $arr1 ); // prints 3 print count ( $arr2 ); // prints 1 print count ($arr2 ,1); // prints 4 PHP – Array – cont. It is possible to omit the keys when using the indexed array $array1 = array (" Peter ", " Paul ", " Mary "); The key values are 0, 1, … All the keys of an array can be retrieved using array_keys($array1) returns a natural number-indexed array containing the keys of $array1 All the values of an array can be retrieved using array_values($array1) returns a natural number-indexed array containing the values stored in $array1 Demo 4-array-keys Demo 5-array-values PHP – Array – cont. We know that an individual value can be accessed via its key. $arr1 = array (1 => " Peter ", 3 => 2009 , "a" => 101); print "'a' => ". $arr1 ["a"]."\n"; The function array_key_exists(key, $array_name) can be used to check whether there is a value that associated with a key in an array. array_key_exists (string|int $key, $array_name): bool Examples array_key_exists("a",$arr1 ) # returns TRUE array_key_exists ("c",$arr1 ) # returns FALSE Demo 6 PHP - Multidimensional arrays Two-dimensional Arrays $students = array ( array(“Jessica", A, B, B), array(“Amber", C, A, B), array(“Lauren", B, C, A), array(“Geoff", C, A, B), array(“Caleb”, B, B, B) ); PHP - Arrays: foreach-loop The foreach loop works only on arrays and is used to loop through each key/value pair in an array. Syntax: foreach ($array_name as $key=>$value) // $key=> is optional { echo “$value \n”; // process $value } PHP - Arrays: foreach-loop Example 1: $colors = array("red", "green", "blue", "yellow"); foreach ($colors as $value) { echo "$value \n"; } Example 2: $array1= array (" Peter ", " Paul ", " Mary "); foreach ($array1 as $key => $value ) print "The array maps $key to $value \n"; what are the outputs? Demo 7-1, 7-2 PHP - Array Operators – Stack: FILO array_push() — Push one or more elements onto the end of array. array_push($array_name, value1, value2,...) : int appends one or more elements at the end of the end of an array variable; returns the number of elements in the resulting array. Example: $stack = array("orange", "banana"); array_push($stack, "apple", "raspberry"); print_r($stack); (print_r(): prints the information about a variable) The output: PHP - Array Operators – Stack: FILO array_pop($array_name); extracts the last element from an array and returns it. If array is empty (or is not an array), null will be returned. Example: The output: Demo 8-1 push-pop. PHP - Array Operators array_shift() — Shift an element off the beginning of array. array_shift($array_name) : mixed Parameter is the array Return values: the shifted value, or null if array is empty or is not an array. Example: $stack = array("orange", "banana", "apple", "raspberry"); $fruit = array_shift($stack); print_r($stack); The output: PHP - Array Operators array_unshift() prepends passed elements to the front of the array. array_unshift($array_name, value1, value2,...) : int Parameters: Array_name: The input array; value1, value2…: The values to prepend. Return values: Returns the new number of elements Example: $queue = array("orange", "banana"); array_unshift($queue, "apple", "raspberry"); print_r($queue); The output. Demo-9-shift. Variable scopes in PHP Variable scope The scope of a variable is the context in which it is defined and used. Most PHP variables only have a single scope. Summary Comparisons → PHP is not a rigorous language, but it is widely used Identical vs. equal Arrays Basics Foreach-loops Array Operators Variable scopes Question? How to access variables that are used in other PHP files? Announcement Please find a group as soon as possible Please let me know if you need help The benefit of becoming a group Learn from each other Succeed in group project Improve communication skills. …… 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