Cyclic while, Do...while, for, foreach four loops in PHP.

Source: Internet
Author: User

A while loop in PHP that loops through the number of times a block is made, or loops through a block of code 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;}
Case:

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:

1 <? PHP 2 $a=11; 3  while ($a<=20) {4   Echo "While output value:". $a. " <br> "; 5   $a+ +; 6 }7 ?>
While Loop code,

Do...while Cycle

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

Grammar
do{  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 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:

1 <? PHP 2 $a=11; 3  Do {4   echo"Do...while output value:". $a. " <br/> "; 5   $a+ +; 6 }while ($a<=20); 7 ?>
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.

For loop

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

Grammar
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).

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:

1 <? PHP 2  for ($a=11;,$a<=20;,$a+ +) {3   Echo "For output value:". $a. " <br/> "; 4 }5 ?>
For Loop code,

foreach Loop

A foreach loop is used to iterate through 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 you will see the next value in the array for the next loop.

Instance

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:

1 <? PHP 2 $x=array("One", "one", "three", "four", "five"); 3 foreach ($xas$value) {4   Echo "Output array values individually:". $value. " <br/> "; 5 }6 ?>
foreach Loop code,

Cyclic while, Do...while, for, foreach four loops in PHP.

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.