Correct PHPforeach loop _ PHP Tutorial

Source: Internet
Author: User
Correctly apply the PHPforeach loop. Htmlbody? Php $ arrarray (one, two, three); foreach ($ arras $ value) {echoValue:. $ value.br ;}? In this PHPforeach loop example, bodyhtml creates the $ arr array

  1. < Html>
  2. < Body>
  3. <? Php
  4. $ Arr = array ("one", "two", "three ");
  5. Foreach ($ arr as $ value)
  6. {
  7. Echo "Value:". $ value ."< Br />";
  8. }
  9. ?>
  10. </Body>
  11. </Html>

In this PHP foreach loop example, we create a $ arr array with the element values "one", "two", "three ", then, a foreach loop is used to output the value of its array. The values in the array are saved to the variable value through "as", so that the value stores all elements of the array arr.

Here, we are targeting an array with automatic indexing. what if it is an associated array? See the following situation:

 
  1. $sounds=array(”cow”=>”moo”,”dog”
    =>”woof”,”pig”=>”oink”,”duck”=>
    ”quack”);
  2. foreach($sounds as $animal=>$sound)
  3. {
  4. Print “And of that farm he had a
    {$animal}”;
  5. }

Note: Through the PHP foreach loop, we assign the $ sounds key of the array to the variable $ animal and assign the value of the array to $ sound, in this way, we can easily access each "key-value pair" in the array.


The http://www.bkjia.com/PHPjc/446070.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446070.htmlTechArticlehtml body? Php $ arr = array (one, two, three); foreach ($ arras $ value) {echoValue:. $ value. br /;}? /Body/html in This PHP foreach loop example, we created the $ arr array, in...

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.