PHP Do While Loop

PHP Do While Loop

PHP Do While Loop

PHP Do while loop, is one of the many types of conditional loop being use to step through or iterate through programs.Basically the meaning of do While loop is to run the program first and check the condition later, continue run if the condition is still true. Below list shows the situation where do while loop can be used.

  • You are required to run the loop at least one regardless of the conditions
  • Run through the iteration first and check Later

Refer the code below for the Do While Loop Sample


<?php

$n = 1;


do {
	
   echo "The current iteration now is".$n.'<BR>';
       $n++;
	
	}While($n <=10);
?>

Use Do While Loop to print out name where first character starting with ā€œEā€ and stop Printing when detect any 1st character other than E

The Do while Loop will print out the Name starting with E,
It will stop printing if it detects any string other than E.
It will use the substr function

<?php



$name=array("Edmund", "Eric", "Eddy ", "Edison ", "Aaron ");

$i=0;
do {
	
	
	echo $name[$i]. '<br>';
	
	$i++;
	
// substr will start checking the first Character of an Array  whether it 
// equal to E	
}while( substr($name[$i],0,1)=="E" );
      


?>


Result


Edmund
Eric
Eddy
Edison 



Check out PHP Switch Statement here

Check out PHP.net

Leave a Reply

Your email address will not be published. Required fields are marked *

eight āˆ’ 6 =