$a 5 $B 3 $B &$a $B $a $B They Are Both 25
$a 5 $B 3 $B &$a $B $a $B They Are Both 25
Name: _________________________
Coding questions are at the end. Function prototypes are on the last page. The number of marks allocated per
question appears in parentheses: total = 40.
1.
2.
=
=
=
=
5;
3;
&$a;
$a * $b;
3.
4.
5.
(1) Compare when a for loop should be used vs. when a while loop should be used.
A for loop is used when you know the starting value and the number of iterations.
A while loop is used when the terminating condition is computed inside of the loop.
6.
(1) When inside of a loop, what is the difference between using break and continue?
break exits the loop and execution continues on the line following the loop
continue immediately starts the next iteration of the loop (but the $i++ in a for loop is executed!)
7.
8.
(1) Describe one distinct advantage and one distinct disadvantage of using associative arrays vs. using
indexed arrays.
associative arrays can be more intuitive since elements are accessed by name, e.g., $_POST[address]
associative arrays require more overhead to access than indexed arrays
Name: _________________________
17. (1) Is it a good idea for a subclass to declare an attribute having the same name as an attribute inside of the
class that was inherited? Why or why not?
No. It breaks the use of the attribute in the parent class and is inappropriate for object-oriented
programming
Name: _________________________
20. (2) Write an appropriate short example of a function that uses pass-by-reference.
function increment (&$a) {
++$a;
}
21. (2) Write a function product (x, y) that returns the product of two numbers. If either argument is not a valid
number, return false.
function product ($x, $y) {
if (!is_numeric($x) || !is_numeric($y) return false;
return $x * $y;
}
22. (4) Write the PHP code to open the file inventory.txt (containing 3 tab-separated items per line), read the
input line-by-line, and display the entire contents of this file on the web page. You do not need to write the
error-checking code for opening the file. The output format is unimportant.
@ $fp = fopen ("inventory.txt", "r");
// Normally, check $fp for NULL
while (count($item = fgetcsv($fp, 200, "\t")) == 3) {
echo "$item[0] $item[1] $item[2]<br>\n";
}
current( $array_name )
each( $array_name )
reset( $array_name )
end( $array_name )
next( $array_name )
prev( $array_name )
int array_walk(array arr, string func, [mixed userdata])
int array_count_values($array)
extract(array arry [, int extract_type] [, string prefix] )
bool mail(string to, string subject, string message,
string [additional_headers
[, string additional_parameters]]);
chop(string s)
ltrim(string s)
trim(string s)
nl2br(string s)
string sprintf (string format [, mixed args...])
void printf (string format [, mixed args...])
strtoupper(string s)
strtolower(string s)
ucfirst(string s)
ucwords(string s)
string AddSlashes(string s)
string StripSlashes(string s)
string implode(string delim, string, s)
string strtok(array a, string delim);
string substr(string string, int start[, int length] );
int strcmp(string str1, string str2);
int strcasecmp(string str1, string str2);
int strnatcmp(string str1, string str2);
int strnatcasecmp(string str1, string str2);
int strlen(string s);
string strstr(string haystack, string needle);
string stristr(string haystack, string needle);
string strchr(string haystack, string needle);
int strpos(string haystack, string needle, int [offset] );
int strrpos(string haystack, string needle, int [offset] );
mixed str_replace(mixed needle, mixed new_needle,
mixed haystack);
string substr_replace(string string, string replacement,
int start, int [length] )
int ereg(string pattern, string search, array [matches]);
int eregi(string pattern, string search, array [matches]);
string ereg_replace(string pattern, string replacement,
string search);
string eregi_replace(string pattern, string replacement,
string search);
array split(string pattern, string search, int [max]);
require(string filename);
include(string filename);