Control Sructure Facts

  1. foreach (array_expression as $value)
    statement
    foreach (array_expression as $key => $value)
    statement

    * The internal array pointer is advanced by one each time
    * As of PHP 5, it is possible to iterate objects too
    * When foreach first starts executing, the internal array pointer is automatically reset to the first element drof the array
    * Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it
    * Foreach does not support the ability to suppress error messages using '@'

    As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.
    $arr = array(1, 2, 3, 4);
    foreach ($arr as &$value) {
    $value = $value * 2;
    }// $arr is now array(2, 4, 6, 8)
    unset($value); // break the reference with the last element

    This is possible only if iterated array can be referenced (i.e. is variable), that means the following code won't work:
    foreach (array(1, 2, 3, 4) as &$value) {
    $value = $value * 2;
    }
  2. You may have noticed that the following are functionally identical:
    $arr = array("one", "two", "three");
    reset($arr);
    while (list(, $value) = each($arr)) {
    echo "Value: $value<br />\n";
    }

    foreach ($arr as $value) {
    echo "Value: $value<br />\n";
    }

    The following are also functionally identical:
    $arr = array("one", "two", "three");
    reset($arr);
    while (list($key, $value) = each($arr)) {
    echo "Key: $key; Value: $value<br />\n";
    }
    foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
    }
  3. If an include occurs in a file, the variables defined in the include takes the scope available at the line that in was included at
    If an include occurs in a function the variables defined in the include takes the scope of the function.
    An exception to this rule are magic constants which are evaluated by the parser before the include occurs.

    If allow_url_include is set in the php.ini file php can include a remote url.
    included 'http://www.google.com'
    If the ouput contains php tags, it will be parsed

    When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.
  4. Comparing return value of include

    // won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
    if (include('vars.php') == 'OK') {
    echo 'OK';
    }
    // works
    if ((include 'vars.php') == 'OK') {
    echo 'OK';
    }
    //if no values is returned in the include, the include automatically returns 1 if the include was successful and false otherwise
Author
starkisspk
ID
11177
Card Set
Control Sructure Facts
Description
control structure facts
Updated