Loops are another important factor in PHP and all programming languages. They can be used for a multitude of different things, including but not limited to skipping through arrays, searching for data, etc. The structure of a while loop is similar to the basic structure of if statements, which was explained in a tutorial earlier. The while loop structure is as follows: while a condition is true, do something. Please take a look at the below code.
<?php
$count = "1";
while($count !== "5") {
echo("The count has not yet reached 5.");
$count = $count + 1;
}
?>
The above code might be a bit confusing. First it initializes the variable “$count”. It makes “$count” equal to “1”. The while loop will keep repeating while the variable “$count” is not equal (“!==“) to “5”. During each loop, it outputs “The count has not yet reached 5“, and then adds “1” to the current count. So, after the first loop, “$count” is equal to “2”. After the second, it is equal to “3”, and so on. Once “$count” reaches “5”, the loop will stop.