PHP Loop Learning 1: Use of while loop statements (detailed examples)

青灯夜游
Release: 2023-04-10 12:18:02
Original
4518 people have browsed it

In the previous article "PHP Array Learning: Talking about Several Methods of Outputting Arrays", we took the foreach statement as an example and briefly mentioned the loop statement. Next, we will learn about the loop control statements in PHP through a series of articles, ensuring that you will understand them at a glance. Come and learn together!

This article will first introduce to you the simplest loop statement in PHP - while loop statement, analyze the syntax format and execution process, and learn how to use it through code examples. Subsequent articles will gradually deepen the difficulty and understand other loop statements. (Video tutorial attached: Enter the world of PHP from 0)

The while loop statement is a when typeloop statement. The loop condition is first judged, and when If the condition is met, the loop body will be executed, and if it is not met, it will stop.

Function: Repeat an operation until the specified condition is not true. (Attached manual: PHP While Loop)

Features: Judge the expression first, and execute the corresponding statement when the expression result is true.

The general form of while loop is:

while(表达式){    //表达式为循环条件
    PHP语句块;  //PHP语句块为循环体
}
Copy after login

Statement analysis:

  • First calculate the value of "expression", When the value is true, execute the "PHP statement block" in the loop body;

    Description: The calculation result of "expression" is of Boolean type (TRUE or FALSE). If it is a value of other types It will also be automatically converted to a Boolean value (because PHP is a weak language type and will automatically convert the variable into the correct data type based on the value of the variable).

    "Statement block" is a collection of one or more statements surrounded by { }; if there is only one statement in the statement block, you can also omit { }.

  • After the execution is completed, return to the expression and calculate the value of the expression again for judgment. When the expression value is true, continue to execute the "statement block" ...This process will be repeated

  • until the value of the expression is false before jumping out of the loop and executing the statement below while.

The flow chart of the while statement is as follows:

PHP Loop Learning 1: Use of while loop statements (detailed examples)

Usually the "expression" is using comparison Value calculated by operator or logical operator

Example: Calculate the value of 1 added to 100

<?php
header("Content-type:text/html;charset=utf-8");
$i = 1;
$sum=0;
while($i<=100){
    $sum+=$i;
    $i++;
}
echo "1加到100的值为:".$sum ;
?>
Copy after login

Output result:

1加到100的值为:5050
Copy after login

Note: "Infinite Loop" - a loop that cannot be terminated by its own control

If the expression is always true and the loop condition is always true, the while loop will continue to execute forever. If it does not end, it will become an "infinite loop"

<?php
$i=1;
while($i){
    echo $i;
}
?>
Copy after login

After running the program, the value of the variable $i will be output until the user forces it to close.

Recommendation: 2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of PHP Loop Learning 1: Use of while loop statements (detailed examples). For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!