Control structure question

Card Set Information

Author:
starkisspk
ID:
11210
Filename:
Control structure question
Updated:
2010-03-31 03:38:04
Tags:
Control Structure
Folders:

Description:
Control Structure Questions
Show Answers:

Home > Flashcards > Print Preview

The flashcards below were created by user starkisspk on FreezingBlue Flashcards. What would you like to do?


  1. 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
  2. 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
  3. 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
  4. 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";
    • }
  5. 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";
    • }
  6. 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
  7. 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
  8. What is the first thing foreach does before working with an array?
    • It resets the array.
    • same as reset($arr)
  9. 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
  10. 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.
  11. What is the output of the following:
    for ($i = 0; $i < 5; ++$i) {
    if ($i == 2)
    continue
    print "$i\n";
    }
    • 1
    • 2
    • 3
    • 4
  12. What values must be set in the php.ini to allow
    a include statement to include a http url?
    allow_url_include
  13. What values must be set in the php.ini to allow fopen
    to open a http url?
    allow_url_fopen

What would you like to do?

Home > Flashcards > Print Preview