Examples of while, Do...while, for, foreach four loops

Source: Internet
Author: User
in PHPWhile loop , the number of times the code block is executed, or the loop executes the code block when the specified condition is true.


When we write code, we often need a block of code to execute repeatedly. We can use the while loop statement to accomplish this task.
while--executes a code block as long as the specified condition is true.
do...while--executes the code block first, and then repeats the loop when the specified condition is set.
The number of times the for--Loop executes the code block.
foreach--loops a block of code based on each element in the array.

While loop

The while statement loops through the code block until the specified condition is not true.

while (condition)
The code to loop execution;


Set the value of a variable A to one ($a =11).
Then, as long as the a< or =20,while Loop will continue to run. Cycle once, a will increment by 1;


$a =11;while ($a <=20) {echo "Output value:". $a. "; $a + +;}

Will output the result: While output value: 11

While output value: 12
While output value: 13
While output value: 14
While output value: 15
While output value: 16
While output value: 17
While output value: 18
While output value: 19
While output value: 20

Here is the instance code:

<?php$a=11;while ($a <=20) {echo "While output value:". $a. " <br> "; $a + +;}? >while Loop Code,

Do...while Cycle

The Do...while statement executes the code at least once, and then checks the condition, repeating the loop.


The code to execute;
while (condition)


The following instance first sets the value of variable A to 1 ($a =11). Then, start the Do...while loop. The loop increments the value of variable a by 1 and then outputs it. Check the condition first (a drizzle or equal to 20), as long as a is less than or equal to 5, the loop will be executed according to Xu:

Do...while Output Value: 11
Do...while Output Value: 12
Do...while Output Value: 13
Do...while Output Value: 14
Do...while Output Value: 15
Do...while Output Value: 16
Do...while Output Value: 17
Do...while Output Value: 18
Do...while Output Value: 19
Do...while Output Value: 20

Here is the instance code:

<?php$a=11;do{echo "Do...while output value:". $a. " <br/> "; $a + +;} while ($a <=20);? >do...while Loop Code

For loop

Loop executes the number of times specified by the code block, or the loop executes the code block when the specified condition is true.

The For loop is used in cases where you know in advance how many times the script needs to run.


for (initial value; condition increment;) {
The code to execute

Initial value: The primary is to initialize a variable value that sets a counter (but can be any code that is executed once at the beginning of the loop).

Condition: The throttling condition of the loop execution. If true, the loop continues. If FALSE, the loop ends.

Increment: Mainly used for incrementing the counter (but can be any code executed at the end of the loop).

Note: The above initial and increment parameters can be empty, or there are multiple expressions (separated by commas).


For output value: 11
For output value: 12
For output value: 13
For output value: 14
For output value: 15
For output value: 16
For output value: 17
For output value: 18
For output value: 19
For output value: 20

Here is the instance code:

<?php for ($a =11;, $a <=20;, $a + +) {  echo "for output value:". $a. " <br/> "; }?>

foreach Loop

A foreach loop is used to iterate through an array.

foreach ($array as $value) {
to execute the code;

The value of the current array is assigned to the $value variable (the array pointer moves one at a time), and you will see the next value in the array for the next loop.


The following example shows a loop that outputs the value of a given array:

Output array values one by one: one
Output array values individually:
Output array values individually: three
Output array values individually: four
Output array values individually: five

Here is the instance code:

<?php $x =array ("One", "one", "a", "three", "four", "Five"), foreach ($x as $value) { echo "Output array values individually:". $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.