- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
while loop in PHP with examples
The "while" loop in PHP is used when we need to execute a block of code multiple times.
PHP while loop syntax
The syntax to use the "while" loop in PHP is as follows:
while(condition)
{
// body of the loop
}
The "body of the loop" will continue its execution until the "condition" is evaluated to be false. For example:
<?php $x = 1; while($x<=10) { echo $x, "<BR>"; $x++; } ?>
The output produced by the above PHP example is:
PHP while loop example
The following PHP code can be considered an example of the "while" loop.
<?php $num = 2; while($num<=20) { echo $num, "<BR>"; $num = $num+2; } ?>
The output of the above PHP example is shown in the snapshot given below:
Note: Whatever the expression inside of while() is, it will be considered a conditional expression. After evaluating a conditional expression, a boolean value (true or false) gets returned. If the boolean value is true, then the execution of the while loop continues; otherwise, if the conditional expression evaluates to be false, then the execution of the while loop stops.
The dry run for the preceding example is as follows:
- A variable $num is defined with its value as 2.
- Now the execution of the while loop begins.
- Since at first the condition $num<=20 or 2<=20 evaluates to be true.
- Therefore, program flow goes inside the loop.
- Using the statement:
echo $num, "<BR>";
- The value of $num, which is 2, along with a line break, will be printed on the output.
- Now for the second statement of the loop, that is:
$num = $num+2;
gets executed - Therefore, $num+2 or 2+2 or 4 will be initialized to $num. Now $num = 4.
- As all the statements inside the loop have been executed, the program flow again goes to the conditional expression of the while loop to check the condition again.
- The second time, the condition $num<=20 or 4<=20 evaluates to be true again.
- Therefore, program flow again goes inside the loop.
- Now the similar process goes again, from step no. 5 to step no. 9, with of course the new value of $num.
- This process continues until the value of $num becomes greater than 20.
- Because when the value of $num becomes greater than 20, the condition $num<=20 evaluates to be false, and the execution of the while loop stops.
Let me create another example using the while loop in PHP:
<?php $num = 5; $i = 1; echo "<p>----Table of $num----</p>"; while($i<=10) { echo "$num * $i = ", $num*$i, "<BR>"; $i++; } ?>
Now the output should be:
Following is one last example of a while loop in PHP:
<?php $sum = 0; $count = 0; $check = true; while($check) { if($sum === 500) $check = false; else $sum += 10; $count++; } $count--; echo "<p>Value of \$sum = ", $sum, "</p>"; echo "<p>Number of times, loop executed = ", $count, "</p>"; ?>
The output should be:
Value of $sum = 500 Number of times, loop executed = 50
Advantages of the while loop in PHP
- "while" loops are extremely flexible and can be used for a variety of tasks, including iterating over arrays and performing complex calculations.
- "while" loops can be more efficient than other types of loops, such as "for" loops, when the number of times the loop must execute is unknown in advance.
- "while" loops are simple to comprehend and employ, making them a good option for beginners or simple tasks.
Disadvantages of the while loop in PHP
- Risk associated with infinite loops: If the condition in a "while" loop is never false, the loop will continue to execute indefinitely, causing your script to crash or hang.
- Sometimes "while" loops can become overly complicated, especially when multiple conditions and nested loops are involved.
- "while" loops do not have built-in control over the number of iterations, so you must manually control the loop exit conditions, which can be difficult for complex tasks.
« Previous Tutorial Next Tutorial »