PHP notes 5:php Process Control branching structure

Source: Internet
Author: User

Overview of PHP Process Control

1, branch results (conditional structure)

One-way branch:
if (condition) {//curly braces can be followed without semicolons;
# code ...
}

Two-Way Branch
if (condition) {
# code ...
} else {
# code ...
}

Multi-Channel Branch
if (condition) {
# code ...
} elseif (condition) {//elseif can be written as else if
# code ...
} else {
# code ...
}


2, switch Multiplex branch structure

Use if structure for judging range, single value with switch

Switch (variable) {//variable with integral type and string
Case ' value1 ':
Case ' value2 '://can have multiple case
# code ...
Break If you do not break, the next case will continue to execute
Case ' Value3 ':
# code ...
Break
Default://defaults, optional
# code ...
Break
}

Second, PHP Loop statement

1, while loop

while (condition) {//First judge the condition before execution
# code ...
}


2, do While loop

Do {//First judgment condition
# code ...
while (condition); Be careful not to omit the semicolon


3, for Loop

For ($i =0 $i < $i + +) {//generally used for counting, two semicolons cannot be omitted
# code ...
}

Example: 99 multiplication table


For ($i =1 $i < $i + +) {

for ($ii =1; $ii <= $i; $ii + +) {

echo "$ii X $i =". $ii * $i. ';

}

echo "<br>";

}

Third, exit the loop statement

Exit this cycle and the next cycle will no longer be done
Break

Exit this cycle, the next cycle continues, only for the loop body (for,while,do-while)
Coninue

Completely quit the program
Exit

function executes the return value, ending the statement of the function
Return


Exit multi-tier loops

Break 1//exit current loop
Break 2//exit 2-tier loop
Continue 2

Error instances

$i = 0;
while ($i < 10) {
if ($i = = 5)
Continue
echo "$i";
$i + +; Note that the i++ here will not be executed, resulting in a dead loop
}

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.