PHP while loop statement script and exampleWhile loop in PHP is very simple and similar to while loop in C programming. In PHP while loop the condition is checked at the beginning of the loop and the statement inside is executed till the end of the loop, then again the condition is checked and loop executed. Like this loop is continued till the condition became false and the loop is then stopped.
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?
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.
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 table
Here 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|