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
The Php features we will be using are:
- “=” 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.
A1WEBSITEPRO Social Media Pages
Here are my social media pages, lets hook up!