Table of Contents
Detailed explanation of PHP loop statements while, for, foreach, do while
Home Backend Development PHP Tutorial Detailed explanation of PHP loop statements while, for, foreach, do while_PHP tutorial

Detailed explanation of PHP loop statements while, for, foreach, do while_PHP tutorial

Jul 12, 2016 am 09:06 AM
statement

Detailed explanation of PHP loop statements while, for, foreach, do while

1. while loop
while(expression)
{
Loop body; // Repeatedly execute until the expression is false
}
Code:
$index = 1;
while ($index<5)
{
print "Number is {$index} ";
$index ;
}
Run result:
Number is 1
Number is 2
Number is 3
Number is 4
2. do while loop
do {
Loop body;//Repeatedly execute until the expression is false
} while(expression)
Code:
do {
$index ;
print "Number is {$index} ";
} while($index<0);
Run result:
Number is 1
There is a certain difference between the Do While loop statement and while. The difference between them is that do while will be executed first regardless of whether the condition is true, while while will only be executed once if it is true.
3. for loop
There are two types of loops depending on the loop conditions
One: counting loop (usually using for)
Another type: conditional loop (generally use while do-while)
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 statement is executed. It is used to change the conditions for the next loop judgment, such as adding one, etc. And 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 an example of "I won't dare to do it again" written using a for loop. It can be compared with the one using a while loop.
for ($i=1; $i<=10; $i ) {
echo "$i. I won’t dare anymore
";
}
?>
Run result:
1. I won’t dare anymore
2. I won’t dare anymore
3. I won’t dare anymore
4. I won’t dare anymore
5. I won’t dare anymore
6. I won’t dare anymore
7. I won’t dare anymore
8. I won’t dare anymore
9. I won’t dare anymore
10. I won’t dare anymore
4. foreach loop
The foreach statement is used to loop through an array. Each time the loop is executed, the value of the current array element will be assigned to the value variable (the array pointer will move one by one) - and so on
Syntax:
foreach (array as value)
{
code to be executed;
}
Code:
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . " ";
}
?>
Run result:
Value: one
Value: two
Value: three
The above are the four loop bodies in PHP. Choose the corresponding loop body to use according to different conditions

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1065651.htmlTechArticleDetailed explanation of PHP loop statements while, for, foreach, do while 1. while loop while (expression) { loop body ;//Execute repeatedly until the expression is false} Code: $index = 1; while ($index5) {...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

multi-catch statement in PHP8.0 multi-catch statement in PHP8.0 May 14, 2023 pm 01:51 PM

With the development of web applications, PHP language has been widely used in web development. In the PHP8.0 version, a new language feature was introduced - the multi-catch statement. What is a multi-catch statement? In previous PHP versions, developers needed to write multiple catch statements to handle multiple exception types. For example, the following code block shows the handling of two different exceptions: try{//Somecodethatmay

How to implement the statement of inserting data in MySQL? How to implement the statement of inserting data in MySQL? Nov 08, 2023 am 11:48 AM

How to implement the statement of inserting data in MySQL? When using a MySQL database, inserting data is a very basic and common operation. By inserting data, new records can be added to database tables to provide support for business operations. This article will introduce how to use the INSERT statement in MySQL to implement data insertion operations, and provide specific code examples. The INSERT statement in MySQL is used to insert new records into the database table. Its basic syntax format is as follows: INSERTINTOt

Master the types of Python flow control statements and learn from scratch! Master the types of Python flow control statements and learn from scratch! Jan 20, 2024 am 09:02 AM

To learn Python from scratch, first understand the types of flow control statements! Python is a simple and powerful programming language that is widely used in data analysis, artificial intelligence, network development and various scientific computing fields. As a beginner, it is very important to master basic flow control statements, because they are the basis for realizing logical judgment and controlling the program execution flow. In Python, there are three main types of flow control statements: sequential structures, conditional structures and loop structures. The following will introduce these three process control statements in detail and give the corresponding

To understand flow control statements in Python, you need to master several situations To understand flow control statements in Python, you need to master several situations Jan 20, 2024 am 08:06 AM

Python is a widely used high-level programming language. It is easy to learn, efficient and flexible, and is deeply loved by developers. In Python, flow control statements are an important part of implementing program logic. This article will introduce commonly used flow control statements in Python and provide code examples to deepen understanding. In Python, common flow control statements include conditional statements and loop statements. Conditional statements execute different code blocks based on the true or false condition and are used to determine and select execution branches. The loop statement is used to repeat

C# try statement usage C# try statement usage Feb 22, 2024 pm 12:45 PM

The usage of try statement in C# requires specific code examples. C# is an object-oriented programming language, in which the try statement is a structure used to capture and handle exceptions. Through the try statement, we can write code to handle exceptions that may occur, thereby improving the stability and reliability of the program. In this article, we will introduce the usage of try statement in C# and provide some specific code examples to help readers understand. In C#, the try statement consists of try block, catch block and optionalfina

How to implement the statement to unlock the table in MySQL? How to implement the statement to unlock the table in MySQL? Nov 08, 2023 pm 06:28 PM

How to implement the statement to unlock the table in MySQL? In MySQL, table locks are a commonly used locking mechanism used to protect data integrity and consistency. When a transaction is reading and writing to a table, other transactions cannot modify the table. This locking mechanism ensures data consistency to a certain extent, but may also cause other transactions to be blocked. Therefore, if a transaction cannot continue for some reason, we need to manually unlock the table so that other transactions can continue. MySQL provides a variety of

How to implement the statement to change user password in MySQL? How to implement the statement to change user password in MySQL? Nov 08, 2023 am 09:05 AM

MySQL is a commonly used relational database system used to manage and store data. In MySQL, user passwords are one of the important factors in protecting database security. In daily management of the database, it is often necessary to change the user's password to ensure the security of the database. So, how to implement the statement of changing user password in MySQL? This article will provide you with specific code examples. Change the MySQL user password through the ALTERUSER statement. The ALTERUSER statement is MySQL8.0 and above.

The purpose of SQL ALTER statement The purpose of SQL ALTER statement Feb 19, 2024 pm 05:01 PM

The function of the SQL ALTER statement requires specific code examples. In a database management system, the ALTER statement is a SQL command used to modify database objects. Through the ALTER statement, we can modify database objects such as tables, columns, indexes, and views, including adding, deleting, modifying, and other operations. The following will introduce the common usage of the ALTER statement in detail and provide relevant code examples. ALTERTABLE statement is used to modify the structure of the table. You can add, delete, modify columns, constraints, indexes, etc.

See all articles