Php process control statements and loop control statements (explanation)

Source: Internet
Author: User

Php process control statements and loop control statements (explanation)

1. Flow Control statements include if, ii... else, elseif (sometimes else if), and switch.

The statement format in PHP is:

If (condition met) {execution statement}

If (condition met) {execution statement} else {execution statement}

If (condition met) {execution statement} elseif {execution statement} ...... else {execution statement}

Switch (condition) {case 1: Statement; break;

Case 2: Statement; break;

Case 3: Statement; break;

Default: Statement; break ;}

If: only one condition exists.

If... else: two conditions exist.

Elseif: there are multiple conditions

Switch: when multiple conditions exist, the elseif statement works the same as the switch statement. To avoid complicated and lengthy statements, use the switch statement

2. There are three types of loop control statements: while, for, and do while. For example, All integers smaller than 5 are output.

The statement format 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);


1. The while LOOP does not know the number of cycles, and the for loop knows the number of cycles.

2. A complex PHP code may contain multiple condition control statements, loop control statements, and functions. It is very troublesome to find matching braces. To this end, PHP provides another writing format, including if, while, for, foreach, and switch. The basic form of writing this form is: Use the colon ":" to replace the braces "{" on the left, use endif;, endwhile;, endfor;, endforeach;, endswitch; to replace the braces "}" on the right.


Break: terminate a loop

Continue: Terminate the loop and continue the next loop until the loop ends.

The above php-based process control statements and cyclic control statements (explanations) are all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer.

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: 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.