CP476 Internet Computing-week-2-1.pdf
Document Details
Uploaded by ZippyPelican
null
Tags
Related
- CP476 Internet Computing-week-1-2.pdf
- CP476 Internet Computing Week 1-3 Tutorial PDF
- Week 2.pdf
- Week 3.pdf
- Unit 1. Web programming platforms in server environment. Characteristics of the PHP language V8.pdf
- Unit 1. Web programming platforms in server environment. Characteristics of the PHP language V8.pdf
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. ……