- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP while Loop
The while loop in PHP, is used when we need to execute some block of code, multiple times. For example:
<?php $x = 1; while($x<=10) { echo $x, "<BR>"; $x++; } ?>
The output produced by above PHP example, is:
PHP while Loop Syntax
The syntax to use while loop in PHP, is:
while(condition) { block of code; }
The block of code; will continue its execution, until the condition evaluates to be false.
PHP while Loop Example
The following example on while loop in PHP, is created along with its output and in-depth description:
<?php $num = 2; while($num<=20) { echo $num, "<BR>"; $num = $num+2; } ?>
The output of above PHP example, is shown in the snapshot given below:
Note - Whatever the expression inside the while() will be considered as a conditional expression. After evaluating a conditional expression, a boolean value (true or false) gets returned. If the boolean value is true, then execution of while loop continues, otherwise if the conditional expression evaluates to be false, then the execution of while loop stops.
The dry run of above example, goes like:
- A variable $num is defined with its value as 2
- Now the execution of 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, that is 2, along with a line break, will be printed on the output
- Now 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 has been executed, therefore program flow again goes to the conditional expression of the while loop, to check the condition again
- 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, then the condition $num<=20 evaluates to be false, and the execution of while loop stops
Let me create another example on 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 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
« Previous Tutorial Next Tutorial »