For loop control
For (cyclic initial value; loop condition; step size) {
Execute the statement;
}
Cases
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 cycle, $i = 0, which means expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i is added by 1 and becomes 1.
In the second cycle, $ = 1, which means an expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i is added by 1 and becomes 2.
In the third iteration, $i = 2, which means expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $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 and does not execute print statements.
While loop
The basic grammatical structure is
while (loop condition) {
Execute the statement;
The cyclic condition value changes, does not add words will become the dead loop
}
Cases
The code is as follows |
Copy Code |
<?php $a =1;while ($a <=5) {echo "the number is". $a. "<br/>"; $a + +; }?> </body> |
Let's take a simple example and see it. The following example uses the While loop statement, it means that when the variable $a is less than or equal to 5, a loop executes, the loop executes a single statement, the value of the output $a, and the second is to add 1 to the value of $a until $a is less than or equal to 5 FALSE, that is, $a > 5 stops following Ring.
Do.. While loop control
Basic grammatical structure
do{
Execute the statement;
The cyclic condition value changes, does not add words will become the dead loop
}while (cyclic conditions);
Cases
For example, in this example, the first loop is performed, that is, $i plus 1, and then output $i value, after the first loop, check the condition $i < 5, and then loop again until $i < 5 is FALSE, if the condition is met.
The code is as follows |
Copy Code |
<?php $i =0;do {$i + +; echo "The number is". $i. "<br/>"; }while ($i <5);? > </body> |
Loop-related Statements-break
Basic concept: Represents the end of the current for, while, do. While, switch, process, you can give a number that represents the exit to the first level.
1. Break statement default jump out of 1 layer
2. The number behind the break statement, can not exceed the actual number of loops to jump out, otherwise, will report fatal error
Loop-related Statements-continue
Basic concept: Continue is used to end this cycle of remaining code, to start a new cycle (if the condition is true, continue to execute), continue can also be followed by a number, indicating from the first several loops restart
Goto statement
Basic concept: Through the goto statement we can jump the program to the specified place to execute.
Goto label;
Statement
Quick Start Cases:
The code is as follows |
Copy Code |
Goto A; echo ' AA '; A: Echo ' BB '; |
Constants in PHP
Description
The so-called constants, we can understand that is a special variable: reflected in
1. Define constants before you need $
2. Once a constant is defined, it cannot modify its value
3. Constants need to be assigned an initial value when they are defined.
4. Constants can be passed define or const
5. The name of the constant, we generally say is all uppercase, and then use the underline interval
6. When you need 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 Cases:
code is as follows |
copy code |
<?php //First method define ("Tax_ RATE ", 0.08); echo tax_rate; //second method (php5.3) Const tax_rate2=0.1; Echo '-'. Tax_rate2; ? |