IS 448 - PHP 16.pdf
In a string indexed array called 'fruits', how can we access the first value in this array? In string indexed arrays, the index is also called (B), and the array content is also called (C)
$element = $fruits['a']; (B) key (C) value
Create a numerically indexed array called 'mixed' Create an array with string indexes called 'fruits' (B)
$mixed = array(23, 'xiv', "bob", 777); (B) $fruits = array("a" => "apple", "b" => "banana", "k" => "kiwi", "r" => "orange");
What are the two ways arrays can be indexed in PHP
(1) An array can have elements *indexed numerically.* - These are maintained in order (2) An array, even the same array, can have elements *indexed by a string.* - These are not maintained in any particular order
What are the Two ways of using *foreach* to iterate through an array
(1) Assigns each value in the array to the scalar_variable Syntax: foreach (array-name as scalar_variable) loop-body (2) The second version assigns each key to key-in-array variable and the associated value to value-in-array variable Syntax: foreach (array-name as key-in-array => value-in-array) loop-body
What are 2 bad ways to produce HTML in PHP Best PHP style is to (B)
(1) printing HTML code with print statements is ugly and error-prone: - must quote the HTML and escape special characters, e.g. \" (2) Don't print HTML; it's bad style! (B) use as few print/echo statements as possible in embedded PHP code
Users expect an (A) when they submit forms Embedded PHP allows you to (B) The user-entered values from forms can be accessed in PHP using (C)
(A) HTML response page (B) run some server-side code and also send back an HTML response page (C) the $_POST and $_GET arrays
*Functions for Dealing with Arrays* The *unset* function can be used to (A) The *array_keys* function (B) The *array_values* function (C)
(A) delete an array or an element of an array (B) returns an array of the keys of an array (C) returns an array of values in an array
*Static* The usual lifetime of a local variable is (A) In other words, a static variable exists only (B)
(A) from the time the function begins to execute to the time the function returns (B) in a local function scope, but it does not lose its value when program execution leaves this scope.
A variable defined in a function is, by default, (A). This is Called a (B) A variable defined outside the function is called (C) *A global variable with the same name as a local variable is (D)*
(A) local to the function. (B) local variable. (C) a global variable (D) not visible in the function
*Functions for Dealing with Arrays* The *array_key_exists* function (A) The *is_array* function (B) The *sizeof* function (C) *implode* converts (D) *explode* converts (E)
(A) returns true if a given key is actually present in a given array Syntax: array_key_exists(keyName, $arrayName); (B) determines if its argument is an array (C) takes an array as argument and returns length of array (D) an array of strings to a single string, separating the parts with a specified string Syntax: $str = implode(" ", $arrayName) (E) a string in to an array of strings by separating the string at specified characters
What are Two keywords that matter to scope of variables? Explain each
*Global:* Declaring a variable in a function with the global declaration means that the functions uses the global variable of that name *Static:* Declaring a variable with the static keyword means that the lifetime is from the first use of the variable to the end of the execution of the entire PHP script
Explain Arrays in PHP
- Arrays in PHP combine the characteristics of regular arrays and hashes - The elements of an array are, conceptually, key/value pairs
What are two ways we can create arrays?
- By assigning a value to an element of an array - By using the *array* function
What are Formal parameters? What are Actual parameters?
Name given to parameters that are in function definition (B) Name given to parameters that are in the function call
What is the syntax for a function in PHP Explain functions (B)
function name(parameters) { ... //statements } (B) - The parameters are optional, but not the parentheses - Function names are not case sensitive - A *return* statement causes the function to immediately terminate and return a value, if any value is provided in the return - *A function that reaches the end of the body without executing a return, returns no value*
Array elements are accessed by using an An *array's content* can be assigned to a list of (B), by using (C) [Slide 7]
index subscript in square brackets (B) variables (C) the list function
*Formal and Actual Parameters* A formal parameter, specified in a function declaration, is If more actual parameters are supplied in a function-call than there are formal parameters, what happens? (B) If more formal parameters are specified than there are actual parameters in a call then (C) PHP defaults (D)
simply a variable name (i.e., no need to specify the data type of the variable (B) the extra values are ignored (C) the extra formal parameters receive no value (D) to pass by value
The *sort* function sorts The function *asort* sorts (B) The function *ksort* is similar to (C) The functions *rsort, arsort*, and *krsort* are (D)
the values in an array and makes a numerically subscripted array from the sorted list (B) the values in an array but keeps the original key/value association (C) *asort* but sorts by keys (D) similar but sort in reverse order