Notes on the basic syntax structure of PHP loop statements

Source: Internet
Author: User

In php, loop statements include many, such as for, foreach, while, do while, and goto statements. Below 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: Copy code

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

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: Copy code

<Html> <body>
<? Php $ a = 1; while ($ a <= 5) {echo "The number is". $ a. "<br/>"; $ a ++ ;}?>
</Body>

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: Copy code

<Html> <body>
<? Php $ I = 0; do {$ I ++; echo "The number is ". $ I. "<br/>";} while ($ I <5);?>
</Body>


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: Copy code

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: Copy code

<? Php


// Method 1

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

// Method 2 (php5.3)

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

?>

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.