PHP while loop statement script and example![]()
Here if the condition became false inside the loop then also the loop continue to execute till the next time
the condition is checked. Here is one simple example of while loop.
What is the output of this?
Here we are checking the while condition at the starting of the loop. So the execution of the loop will be done only if condition is satisfied at the beginning. We can read on the difference between do while loop and while loop here.
Exit inside the loop by using breakPrematurely we can come out of the loop by using break statement. We used one if condition to check the value and then apply break statement.
Using NULLCheck this code
In above code the line echo "hi"; will not be executed as NULL is same as False. This concept we will be using in database record set where pointer will return NULL when there is no more record to return.However below code will print the output once, as we have used break; to come out of the loop after printing hi.
Display elements of an array by using While loopLet us declare one array with some elements. Then by using while loop we can display the key and value of the array.
The output is here
Create ( 2 to 10 )multiplication table using While loopTo generate multiplication tables we will start with 2 tableHere is the simple code
This will display 2 table like this
Now we will keep this loop inside another outer loop and change $i value from 2 to 10. We will add a line break after each looping.
Output is here
This article is written by plus2net.com team.
![]() | ||||||||
▼ More on PHP Basics: Loops
| ||||||||