-
What is the output?
$arr = array('ryan','bob', 'david');
foreach($arr as $v){
if($v == 'bob')
break;
}
print current($arr);
//david
after each array is read, the internal pointer is advanced
-
What is the output?
$arr = array('ryan','bob', 'david');
foreach($arr as $v){
break;
}
- //bob
- after each array is read, the internal pointer is advanced
-
What is the ouput of the following?
foreach (array(1, 2, 3, 4) as &$value) {
$value = $value * 2;
}
- This is not possible. Only variables can be referenced. Since the array is not a variable, it cannot be referenced (i.e. is
- variable), that means the following code won't work
-
Rewrite the following using a while loop:
$arr = array("one", "two", "three");
foreach ($arr as $value) {
echo "Value: $value<br />\n";
}
- $arr = array("one", "two", "three");
- reset($arr);
- while (list(, $value) = each($arr)) {
- echo "Value: $value<br />\n";
- }
-
Rewrite the following using a while loop:
$arr = array("one", "two", "three");
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
- $arr = array("one", "two", "three");
- reset($arr);
- while (list($key, $value) = each($arr)) {
- echo "Key: $key; Value: $value<br />\n";
- }
-
What is the output of the following:
$arr = array(1, 2, 3, 4);
foreach (&$arr as &$value) {
$value *= 4;
}
var_dump($arr);
- Parse error: syntax error, unexpected '&'
- &$arr is NOT legal
- However the &$value portion is legal
-
What is the output of the following:
$arr = array(2=>1, 3=>2, 4=>3, 5=>4);
foreach ($arr as &$key => &$value) {
$value *= 4; $key *= 3;
}
var_dump($arr);
- Fatal error: Key element cannot be a reference
- &$key is illegal
-
What is the first thing foreach does before working with an array?
- It resets the array.
- same as reset($arr)
-
What is the output of the following:
reset($arr);
while(list($key,&$value) = each($arr)){
print $key.'=>'.$value.'<br/>';
}
- It would produce an error
- Parse error: syntax error, unexpected '&', expecting ',' or ')'
- &$value is illegal
-
What does break and continue do?
- break ends execution of the current for, foreach, while, do-while or switch structure. break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.
- continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
- if you use "break" with no numerical argument, that's the same as doing "break 1" or "break 0"
- if you use "continue" with no numerical argument, that's the same as doing "continue 1" or "continue 0"
- Note
: in PHP the switch statement is considered a looping structure for the purposes of continue.
-
What is the output of the following:
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
print "$i\n";
}
-
What values must be set in the php.ini to allow
a include statement to include a http url?
allow_url_include
-
What values must be set in the php.ini to allow fopen
to open a http url?
allow_url_fopen
|
|