PHP Tutorial on While Loop with Stone River eLearning

Flow control statements are basically loops aimed at performing a particular set of operation for a period.  Writing condition for let’s say 100 integers knowing that the operation to be performed on each one of them is the same is both ineffective and time consuming.

Therefore we need a flow control statement that can customise our code and group all the elements together, and then write the operation to be performed for the group instead of doing it for individual elements.  To deal with the repetitive nature of any code or program, we can use either the ‘for loop’ or the while loop. However; if the operation to be performed in each case is the same, while loop is a better alternative than the ‘for loop’. 

There are quite a few interesting points to note about the while loop. First and foremost, a while loop provides a condition and as long as that condition holds true, performs a specified operation. That sounds very familiar to IF statement or the FOR statement, isn’t it? The while loop is essentially used to optimize a given code and to ensure the perfect use of flow control statements in it, therefore it has a few similarities to both, the IF statement and the FOR statement. 

Eg:

<?php
	$x=0; //Variable declaration
	while($x<=10) //While loop initialised and condition provided
	{
		echo 'The number '.$x.' is smaller than our required value<br>';   
		/* An echo statement is printed every time the condition holds true. */
		$x++; // The value inside the variable $x is incremented
	}
?>

As it is quite clear in the example given above, the while loop performs the same operation upon a variable until a given condition holds true.  The while keyword is used to initiate a while loop and the condition to be checked is included inside closed parenthesis, followed by the operation to be performed, which is included inside closed brackets. 

Note:

The condition included inside the while statement can be more than one with each separated by operators for comparison, such as ‘&&’ or ||.

The statement $x++ is the same as $x= $x+1 and must be included inside the while loop to ensure that the operation does not turn into an infinite loop.

We can also decrement the value of the variable in the loop according to the requirement of the problem and the values desired on the webpage.

There is another way of performing the while operation on any variable. Instead of including the operation to be performed inside the curly brackets, we can also use the endwhile keyword to specify the end of any while loop.  The syntax of a while loop involving endwhile is slightly different from the conventional while loop, as explained in the example below.

Eg:

<?php
	$num = 10; // Variable is initialised
	while($num>=0) : // Note that the while statement is followed by a colon (:) sign
		echo $num.'<br>'; //The operation is written without the curly brackets
		$num–;
	endwhile;             
	/*endwhile keyword marks the end of the while loop and eliminates the use of brackets */
?>

It is important to note that the use of endwhile keyword can only give desired results if the colon sign is used after the while statement. Programmers around the globe are more comfortable with the curly brackets as it stands for all the other flow control statements as well. 

Just like the while loop, there is another flow control statement, namely the do while loop that follows the same rules with a slight modification in the syntax.  As we just saw, the WHILE loop provides the condition first and then the operation to be performed; DO..WHILE on the other hand employs an exactly opposite approach. In a DO…WHILE loop, the operation to be performed is given first followed by the condition at the end of every loop.  

Along with its utility in simple PHP codes, ‘While loop’ is used extensively in cases where a particular data is to be fetched from a database.  Names of people above a certain age or other related data can be fetched with ease and accuracy from a big database using the while loop.

Online Technology Courses Stone River eLearning

GET YOUR FREE PHP EBOOK!

Comments