In a "Php 7 While Loop" you can repeat different information dynamically when you compare it to a variable. A While loop will execute when a condition is "TRUE" in other words "matches the variable". Here is what we are going to be learning in this lesson.
- While
- Do While
- "=" to set a Php variable
- "while()" A Php Loop
- "do{}while()" A Php Loop that executes a condition after the statement
- "<=" Less than or equal to Conditional Operator
- "++" Increment Operator, adds to a variable
Php 7 While Loop
In a Php 7 while loop we first need to set a variable. We are going to use this variable to compare conditions too. You will recall that we learned about "Conditional Statements" in a previous lesson. Here is an example of a simple "While Loop".$var=10; while($var <= 10 ){ echo 'The loop says '.$var.'; $var++; }The while loop will echo out the numbers in consecutive order. The loop will keep adding 1 because of the "increment" operator. When the loop reaches the number 10 it will stop because of the Conditional Statement "<=".
Php 7 Do While Loop
The "Php 7 Do While Loop" will execute a statement first then it will start incrementing the numbers. We will explain this further on the next page. Here is a simple way to write a "do while loop" in Php.$var=1; do{ echo ' The loops says '.$var.' '; $var++; }while($var <= 10 );continue on next page.
Compare While Loop And Do While Loop
If we were to compare the "while loop" with the "do while loop" you might be thinking that they are doing the same thing. Consider these 2 code examples.Do While Loop
In this example of the "do while loop" the conditional statement is less than or equal to zero. However, when you run the statement you will still get a result.$var=1; do{ echo ' The loops says '.$var; $var++; }while($var <= 0 );
While Loop Alone
If we use the "while loop alone" in the same manor we will not get any result. The conditional statement is once again set to zero in this example.$var=1; while($var <= 0 ){ echo 'The loop says '.$var; $var++; }When running this code example you will notice that you do not get any result whatsoever. Thank you for reading. If you would like to stay updated please share and subscribe. :)