Home > Backend Development > PHP Tutorial > PHP Reading Notes (5) - Structural Statements, PHP Reading Notes_PHP Tutorial

PHP Reading Notes (5) - Structural Statements, PHP Reading Notes_PHP Tutorial

WBOY
Release: 2016-07-12 08:49:25
Original
871 people have browsed it

PHP reading notes (5) - structural statements, php reading notes

PHP structural statements

Sequential structure

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

Conditional structure if…else…

 The conditional 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 gender. At this time, we need to follow the conditions provided by the bathroom, the men's bathroom on the left, the women's bathroom on the right, or just the opposite, where gender is the condition of this conditional structure. For another example, it is now popular to use A, B, and C to grade scores. Assume that the test score is 93 points, which can be set to level A. If the test score is 87, it can be set to level B. The score range here is Conditions in conditional structures.

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

<span>1</span> <?<span>php
</span><span>2</span> <span>if</span><span>(条件){
</span><span>3</span>      <span>//</span><span>分配服务器干的任务A</span>
<span>4</span> }<span>else</span><span>{
</span><span>5</span>      <span>//</span><span>分配服务器干的任务B</span>
<span>6</span> <span>}
</span><span>7</span> ?>
Copy after login

 Through conditional judgment, if the return value is Boolean TRUE, task A will be executed. If the return value is FALSE, task B will be executed.

Conditional structure if…else if…

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

<?<span>php
</span><span>if</span><span>(条件一){
     </span><span>//</span><span>分配服务器干的任务A</span>
}<span>else</span> <span>if</span><span>(条件二){
     </span><span>//</span><span>分配服务器干的任务B</span>
<span>}
</span>?>
Copy after login

  Judgment by condition one , if the return value is a Boolean value TRUE, then execute task A, if the return value is FALSE, then judge condition two, if the return value is a Boolean value TRUE, Then execute task B, otherwise neither task A nor task B will be executed. The server will continue to perform other tasks.

Conditional structure switch…case…

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

<span> 1</span> <?<span>php
</span><span> 2</span> <span>switch</span><span> (条件)
</span><span> 3</span> <span>{
</span><span> 4</span> <span>case</span> 条件值一:
<span> 5</span>   <span>//</span><span>任务一</span>
<span> 6</span>   <span>break</span><span>; 
</span><span> 7</span> <span>case</span> 条件值二:
<span> 8</span>   <span>//</span><span>任务二</span>
<span> 9</span>   <span>break</span><span>;
</span><span>10</span> <span>default</span>:
<span>11</span>   <span>//</span><span>默认任务</span>
<span>12</span> <span>}
</span><span>13</span> ?>
Copy after login

 First determine the condition. If the return value of the condition is condition value one, then execute task one. If the return value of the condition is condition value two, then execute task two. If the return value of the condition is neither condition value one, If the condition value is not two, the default task will be executed. The function of break is to end the switch. Using the switch statement can avoid lengthy "if..else if..else" code blocks.

The function of break is to prevent the code from continuing to execute in the next case.

The while loop statement of loop structure in PHP

The loop structure is like running around the football field in circles, finishing one circle and then another. In other words, a certain task is repeatedly performed under certain conditions. Like a 400-meter track, if you run 800 meters, you run 2 laps. When you finish the first lap, you run the second lap. At the end of the second lap, you have reached 800 meters, and you stop running.

In PHP, the while loop statement is as follows:

 

<?<span>php
</span><span>while</span><span>(条件){ 
     </span><span>//</span><span>执行任务</span>
<span>}
</span>?>
Copy after login

 First determine whether a certain condition is met (whether the condition return value is TRUE), if it is met, execute the task, complete the task, and then determine whether the condition meets the requirements. If it is met, repeat the task, otherwise end the task.

The do while loop statement of loop structure in PHP

 There is another type of loop statement in PHP: do...while loop statement syntax is as follows:

<span>1</span> <?<span>php
</span><span>2</span> <span>do</span><span>{ 
</span><span>3</span>      <span>//</span><span>执行任务</span>
<span>4</span> }<span>while</span><span>(条件)
</span><span>5</span> ?>
Copy after login

 First execute the task (the while statement in the previous section first determines whether the condition is true, and then executes the task). After executing the task, determine whether a certain condition is met (whether the condition return value is TRUE), and if it is met, then Execute the task again, complete the task, and continue to determine the conditions.

The difference between while and do...while statements in loop structures in PHP

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

The for loop statement of loop structure in PHP

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

<span>1</span> <?<span>php
</span><span>2</span> <span>for</span><span>(初始化;循环条件;递增项){
</span><span>3</span>       <span>//</span><span>执行任务</span>
<span>4</span> <span>}
</span><span>5</span> ?>
Copy after login

 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 (execution task) is executed. If the value is FALSE, the loop is terminated. The "increment term" is evaluated (executed) after each loop. It is often used to loop through a block of code a specified number of times.

  PHP中循环结构之foreach循环语句

  在PHP中foreach循环语句,常用于遍历数组,一般有两种使用方式:不取下标、取下标。

  (1)只取值,不取下标

<span>1</span> <?<span>php
</span><span>2</span>  <span>foreach</span> (数组 <span>as</span><span> 值){
</span><span>3</span> <span>//</span><span>执行的任务</span>
<span>4</span> <span>}
</span><span>5</span> ?>
Copy after login

(2)同时取下标和值

<span>1</span> <?<span>php
</span><span>2</span> <span>foreach</span> (数组 <span>as</span> 下标 =><span> 值){
</span><span>3</span>  <span>//</span><span>执行的任务</span>
<span>4</span> <span>}
</span><span>5</span> ?>
Copy after login

 

 

上一节:PHP读书笔记(4)-运算符

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1138819.htmlTechArticlePHP读书笔记(5)-结构语句,php读书笔记 PHP结构语句 顺序结构 顺序结构就像一条直线,按着顺序一直往下执行。我们编写的代码默认都是...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template