Table of Contents
PHP reading notes (5) - structural statements, php reading notes
Sequential structure
Conditional structure if…else…
Conditional structure if…else if…
Conditional structure switch…case…
The while loop statement of loop structure in PHP
The do while loop statement of loop structure in PHP
The difference between while and do...while statements in loop structures in PHP
The for loop statement of loop structure in PHP
  PHP中循环结构之foreach循环语句 
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

Jul 12, 2016 am 08:49 AM
for loop

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结构语句 顺序结构 顺序结构就像一条直线,按着顺序一直往下执行。我们编写的代码默认都是...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use php to find odd numbers within 100 How to use php to find odd numbers within 100 Dec 23, 2022 pm 06:54 PM

Implementation steps: 1. Use the for statement control range to traverse the numbers from 1 to 100, the syntax is "for ($i = 1; $i <= 100; $i++) {loop body code}"; 2. In the loop body, Just use the if statement and the "%" operator to obtain and output odd numbers. The syntax is "if($i % 2 != 0){echo $i." ";}".

What is the execution order of for loop in PHP What is the execution order of for loop in PHP Sep 22, 2021 pm 06:24 PM

Execution sequence: 1. Execute the "initialization expression"; 2. Execute the "conditional judgment expression". If the value of the expression is true, execute the "loop body", otherwise the loop ends; 3. After executing the loop body, execute "Variable update expression"; 4. After the variable is updated, enter the next loop until the condition judgment value is false, ending the loop.

Does mysql have a for loop? Does mysql have a for loop? Mar 30, 2023 pm 08:26 PM

MySQL does not have a for loop. MySQL does not support for loop statements. It only supports three loop statements: WHILE, REPEAT and LOOP. MySQL provides loop statements, allowing you to repeatedly execute a SQL code block based on conditions.

JS loop learning: use of for loop statements (detailed examples) JS loop learning: use of for loop statements (detailed examples) Aug 03, 2022 pm 06:45 PM

In the previous article "JS Loop Learning: The Use of While Loop Statements (Detailed Examples)", we briefly learned about the while loop and the do while loop, and today we will introduce another kind of loop-the for loop statement. I hope it will be useful to everyone. Helped!

How to use for loop in Python How to use for loop in Python Oct 25, 2023 pm 12:18 PM

How to use the for loop in Python Python is a simple and easy-to-use programming language, and the for loop is one of the most commonly used tools. By using for loops, we can loop through a series of data, perform effective processing and operations, and improve the efficiency of the code. Below, I will introduce how to use the for loop in Python through specific code examples. Basic for loop syntax In Python, the syntax of a for loop is as follows: for variable in iterable object:

Handling large arrays in Go: use for range or for loop? Handling large arrays in Go: use for range or for loop? Jul 24, 2023 pm 02:47 PM

We know that Go’s syntax is relatively concise. It does not provide loop control syntax such as while, do...while, etc. supported by C, but only retains one statement, the for loop.

How to separate even and odd numbers in an array using for loop in C language? How to separate even and odd numbers in an array using for loop in C language? Aug 25, 2023 pm 03:09 PM

An array is a group of related data items stored under a single name. For example intStudent[30];//student is an array name, a collection of 30 data items containing a single variable name Operational search of array - used to find whether a specific element exists sorting - it helps to arrange the elements in the array in ascending order or descending sort. Traversal - It processes each element in the array sequentially. Insertion - It helps to insert elements in array. Delete - It helps in deleting elements from an array. elements in the array. The logic of finding even numbers in an array is as follows - for(i=0;i<size;i++){ if(a[i]%2==0){

How to use for loop to implement flip operation in Go language How to use for loop to implement flip operation in Go language Mar 24, 2024 pm 02:15 PM

Title: How to use for loops to implement flip operations in Go language. In Go language, you can easily flip data structures such as arrays and slices by using for loops. In this article, we will introduce how to use for loops to flip arrays and slices, and give specific code examples. Operation of flipping an array First, let's look at how to flip an array through a for loop. We define an array containing integer elements and flip it using a for loop. packagemain

See all articles