Simple introduction to Process Control statements in PHP (with examples)

Source: Internet
Author: User
This article brings the content is about PHP in the Process Control statements Simple introduction (with examples), there is a certain reference value, the need for friends can refer to, I hope to help you.

First, conditional judgment statement

If statement
1. Single Branch if statement
2, double branch If-else statement
3, multi-branch If-elseif-else statement

$bool 1 = true;//if statement if ($bool 1) {    echo ' is true ';}//Result output is true//if-else statement if ($bool 1) {    echo ' is true ';} else {    echo ' is false ';} The result is output as true//if-elseif-else multi-branch Statement $var1 = 86;if ($var 1<100) {    echo ' less than 100 ';} ElseIf ($var 1>0) {    echo ' is greater than 0 ';} else{    Echo ' is not in the range of 0-100 ';} The result is less than 100 output

Switch statement

    • The parameter type in switch can only be integer or string

    • Switch each case code block without adding {}

    • There should be a break after each case, otherwise the contents of the next branch statement will continue to be executed

    • The default clause is not required and can be omitted

$grade = Mt_rand (0,100), switch ($grade) {case    $grade >90:            Echo ' $grade is A ';            break;        Case $grade >80:            echo "$grade is B";            break;        Case $grade >70:            echo "$grade is C";            break;        Case $grade >60:            echo "$grade is D";            break;        Default:            echo "$grade is E";            break;}

Second, circular control statements

1. While loop
2. Do-while Cycle
3. For loop
4. Foreach Loop

While Loop $a=1;while ($a <=) {    echo $a + +;} The result is a 12345678910//do-while loop, which executes a loop body, in judging the condition $b=10;do{    echo $b--;} while ($b >5),//Results for 109876//for loop for ($i =0; $i <=10; $i + +) {     echo $i;} The result is a 12345678910//foreach loop $array = Array (1,3,5,7,9), and a foreach ($array as $key = = $value) {    echo $key. = '. $value. ' ';} The result is 0=>1 1=>3 2=>5 3=>7 4=>9

Third, jump control statements

1. Break
Statement used to interrupt the execution of a loop
In a multilayer loop, you can add a digitally terminated multilayer loop after break

2, continue
Just jump out of this loop and continue to the following loop

3. Exit
Exits the current script, and subsequent statements are no longer executed

Breakfor ($i =0; $i <=10; $i + +) {     if ($i ==5) break;//$i equals 5 o'clock, jumps out of the loop    echo $i;} The result is 01234//continuefor ($i =0; $i <=10; $i + +) {     if ($i ==5) continue;//$i equals 5 o'clock, jump out of the loop    echo $i;} The result is 01234678910 no 5//continuefor ($i =0; $i <=10; $i + +) {     if ($i ==5) exit;//$i equals 5 o'clock, jump out of the loop    echo $i;} The result is 01234echo ' here the output will not execute, the script is stopped ';
Related Article

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.