A brief talk on the loop while, Do...while, for, foreach four kinds of cyclic _php instances in PHP

Source: Internet
Author: User

A while loop in PHP that iterates through the number of code block attempts, or loops through code blocks when the specified condition is true.

-------------------------------------------------------------

When we write code, we often need a block of code repeated several times. We can use the while loop statement to accomplish this task.
while--executes the code block as long as the specified condition is set.
do...while--executes the code block first, and then repeats the loop when the specified condition is set up.
for--the number of times the code block is executed by the loop.
foreach--loops The 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 valid.

while (condition)
{
The code to loop through;
}

Case:

Set the value of a variable A to one ($a =11).
Then, as long as a< or =20,while loops will continue to run. Once a loop, a is incremented 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, then checks the condition and repeats the loop.

Grammar

Todo
{
The code to execute;
}
while (condition)

Instance

The following instance first sets the value of variable A to 1 ($a =11). Then, start the do...while cycle. The loop increments the value of variable a by 1 and then outputs. First check the condition (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

Iterates through the number of times the code block specifies, or loops through the code block when the specified condition is true.

For loop

The For loop is used when you know beforehand how many times a script needs to run.

Grammar

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

Initial value: Primarily initializes a variable value that is used to set a counter (but can be any code that executes once at the beginning of the loop).

Condition: The restriction condition for circular execution. If TRUE, the loop continues. If FALSE, the loop ends.

Increment: Primarily for incrementing counters (but can be any code that executes at the end of the loop).

Note: The above initial and incremental parameters can be empty or have multiple expressions (separated by commas).

Instance

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

The Foreach loop is used to traverse an array.

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

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

Instance

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

Output array values: one
Output Array Value: two
Output Array Value: three
Output Array Value: Four
Output Array Value: Five

Here is the instance code:

<?php
 $x =array ("One", "two", "three", "four", "five");
 foreach ($x as $value) {
  echo outputs the array value individually: ". $value." <br/> ";
 }
 ? >

The above article on the cycle while in PHP, Do...while, for, foreach four kinds of loops are small parts to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.