PHP loop structure example. For loop statement print pyramid complete pyramid copy code: print pyramid $ n25; for ($ i1; $ I $ n; $ I ++) {space loop for ($ k1; $ k $ n-$ I; $ k ++) {echo;} character 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:
Step 2: write the computing background code
The code is as follows:
$ Num1 = $ _ REQUEST ['num1'];
$ Num2 = $ _ REQUEST ['num2'];
$ Operation = $ _ REQUEST ['operation'];
$ Res = 0;
Switch ($ operation ){
Case '+ ':
$ Res = $ num1 + $ num2;
Break;
Case '-':
$ Res = $ num1-$ num2;
Break;
Case '*':
$ Res = $ num1 * $ num2;
Break;
Case '/':
$ Res = $ num1/$ num2;
Break;
Default:
Echo 'incorrect input ';
}
Echo 'result is '. $ res;
?>
Continue statement: Skip the code after this loop. You can specify the number of layers to jump out of, for example, continue 2; two layers to jump out, similar to break 2;
Goto statement: like the C language, jump to the code with tags. the code in the middle is not executed and is ignored directly.
The code is as follows:
// Goto statement
// I only run once
For ($ I = 0, $ j = 50; $ I <100; $ I ++ ){
While ($ j --){
If ($ j = 17) goto end;
}
}
Echo 'I = $ I ';
End:
Echo 'I ='. $ I. 'j = '. $ j;
Keep it simple, keep it clear. -- PAXSTER
Complete pyramid print complete pyramid code: // Print the pyramid $ n = 25; for ($ I = 1; $ I = $ n; $ I ++) {// Space Loop for ($ k = 1; $ k = $ n-$ I; $ k ++) {echo '';} // character...