PHP reading notes (5)-structure statements, php reading notes _ PHP Tutorial

Source: Internet
Author: User
PHP reading notes (5)-structure statements, php reading notes. PHP reading notes (5)-structure statements, php reading notes PHP structure statements order structure is like a straight line, in order to continue to execute. By default, the code we write is PHP reading notes (5)-structure statements, php reading notes
PHP structured statement sequence structure

The ordered structure is like a straight line, which is executed in sequence. By default, the code we write is executed in a sequential structure.

Conditional structure: if... Else...

The condition structure is like a fork in the road. you can go left or right. For example, when we go to the bathroom, we know our sex. at this time, we need to follow the conditions provided by the bathroom. the men's restroom on the left, women's restroom on the right, or the opposite is true, gender is the condition of this condition structure. For example, scores A, B, and C are commonly used for grading. if the score is 93, you can set it to grade A and the score is 87, you can set it to grade B, where the score range is a condition in the condition structure.

"If…" in PHP... Else ..." Syntax:

1
 

If the return value is TRUE, task A is executed. if the return value is FALSE, task B is executed.

Conditional structure: if... Else if...

"If…" in PHP... Else if ..." Syntax:

 

PassCondition 1If the return value is TRUE, task A is executed. if the return value is FALSE, condition 2 is judged. if the return value is TRUE, task B is executed, otherwise, neither task A nor task B is executed. The server continues to execute other tasks.

Conditional structure switch... Case...

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

1
 

First, judge the condition. if the return value of the condition is condition value 1, execute task 1. if the return value of the condition is condition value 2, execute task 2, if the return value of a condition is neither a condition value nor a condition value two, the default task is executed. The role of break is to end the switch. Using the switch statement can avoid lengthy "if... else" code blocks.

Break is used to prevent code from entering the next case for further execution.

While loop statement in PHP

The loop structure is like running a football field in a circle, and then running again after a circle. That is to say, a task is executed repeatedly under the conditions that meet the requirements. Like a 400-meter runway, if you run 800-meter runway, you will run 2 laps. when you run the first lap and then run the second lap, the second lap has reached 800 meters and stops running.

In PHP, the while loop statement is as follows:

  

 

First, determine whether a condition meets (whether the condition return value is TRUE). If yes, the task is executed, the task is completed, and whether the condition meets the requirements. If yes, the task is executed again, otherwise, end the task.

Do while loop statement in PHP loop structure

In PHP, the loop statement syntax is as follows:

1
 

First, execute the task (the while statement in the previous section first checks whether the condition is TRUE, and then executes the task). after the task is executed, determine whether a condition is met (whether the return value of the condition is TRUE ), if yes, execute the task again. after the task is completed, continue to determine the conditions.

Loop structure in PHP: while and do... Differences between while statements

While and do... The difference between while loop statements is that while first determines whether the condition is true, and then executes the loop, do... while executes a task first, and then determines whether to continue executing the loop, that is, do... while will execute at least one task. When the condition is FALSE, the while task will not be executed once, and the task in do... while will be executed once.

For loop statement in PHP

There is also a loop statement in PHP. the for loop statement structure is as follows:

1
 

In the for statement, "initialization" is evaluated unconditionally once before the loop starts, and "loop condition" is evaluated before each loop starts. If the value is TRUE, the loop continues and the loop body statement (task execution) is executed ). If the value is FALSE, the loop is terminated. "Incremental items" are evaluated (executed) after each loop ). It is often used to specify the number of times the code block is executed cyclically.

Foreach loop statement in PHP loop structure

In PHP, the foreach loop statement is often used to traverse arrays. There are two ways to use it: no subscript and no subscript.

(1) only values, not subscript

1
 

(2) obtain both the subscript and value.

1
 Value) {3 // Task 4} 5?>

Previous section: PHP reading notes (4)-Operators

Explain (5)-structure statement, php reading notes PHP structure statement order structure is like a straight line, in order to continue to execute. The code we write is by default...

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.