PHP loop structure example _ PHP Tutorial

Source: Internet
Author: User
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...

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.