PHP for Beginners: Loops
PHP Developer Roadmap: Part 2
In the previous entry, we covered several topics, from the basic syntax up to the PHP conditionals. This is the next entry.
Loops
The next item you should be familiar with in PHP is Loops. Unlike conditionals, loops are code blocks that are called to run over and over as long as a certain set of conditions are met.
PHP uses the following types of loops:
- while loops
- do…while loops
- for Loops
- foreach loops
PHP while Loop
The PHP while loop executes a block of code for as long as a specified condition holds true. It is the simplest type of loop. A while loop should have an initializer, a code that checks if a condition is true, the body of the loop, and an increment when it is required. An example is shown below;
<?php
$a = 5;
while ($a <= 30){
echo "The value is below the limit <br>";
$a+= 5;}
?>
In the above loop, $a initializes the value of a to 5, while ($a ≤ 30) sets the condition to be met, the echo states what’s to be done if the condition is true, and $a+= 5 is incremental that will increase the value of a by five. If we wanted to increase the value of each loop by one, we would just write $a++.
PHP do…while Loop
This loop is different from the while loop, in that it first executes a block of code once before checking the condition. If the condition is true, it will run the same code once again and then check the condition…and so on. So the condition is always tested after executing the block of code. An example is shown below;
<?php
$a = 5;
do {
echo "The value: $a is less than or equal to 30 <br>";$a+= 5;
} while ($a <= 30);
?>
PHP for Loop
For loop goes through a block of code a specified number of times. When using this loop, you should know in advance the number of times the loop should run.
Get Langat Langs’s stories in your inbox
Join Medium for free to get updates from this writer.
The parameters for this loop are three; Initializer, to initialize the value of the loop counter. The next parameter is the test counter, which evaluates the code for each iteration. If the evaluation is TRUE, the loop continues to the next iteration, but if it evaluates to FALSE, the loop ends. The last parameter is the loop increment value, which specifies by what value the loop iteration should increase by. An example of the for loop is shown below.
<?phpfor ($a = 5; $a <= 30; $a+= 5){
echo "The value $a is less than or equal to 30 <br>";
}
?>
PHP foreach Loop
This loop is slightly different in that it only works on arrays and objects, and will throw an error when you try to use it on other data types. It has two syntaxes:
<?php
foreach(iterable_expression as $value)
Statement
?><?php
$cars = array("BMW", "Toyota", "Merc", "Ford", "Mobius");foreach ($cars as $value){
echo "$value <br>";
}
?>
In the above syntax, the value of the current expression is assigned to $value on each iteration.
The other syntax is shown below;
<?php
foreach (iterable_expression as $key => $value)
statement
?>
<?php
$cars = array("BMW"=>"2013", "Toyota" => "2016", "Merc"=> "2018","Ford"=> "2011", "Mobius"=> "2019");foreach($cars as $brand => $value){
echo "The $brand was manufactured in the year $value<br>";
}
?>
In the above syntax, the current element’s key is added to the $key on each iteration. It should be noted that foreach does not modify an array pointer.
PHP Break and Continue
Break is used to jump out of a loop or a switch statement.
<?phpfor ($a = 5; $a <= 30; $a+= 5){if ($a == 15){
break;
}
echo "The value $a is less than or equal to 30 <br>";
}
?>
Continue on the other hand breaks one iteration in a loop when a condition is met, then continues with the next iteration, as shown below;
<?phpfor ($a = 5; $a <= 30; $a+= 5){if ($a == 15){
continue;
}
echo "The value $a is less than or equal to 30 <br>";
}
?>
Look out for Part 3.







