PHP structure statement
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 style="color: #008080;">1</span> <span style="color: #000000;">php </span><span style="color: #008080;">2</span> <span style="color: #0000ff;">if</span><span style="color: #000000;">(条件){ </span><span style="color: #008080;">3</span> <span style="color: #008000;">//</span><span style="color: #008000;">分配服务器干的任务A</span> <span style="color: #008080;">4</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #008080;">5</span> <span style="color: #008000;">//</span><span style="color: #008000;">分配服务器干的任务B</span> <span style="color: #008080;">6</span> <span style="color: #000000;">} </span><span style="color: #008080;">7</span> ?>
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 style="color: #000000;">php </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(条件一){ </span><span style="color: #008000;">//</span><span style="color: #008000;">分配服务器干的任务A</span> }<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #000000;">(条件二){ </span><span style="color: #008000;">//</span><span style="color: #008000;">分配服务器干的任务B</span> <span style="color: #000000;">} </span>?>
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 style="color: #008080;"> 1</span> <span style="color: #000000;">php </span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">switch</span><span style="color: #000000;"> (条件) </span><span style="color: #008080;"> 3</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">case</span> 条件值一: <span style="color: #008080;"> 5</span> <span style="color: #008000;">//</span><span style="color: #008000;">任务一</span> <span style="color: #008080;"> 6</span> <span style="color: #0000ff;">break</span><span style="color: #000000;">; </span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">case</span> 条件值二: <span style="color: #008080;"> 8</span> <span style="color: #008000;">//</span><span style="color: #008000;">任务二</span> <span style="color: #008080;"> 9</span> <span style="color: #0000ff;">break</span><span style="color: #000000;">; </span><span style="color: #008080;">10</span> <span style="color: #0000ff;">default</span>: <span style="color: #008080;">11</span> <span style="color: #008000;">//</span><span style="color: #008000;">默认任务</span> <span style="color: #008080;">12</span> <span style="color: #000000;">} </span><span style="color: #008080;">13</span> ?>
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 style="color: #000000;">php </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(条件){ </span><span style="color: #008000;">//</span><span style="color: #008000;">执行任务</span> <span style="color: #000000;">} </span>?>
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 style="color: #008080;">1</span> <span style="color: #000000;">php </span><span style="color: #008080;">2</span> <span style="color: #0000ff;">do</span><span style="color: #000000;">{ </span><span style="color: #008080;">3</span> <span style="color: #008000;">//</span><span style="color: #008000;">执行任务</span> <span style="color: #008080;">4</span> }<span style="color: #0000ff;">while</span><span style="color: #000000;">(条件) </span><span style="color: #008080;">5</span> ?>
首先执行任务(上一节的while语句是先判断条件是否成立,再执行任务),执行任务完毕,判断某个条件是否符合(条件返回值是否为TRUE),若符合则再次执行任务,执行完毕任务,继续判定条件。
PHP中循环结构之while与do…while语句的区别
while与do…while循环语句的区别是,while先判断条件是否成立,后执行循环,do...while先执行一次任务,再判断是否继续执行循环,也就是说do...while至少会执行一次任务。当条件为FALSE时,while中的任务会一次也不执行,do...while中的任务会执行1次.
PHP中循环结构之for循环语句
在PHP中还有一种循环语句,for循环语句结构如下:
<span style="color: #008080;">1</span> <span style="color: #000000;">php </span><span style="color: #008080;">2</span> <span style="color: #0000ff;">for</span><span style="color: #000000;">(初始化;循环条件;递增项){ </span><span style="color: #008080;">3</span> <span style="color: #008000;">//</span><span style="color: #008000;">执行任务</span> <span style="color: #008080;">4</span> <span style="color: #000000;">} </span><span style="color: #008080;">5</span> ?>
for 语句中,“初始化”在循环开始前无条件求值一次,“循环条件”在每次循环开始前求值。如果值为 TRUE,则继续循环,执行循环体语句(执行任务)。如果值为 FALSE,则终止循环。“递增项”在每次循环之后被求值(执行)。其常用于循环执行代码块指定的次数。
PHP中循环结构之foreach循环语句
在PHP中foreach循环语句,常用于遍历数组,一般有两种使用方式:不取下标、取下标。
(1)只取值,不取下标
<span style="color: #008080;">1</span> <span style="color: #000000;">php </span><span style="color: #008080;">2</span> <span style="color: #0000ff;">foreach</span> (数组 <span style="color: #0000ff;">as</span><span style="color: #000000;"> 值){ </span><span style="color: #008080;">3</span> <span style="color: #008000;">//</span><span style="color: #008000;">执行的任务</span> <span style="color: #008080;">4</span> <span style="color: #000000;">} </span><span style="color: #008080;">5</span> ?>
(2)同时取下标和值
<span style="color: #008080;">1</span> <span style="color: #000000;">php </span><span style="color: #008080;">2</span> <span style="color: #0000ff;">foreach</span> (数组 <span style="color: #0000ff;">as</span> 下标 =><span style="color: #000000;"> 值){ </span><span style="color: #008080;">3</span> <span style="color: #008000;">//</span><span style="color: #008000;">执行的任务</span> <span style="color: #008080;">4</span> <span style="color: #000000;">} </span><span style="color: #008080;">5</span> ?>
上一节:PHP读书笔记(4)-运算符
下一节:PHP读书笔记(6)- 数组

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)