This article mainly introduces the example of PHP loop structure. For more information, see
This article mainly introduces the example of PHP loop structure. For more information, see
For Loop statement
Print pyramid
Complete pyramid
The Code is as follows:
// Print the pyramid
$ N = 25;
For ($ I = 1; $ I <= $ n; $ I ++ ){
// Space loop
For ($ k = 1; $ k <= $ n-$ I; $ k ++ ){
Echo '';
}
// Character Loop
For ($ j = 1; $ j <= $ I * 2-1; $ j ++ ){
If ($ I = 1 | $ I = $ n ){
Echo '.';
}
Else {
If ($ j = 1 | $ j = $ I * 2-1 ){
Echo '.';
} Else {
Echo '';
}
}
}
/*
For ($ j = 1; $ j <= ($ i-1) * 2 + 1; $ j ++ ){
Echo '.';
}*/
Echo'
';
}
Switch statement:
The Code is as follows:
/* $ A = "1 ";
Switch ($ ){
Case 1:
Echo $;
Break;
Default:
Echo "error ";
Break;
}
// Automatically convert strings and numbers
Processing When a Boolean value is encountered in the switch selection statement:
The Code is as follows:
$ B = true;
Switch ($ B ){
Case false:
Echo "mismatch ";
Break;
// Indicates that values other than false can be true ----- automatic conversion type
Case "1 ":
Echo "matched ";
Break;
Default:
Echo "ko ";
}
// 1. The default statement is last executed regardless of the order. If other cases are not matched, the default statement is executed.
// 2. If there is no break statement, the result of the next case will be output until there is a break.
While loop and do... while loop:
The Code is as follows:
/* While Loop
$ I = 0;
While ($ I <10 ){
Echo "paxster
". $ I;
$ I ++;
}
// Do... while loop -------- first execute and then judge, at least once
/* $ Do = 0;
Do {
Echo'
Paxster ';
$ Do = $ do + 1;
} While ($ do <8 );*/
The combination of while loop and switch selection statement:
Constant:
The Code is as follows:
// Define constants ----- two methods
Define ('tax ', 200 );
Echo TAX;
Const Tab = 100;
Echo Tab;
Compile a simple calculator:
Step 1: Write the Input Interface
The Code is as follows: