In PHP, the loop statement includes a lot, such as: For,foreach,while,do While,goto statement, let me give you a brief introduction to these circular statement structure usage.
For loop control
For (cyclic initial value; loop condition; step) {
Execute the statement;
}
Cases
The code is as follows |
Copy Code |
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 expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i gets added 1, which becomes 1.
In the second loop, $ = 1, which means expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i gets added 1, which becomes 2.
In the third iteration, $i = 2, which means expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i increments, which becomes 1 3.
In the fourth iteration, $i = 3, which means expression, ($i <= 2), is false. Therefore, PHP does not execute loops and does not execute print statements.
While loop
The basic syntax structure is
while (loop condition) {
Execute the statement;
Cyclic condition value changes, not added will become a dead loop
}
Cases
The code is as follows |
Copy Code |
"; $a + +; }?>
|
Let's take a simple example and see for yourself. In this example, the use of a while loop, which means that when the variable $a less than or equal to 5 of the case, the execution of a loop, the loop executes a single statement, the output $a value, and the value of the $a is added 1 until the $a is less than or 5 is FALSE, that is $a > 5 to stop the Ring.
Do.. While loop control
BASIC syntax structure
do{
Execute the statement;
Cyclic condition value changes, not added will become a dead loop
}while (cyclic conditions);
Cases
example, in this example, a loop is executed first, $i plus 1, and then output the value of $i, after the first loop is executed, the condition is checked $i < 5, and if the condition is met, a loop is executed until $i < 5 is FALSE.
The code is as follows |
Copy Code |
"; } while ($i <5);? >
|
Loop-related statement-break
Basic concept: Indicates the end of the current for, while, do. While, switch, process, you can give a number that indicates exiting to the first layer.
1. Break statement jumps out of layer 1 by default
2. The number after the break statement cannot exceed the number of loop layers that can actually be jumped out, otherwise fatal error will be reported
Loop-related statement-continue
Basic concept: Continue is used to end this cycle of the remaining code, start a new cycle (if the condition is true, continue to execute), continue can also be followed by a number, indicating a restart from the first few cycles
Goto statement
Basic concept: We can jump the program to the specified place and execute it through the goto statement.
Goto label;
Statement
Quick Start Case:
The code is as follows |
Copy Code |
Goto A; echo ' AA '; A: Echo ' BB '; |
Constants in PHP
Description
So-called constants, we can be understood to be special variables: embodied in
1. Define constants before you need $
2. Once a constant is defined, its value cannot be modified
3. When a constant is defined, it is required to assign an initial value.
4. Constants can be define or const
5. The name of the constant, we generally say is all uppercase, and then use the underscore interval
6. When to use constants: In the program we do not want a value to change, then consider using constants, such as pi, tax rate ....
Quick Start Case:
The code is as follows |
Copy Code |
The first of these methods
Define ("Tax_rate", 0.08); Echo tax_rate; Second method (php5.3) Const tax_rate2=0.1; Echo '-'. Tax_rate2; ?> |
http://www.bkjia.com/PHPjc/632629.html www.bkjia.com true http://www.bkjia.com/PHPjc/632629.html techarticle in PHP, the loop statement includes a lot, such as: For,foreach,while,do While,goto statement, let me give you a brief introduction to these circular statement structure usage. For loop control for (Loop ...