A detailed explanation of flow control statements and loop control statements in PHP

Source: Internet
Author: User
1, the Process Control statements mainly have if, ii...else, ElseIf (sometimes can also be written as else if), switch four kinds. The statement format in PHP is:

if (condition satisfied) {EXECUTE statement}

if (condition satisfied) {EXECUTE statement} else {EXECUTE statement}

if (condition satisfied) {EXECUTE statement} ElseIf {EXECUTE statement} ElseIf {EXECUTE statement} ... else {EXECUTE statement}

Switch (condition) {Case 1: statement; break;

Case 2: statement; break;

Case 3: statement; break;

Default: statement; break;}

if: Condition only one

If...else: There are two conditions

ElseIf: Conditions have multiple

SWITCH: Conditional multiple When the condition is multiple, ElseIf is the same as the switch statement. But to avoid lengthy statements, use the switch statement

2, the loop control statement mainly has while, for, do and three kinds. For example, to output all integers less than 5, the format of the statement in PHP is:

While statement ******* $i = 0;    while ($i <5) {echo $i;  $i + +;  } *******for statement ******* for ($i = 0; $i < 5; $i + +) {echo $i;  } ******do while statement ******* $i = 0;    do {echo $i;  $i + +; }while ($i <5);

Note

1, while loop implementation does not know the number of cycles, for loop known number of cycles.

2, in a complex PHP code, may contain a number of conditional control statements, loop control statements and functions, find matching curly braces "{}" very cumbersome. To do this, PHP provides a different writing format, including if, while, for, foreach, and switch can be used. The basic form of writing this form is to use the colon ":" instead of the left brace "{", using endif, Endwhile, ENDfor, Endforeach, Endswitch; to replace the curly brace "}" on the right.

"keywords"

Break: Terminate Loop

Continue: Terminates this cycle and continues the next loop until the end of the loop

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.