PHP loop statement basic syntax structure NOTE _ PHP Tutorial

Source: Internet
Author: User
Notes on the basic syntax structure of PHP loop statements. In php, loop statements include many, such as for, foreach, while, dowhile, and goto statements. below I will give you a brief introduction to the structure and usage of these loop statements. For loop control for (loop in php contains many loop statements, such as: for, foreach, while, do while, goto statements, next I will give you a brief introduction to the structure and usage of these loop statements.

For loop control

For (initial cyclic values; cyclic conditions; step size ){

// Execute the statement;

}

Example

The code is as follows:

FOR ($ I = 0; $ I <= 2; $ I ++)
{
Print "value is now". $ I ."
";
}

Output value

Value is now 0
Value is now 1
Value is now 2

In the first loop, $ I = 0, which means the expression ($ I <= 2) is true. Therefore, when the print statement is executed, $ I is incremented by 1 to 1.

In the second loop, $ = 1, which means the expression ($ I <= 2) is true. Therefore, when the print statement is executed, $ I is incremented by 1 to 2.

In the third iteration, $ I = 2, which means the expression ($ I <= 2) is true. Therefore, when the print statement is executed, $ I increments to 1 3.

In the fourth iteration, $ I = 3, which means the expression ($ I <= 2) is false. Therefore, PHP does not execute loops or print statements.

While loop

The basic syntax structure is

While (loop condition ){

// Execute the statement;
// The value of the cyclic condition changes. If this parameter is not added, it will become an endless loop.

}

Example

The code is as follows:


"; $ A ++ ;}?>

Let's just give a simple example. In the following example, the while loop statement is used. it means that when the variable $ a is less than or equal to 5, a loop is executed, and a join statement is executed in this loop, the first is to output the value of $ a, and the second is to add 1 to the value of $ a until $ a is less than or equal to 5 is FALSE, that is, $ a> 5 is used to stop the loop.

Do... while loop control

Basic syntax structure

Do {

// Execute the statement;
// The value of the cyclic condition changes. If this parameter is not added, it will become an endless loop.

} While (cyclic condition );

Example

In this example, run a loop first, that is, add $ I to 1, and then output the value of $ I. After the first loop is executed, check the condition $ I <5, if the condition is met, execute another loop until $ I <5 is FALSE.

The code is as follows:


";}While ($ I <5) ;?>


Loop-related statement-break

Basic concept: end the current for, while, do... while, switch, flow, you can give a number, indicating to exit to the layer.

1. the break statement jumps out of Layer 1 by default.

2. the number next to the break statement cannot exceed the number of loops that can actually jump out. Otherwise, a fatal error is reported.

Loop-related statements-continue

Basic concept: continue is used to end the remaining code of this loop. it starts a new loop (if the condition is true, it will continue to be executed) and can be followed by a number, indicates the number of cycles to start again.

Goto statement

Basic concept: Through the goto statement, we can jump the program to a specified place for execution.

Goto tag;

Statement;

Quick Start case:

The code is as follows:

Goto;

Echo 'A ';

A:

Echo 'BB ';

Constants in php

Note:

Constants can be understood as special variables:

1. no need before defining constants $

2. once a constant is defined, its value cannot be modified.

3. when defining a constant, it needs to assign an initial value.

4. constants can be defined through define or const.

5. constant names, which are generally in upper case and separated by underscores

6. When to use constants: if we do not want a value to change in the program, consider using constants, such as the circumference rate and tax rate ....

Quick Start case:

The code is as follows:


// Method 1

Define ("TAX_RATE", 0.08 );
Echo TAX_RATE;

// Method 2 (php5.3)

Const TAX_RATE2 = 0.1;
Echo '-'. TAX_RATE2;

?>

When while and goto statements, I will give you a brief introduction to the structure and usage of these loop statements. For loop control for (loop...

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.