


Detailed explanation of several methods of PHP loop control statements_PHP tutorial
In PHP, loop statements generally use while, for, and foreach, and control statements are if swicth. Let me introduce to you how to use PHP loop control statements.
1. There are three structures of if..else loops. The first one only uses the if condition and treats it as a simple judgment.
Interpreted as "what to do if something happens".
The syntax is as follows: if (expr) {statement} where expr is the condition for judgment. Logical operation symbols are usually used as the condition for judgment.
The statement is the execution part of the program that meets the conditions. If the program has only one line, the curly brackets {} can be omitted.
Example: This example omits the curly braces.
The code is as follows | Copy code |
代码如下 | 复制代码 |
if($state==1) echo"哈哈"; ?> |
echo "Haha";
?>
代码如下 | 复制代码 |
if($state==1){ echo"哈哈; echo" "; } ?> |
Example: The execution part of this example has three lines, and the curly brackets cannot be omitted.
The code is as follows | Copy code | ||||||||
if($state==1){
echo"
|
The second type is to add an else condition in addition to if, which can be interpreted as "what to do if something happens, otherwise how to solve it."
The syntax is as follows:
The code is as follows | Copy code | ||||
if(expr){
statement1
|
The code is as follows | Copy code |
";
}else{
echo "Hehe";
echo" "; } ?> |
The code is as follows | Copy code |
if(\a>$b){ echo "a is bigger than b"; }elseif($a==$b){ echo "a equals b"; }else{ echo "a is smaller than b"; } ?> |
The above example only uses a two-level if..else loop to compare two variables a and b.
When actually using this kind of recursive if..else loop, please use it with caution, because too many levels of loops can easily cause problems in the design logic, or if there are too few braces, etc., it will cause inexplicable problems in the program.
2. There is only one type of for loop with no changes. Its syntax is as follows
for(expr1;expr2;expr3){statement}
where expr1 is the initial value of the condition.
expr2 is the condition for judgment, and logical operators are usually used as the condition for judgment.
expr3 is the part to be executed after executing the statement. It is used to change the conditions for the next loop judgment, such as adding one, etc.
The statement is the execution part of the program that meets the conditions. If the program has only one line, the curly brackets {} can be omitted.
The following example is written using a for loop.
The code is as follows | Copy code |
代码如下 | 复制代码 |
for($i=1;$i<=10;$i ){ echo"这是第".$i."次循环 "; } ?> |
){
echo"This is the ".$i."th loop
";
代码如下 | 复制代码 |
switch(expr){ caseexpr1: statement1; break; caseexpr2: statement2; break; default: statementN; break; } |
3. The switch loop usually handles compound conditional judgments. Each sub-condition is part of the case instruction.
In practice, if many similar if instructions are used, they can be synthesized into a switch loop. The syntax is as follows
The code is as follows | Copy code | ||||
switch(expr){
caseexpr1:
break;
break;
} |
代码如下 | 复制代码 |
While循环是php中最简单的循环语句,他的语法格式是: While (expression){ statement; } |
The code is as follows | Copy code |
switch(date("D")){<🎜> case"Mon":<🎜> echo "Today is Monday";<🎜> break;<🎜> case"Tue":<🎜> echo "Today is Tuesday";<🎜> break;<🎜> case"Wed":<🎜> echo "Today is Wednesday";<🎜> break;<🎜> case"Thu":<🎜> echo "Today is Thursday";<🎜> break;<🎜> case"Fri":<🎜> echo "Today is Friday";<🎜> break;<🎜> default:<🎜> echo "I have a holiday today";<🎜> break;<🎜> }<🎜> ?> |
The code is as follows | Copy code |
While loop is the simplest loop statement in PHP , his grammatical format is: While (expression){ statement; } |
When the value of expression is true, the statement statement will be executed. After the execution is completed, it will return to expression to continue judgment. The loop is not broken out until the expression evaluates to false.
Example:
The code is as follows
|
Copy code
| ||||||||||||||||||||||||
$num = 1;
$str = "Even numbers within 10 are:"; while($num <=10){ if($num % 2 == 0){ $str.=$num.””; } $num++; } echo $str; ?>
Do{
statement;
|

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
