PHP reading notes (5)-Structure statements

Source: Internet
Author: User

PHP Structure Statement order structure

 The sequential structure is like a straight line, which is carried down in order. The code we write is executed by default in the order structure.

The if...else of the conditional structure ...

  The conditional structure is like a fork that can walk to the left or to the right. For example, in the bathroom, we know our sex, this time we need to according to the bathroom conditions, the left men's room, the right woman's toilet, or just the opposite, where sex is the condition of the conditional structure. For example, now the scores are popular using A, B, C to grade, assuming that the test score is 93, you can set it to level A, the test score is 87, you can set it to level B, where the score interval is the condition of the conditional structure.

The "If...else ..." syntax in PHP is as follows:

1 <? PHP 2 if (condition) {3      // assigning servers to do tasks a 4 }else{5      // Assignment Server dry task B6}  7 ?>

  By conditional judgment, if the return value is Boolean true, then task A is executed, and if the return value is False, then task B is executed.

Conditional structure of the if...else if ...

 The "If...else If ..." Syntax in PHP is as follows:

<? PHP if (condition i) {     // Assignment Server Dry task A}elseif(condition two) {     // assigning servers to dry tasks B }?>

 by condition One , if the return value is a Boolean value of True, then execute task A, if the return value is false, then the condition two is evaluated, if the return value is Boolean true, then the task B is executed, otherwise neither task A nor task B is executed. The server will continue to perform other tasks.

The switch...case of the conditional structure ...

  The "Switch...case ..." syntax in PHP is as follows:

1<?PHP2 Switch(condition)3 {4  CaseCondition value One:5   //Task One6    Break; 7  CaseCondition Value Two:8   //Task Two9    Break;Ten default: One   //Default Task A } -?>

  First judge the condition, if the return value of the condition is the condition value one, then perform task one, if the value returned by the condition is the condition value two, then perform task two, if the return value of the condition is neither the condition value nor the condition value two, the default task is executed. The effect of break is to end the switch and use the switch statement to avoid lengthy "if." else if: Else "code block.

The effect of break is to prevent the code from entering the next case to continue execution.

Loop structure in PHP while loop statement

 The loop structure is like running a football field in a circle and running a circle. In other words, a task is performed repeatedly under the conditions that are met. Like 400.1-meter laps of the runway, run 800 meters, then run 2 laps, when running the first lap and then run the second lap, the second lap end has reached 800 meters, stop running.

In PHP, the while loop statement is as follows:

  

<? PHP  while (condition) {      // Execute task }?>

  First, determine whether a condition is compliant (the conditional return value is true), perform the task if it is met, perform the task, and then determine whether the condition meets the requirements, and then repeat the task, or end the task.

Do and loop statements for the loop structure in PHP

  There is another way of looping statements in PHP: The Do...while Loop statement syntax is as follows:

1 <? PHP 2  Do  3      // perform task 4 }while(condition)5 ?>

  Perform the task first (the while statement in the previous section is to determine whether the condition is set up, then perform the task), perform the task, determine whether a condition is met (the conditional return value is true), if the task is executed again, the completion of the task, continue to determine the condition.

The difference between the while and the do...while statements of the cyclic structure in PHP

 The difference between the while and the Do...while Loop statement is that while the time first determines whether the condition is true, then executes the loop, Do...while executes the task once and then determines whether to continue the loop, that is, the do...while will perform at least one task. When the condition is false, the task in the while is not executed once, and the task in Do...while executes 1 times.

A For loop statement for the loop structure in PHP

  There is also a looping statement in PHP, with the FOR Loop statement structured as follows:

1 <? PHP 2  for {3 (initialize; loop condition; increment item)       // Perform Tasks 4 }5 ?>

 For statement, initialization is evaluated unconditionally before the loop begins, and the loop condition is evaluated before each cycle begins. If the value is TRUE, the loop continues, executing the Loop body statement (performing the task). If the value is FALSE, the loop is terminated. The increment item is evaluated after each loop (execution). It is often used to loop the number of times a code block is specified.

The Foreach Loop statement for the loop structure in PHP

 In PHP, the Foreach Loop statement, commonly used to iterate over an array, is generally used in two ways: Do not remove the label, remove the label.

(1) value only, do not remove the label

1 <? PHP 2  foreach  as value) {3// performed task 4}5 ?>

(2) Remove the mark and value at the same time

1 <? PHP 2 foreach  as Subscript = = value) {3  // performed task 4}5 ? >

previous section: PHP reading Notes (4)-Operators

PHP reading notes (5)-Structure statements

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.