Loop statement foreach and list differential usage

Source: Internet
Author: User
more commonly used for foreach

<?php $price =array (' Apple ' =>10, ' orange ' =>20, ' banner ' =>30); foreach ($price as $key = + $value) {echo $key. ' = '. $value. ' <br> '; } echo ' <br> ';?>

There's a more advanced and common approach.

<?php $shuiguo =array (' Apple ' =>10, ' orange ' =>20, ' banner ' =>30); while (list ($changpin, $jiage) =each ($shuiguo)) {echo "$changpin = = $jiage". <br> '; }?>

The list () function can be used to decompose an array into a series of values that allow for the naming of new variables. Do not understand the list of click here Two code output is the same.

It is important to note that when using the each () function, the array will record the current element. If you want to use the array two times in the same script. It is necessary to reset the current element to the beginning of the array using the Reset () drop.

<?php $price =array (' Apple ' =>10, ' orange ' =>20, ' banner ' =>30); foreach ($price as $key = + $value) {echo $key. ' = '. $value. ' <br> '; } echo ' <br> '; Reset ($price); while (list ($key, $value) =each ($price)) {echo "$key + = $value", "<br>";}?>

This allows you to still use the array $price.
Some books, as a novice I, I do, knock down to see the effect, understand, write a post, easy to forget to look at later, said the relatively superficial, poor language expression, laughed at.

<?php/* *php in the looping statements in the learning notes * one. The while loop if (expression) executes only one statement at a time. while (expression) {Executes the loop body repeatedly;} * two. do-while loop * three. For loop * There are two types of loops depending on the loop condition * One: Count loop for * Another: conditional loop while Do-while//foreach * several and loops The relevant statement *break;//can be used for process control and loop bodies, jumping out of loops. continue;//can only be used for loop bodies, exiting this cycle. Exit Return * Write loops try not to exceed three layers. * Loop the flow control statement as far as possible not more than five layers. */$num = 0; while ($num <100) {echo "This is the result of performing the {$num} output <br>"; $num + +;}//Echo ' <table border= "1" width= "align=" Cente R ">"; Echo ' <caption>
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.