Home  >  Article  >  Backend Development  >  PHP While Loop

PHP While Loop

WBOY
WBOYOriginal
2024-08-29 12:41:39800browse

The Loop is used in any programming language is to run the same lines of code or block of statements multiple times based on the expected output. The number of times the block of code should be executed is specified in the Loop so as to exit from the loop. Just like most of the programming languages use different Loops, PHP also supports looping and basically has four types of Loop:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  1. For Loop
  2. While Loop
  3. Do-while Loop
  4. For-each Loop

These Loops generally differ in the syntax and the way they execute.

Example: In the Loop, we have to specify the number of times the block of code will execute at the beginning, whereas in a while Loop, we generally specify the condition, and the block of conditions mentioned under it are to be executed until the condition mentioned holds true. Now, if we take the case of a do-while Loop, in the do-while Loop, the condition is checked at the end of the lines of code, so even if the condition holds false, the do-while loop will be operated at least once. However, in the for-each loop, the block of code is run for every element present in the array until there are no more elements left in that array.

What is While Loop?

The While Loop is often referred to as an entry control Loop. This is because of the fact that the code written inside the While Loop will be executed strictly as long as the test expression given at the beginning of the While Loop is true. If the test expression is true, the code executes. Now, in the second iteration, again, the condition is checked. If the condition mentioned still holds true, the code is executed for the second time. The same process will keep on happening until the test expression is false, and then we exit from the Loop.

Syntax

The syntax of the while loop is :

while (TEST CONDITION){
//block of statements
}
Note: The test condition is a boolean expression which is either true or false. The execution of the code block depends on the result of the output of this boolean expression.

When to Use PHP While Loops?

While Loops can be used at all times, there is a requirement to execute a block of code until the given condition is satisfied. While Loops are also quite popular when it comes to reading the records fetched from the database using some query.

Flowchart of the PHP While LOOP

The flow of execution in the while Loop is not very complex. It can be understood using a very basic flowchart which is shown below.

PHP While Loop

Examples of PHP While Loop

Consider a case to print the first 5 natural numbers using a While Loop.

Example #1

Code:

<html>
<body>
<?php
$i = 1;
while ( $i<=5)
{
echo($i. "<br>");
$i ++;
}
?>
</body>
</html>

Output:

PHP While Loop

This will print the first 10 natural numbers beginning from 1. Let us understand the functioning of the code. In the first run, the value of I (which is 1) is tested against 10 as mentioned in the condition for While Loop. As the test condition holds true, we enter into the loop, which prints 1 and then increments the value of the variable I by 1. Now the value of I becomes 2. The test condition is tested again similarly, leading to print of 2,3,4, and so on until the value of I becomes 10 by the addition of 1 every time the loop executes. As the value of I becomes 10, the condition of the while loop becomes 10=10, which is true. Now 10 is printed, and the value of I becomes 11. The condition of the while loop now fails as 11<= 10 is false, so we exit the Loop.

Example #2

Code:

 !(1==0 is false)
=> !(false)
=>  true
2) if i =0
=> !(0==0 is true)
=> !(true)
=>  false
*/
echo $i."
"; $i  = $i-5; } ?>

Output:

PHP While Loop

This time the condition checks if the variable i is not equal to 0. For all the cases where i is not equal to 0, the condition holds true, and as soon as the value of the variable i becomes equal to 0, the condition becomes false, and we exit from the loop. Also, we are decrementing the value of i by 5 each time we enter into the While Loop, so at first, the value 20 will be printed, then the condition being true 15 will be printed, then 10 and then 5. Now, the value of i will decrement by 5 and become 0, so we won’t be able to enter into the loop. Since the condition check is being performed while we are making an entry into the Loop, the while loop is also called an Entry control loop. So this means that in cases when the condition does not match the first time, the code inside the While Loop will not be executed even once.

Conclusion

The While Loop is an entry control loop, whereas the do-While Loop is an exit control loop. If the condition in the While Loop is not true, the code block inside the While Loop will not execute even once, whereas, in the do-While, Loop the block of code will execute at least once even if the condition does not hold true as the condition is tested at the end of the do-While Loop. The boolean condition in the While Loop will govern the flow of execution in the While Loop.

The above is the detailed content of PHP While Loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:PHP Do While LoopNext article:PHP Do While Loop