WEEK 8 AWS

Report 0 Downloads 20 Views
WEEK 8 ARRAY -

-

-

-

Data type made up of elements storing values. Has length, reflecting umber of elements in it. Array starts at index 0. Syntax: $array_name = array ([value1[, value2, … ]]) Syntax for referring to an element an array. $array_name[index]; o $names = array (‘Ted’, ‘Sue’, ‘Ray’); o $names = array(); //create an empty array. o $names[0] = ‘Ted’; // set Ted to the first value to the array o $names[1]= ‘Sue’; // set Sue to the second value to the array o $names[2]=’Ray’; //set Ray to the third value to the array print_r ($names); //view array’s indexes and values. PHP array are dynamic: not fixed in size, but size can be modified. o If remove elements from arrays; this leaves gaps in array represented by NULL value. o Use unset($var1[, $var2…]) to remove elements. o Use array_values($array) to remove NULL values after unset() has been applied. Functions for loops that works with array: o Count($array) – returns the number of elements but not gaps in an array. o End($array) – moves the cursor for the array to the last element in the array. o Key($array) – return the index of the array element that the cursor is on. o Isset($var) – returns a TRUE value if the specified variable or array element contains a value. Associative array: using strings as index values of array. Keys: string index of associative array. Syntax: o Array ([key1 => value1, key2 => value2, …]) To iterate thru the array, needs a variable to store value for each element. Syntax for Associative array foreach loop: o Foreach ($array_name as [ $key => ] $value) { o // statements that use $key and $value

QUEUE AND STACKS -

-

Stack o o

Array implementing last-in, first-out (LIFO) set of values. Can remove/add from either side of the array but must stick to one side: add to the last, remove the last.

Queue o Array implementing first-in, first-out (FIFO) set of values. o Always add to one side and remove from the other.

WEEK 9 FUNCTIONS -

Argument list: set of arguments passed in (matching parameters in parameter list). Syntax: o function function_name ([$param1, $param2, $paramn, …]) { o //code for function o [return[value];] o } o Return only happens when you want to return the function’s value when it is called = no arguments needed.

PASS ARGUMENTS -

Passing (argument) by value: copy of argument value, not original argument provided to function. o Value of original argument unchanged. Passing by Reference: pointer to argument provided to function. o Value of original argument can be changed. o use & infront of parameter.

VARIABLE SCOPE -

-

Scope: how visible are variables and functions in a script (the extent to which they are visible). Global variable: one that is available throughout the script. o Declared outside function. o To use the global variable, syntax: global ; Local variable: available within function. Syntax for specifying parameter with default value: = <default value> Scalar value: a single value – string literal, numeric literal, Boolean value or NULL.