PHP loop structure example

Source: Internet
Author: User

For Loop statement

Print pyramid

Complete pyramid
Copy codeThe 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 '<br/> ';

}

Switch statement:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe Code is as follows:
/* While Loop
$ I = 0;
While ($ I <10 ){
Echo "paxster <br>". $ I;
$ I ++;
}
// Do... while loop -------- first execute and then judge, at least once
/* $ Do = 0;
Do {
Echo '<br/> Paxster ';
$ Do = $ do + 1;
} While ($ do <8 );*/

The combination of while loop and switch selection statement:

Constant:
Copy codeThe 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
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> </title>

</Head>
<Body>

<Form action = "CAL. php" method = "get">
<Input type = "text" placeholder = "enter a number" name = "num1">
<Input type = "text" placeholder = "enter a number" name = "num2">
<Select name = "operation">
<Option value = "+" >+ </option>
<Option value = "-">-</option>
<Option value = "*"> * </option>
<Option value = "/">/</option>
</Select>
<Input type = "submit" value = "computing">
</Form>
</Body>
</Html>

Step 2: Write the computing background code
Copy codeThe Code is as follows:
<? Php
$ 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.

Copy codeThe 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

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.