PHP foreach Loop

Source: Internet
Author: User
Tags php foreach
PHP foreach Loop

The Foreach loop applies only to arrays and is used to iterate through each key/value pair in the array.

Grammar

foreach ($array as $value) {

code to be executed;

}

For each iteration of the loop, the value of the current array element is assigned to the $value variable, and the array pointer moves one at a time until the last array element is reached.

The following example shows a loop that outputs the value of a given array ($colors):

Instance

<?php $colors = Array ("Red", "green", "blue", "yellow"); foreach ($colors as $value) {  echo "$value <br>";}? >
  • 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.