Differences between php loop statements for and foreach

Source: Internet
Author: User
Welcome to the Linux community forum and interact with the 2 million technical staff. The difference between loop statements for and foreach in php: The foreach structure has been introduced in php since version 4, this is similar to perl and other languages. this is just a simple way to traverse arrays. foreach can only be used for arrays. When trying to use it for another data type or an uninitialized

Welcome to the Linux community forum and interact with 2 million technical staff> the difference between loop statements for and foreach in php: The foreach structure was introduced in php from version 4, this is similar to perl and other languages. this is just a simple way to traverse arrays. foreach can only be used for arrays. When trying to use it for another data type or an uninitialized

Welcome to the Linux community forum and interact with 2 million technicians>

Differences between loop statements for and foreach in php:

Php introduced the foreach structure from version 4, which is similar to perl and other languages. this is just a simple way to traverse arrays. foreach can only be used for arrays. An error occurs when you try to use it for another data type or an uninitialized variable.

There are two types of syntax. The second type is secondary, but it is the first type of useful extension.

Foreach (array_expression as $ value ){

......

}

Foreach (array_expression as $ key => $ value ){

......

}

The first format traverses the given array_expression array. in each loop, the value of the current unit is assigned to $ value and the pointer inside the array moves forward (so the next unit will be obtained in the next loop ).

In the second format, only the key names of the current Unit are assigned to the variable $ key in each loop.

Objects may be traversed from php 5.

Note: When foreach starts to execute, the internal pointer of the array will automatically point to the first unit. 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, rather than the array itself. therefore, the array pointer is not changed by the each () structure, and the modification to the returned array unit does not affect the original array. however, the internal pointer of the original array does move forward when processing the array. assuming that the foreach loop is running till the end, the internal pointer of the original array points to the end of the array.

Since php5, you can easily add & before $ value to modify the array unit. This method will assign values by reference instead of copying a value.

$ 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 when the retrieved array can be referenced (for example, a variable ).

Note: foreach does not support "@" to suppress error messages.

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.