The usage of the function in PHP--foreach () _php tutorial

Source: Internet
Author: User
PHP 4 introduces a foreach structure, much like Perl and other languages. This is just a simple way to iterate through an array. foreach can only be used with arrays, and an error occurs when trying to use it for other data types or an uninitialized variable. There are two kinds of syntax, and the second is a useful extension that is minor but the first.
Copy CodeThe code is as follows:
foreach (array_expression as $value)
Statement
foreach (array_expression as $key = $value)
Statement

the first format iterates through the given array of array_expression. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array is moved forward one step (so the next cell in the next loop will be taken).

The second format does the same thing, except that the key name of the current cell is assigned to the variable $key in each loop.
It is possible to traverse objects since PHP 5.

Note:
When foreach starts executing, the pointer inside the array automatically points to the first cell. This means that you do not need to call Reset () before the Foreach loop.

Note:
Unless the array is referenced, foreach operates on a copy of the specified array, not the array itself. The foreach array pointer has some side effects. Unless you reset it, do not rely on the value of the array pointer in the Foreach loop or after the loop.
Since PHP 5, it is easy to modify the elements of an array by adding & before $value. This method assigns a value to a reference instead of copying a value.
Copy CodeThe code is as follows:
$arr = Array (1, 2, 3, 4);
foreach ($arr as & $value) {
$value = $value * 2;
}
$arr is now Array (2, 4, 6, 8)
?>

This method is only available if the array being traversed can be referenced (for example, a variable).
Copy CodeThe code is as follows:
foreach (Array (1, 2, 3, 4) as & $value) {
$value = $value * 2;
}
?>

http://www.bkjia.com/PHPjc/327824.html www.bkjia.com true http://www.bkjia.com/PHPjc/327824.html techarticle PHP 4 introduces a foreach structure, much like Perl and other languages. This is just a simple way to iterate through an array. foreach can only be used with arrays when trying to use them with other data types or ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.