PHP Loops - Part One
Hello There Pretty Brain ...
In this tutorial we'll be looking at PHP loops.
In particular, we'll be looking at how they can be used to iterate over data, as well as how they can help us execute some piece of code repeatedly until the desired result.
Let's first look at arrays, because understanding them; will help us in the proper comprehension of PHP loops as a programming concept. Well loops are mostly used on arrays, so this will be a very useful exercise.
Arrays
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Horse";
Do you see those values in the brackets, the zero and the one etc ($cars[0]
, $cars[1]
). Those are called array keys. Every value in a PHP array has a key. Whether you assign it or not, if there's a value in an array it'll have a key. Behold:
This is an array:
-
$car = array("Mazda", "Toyota", "Honda");
- or this :
$car = ["Mazda", "Toyota", "Honda"];
Yes both those babies are how we represent arrays in PHP. If you were to print this array variable you will see that even though you manually didn't specify the array keys; each value will have a key. So
$cars = [ 0 => "Mazda", 1 => "Toyota", 2 => "Honda"];
Yep they're always there; like the ever present mother in law, who's always bringing casserole and that weird green stuff. But don't you fret George; Arrays are gonna treat you well.
Also one thing to note before starting loops. None specified array keys always start at zero. like our arrays above, note that it's always :
$cars = [ 0 => "Mazda", 1 => "Toyota", 2 => "Honda"];
Okay now that I've drowned your youth in the tales of arrays and their pretty properties, let's finally start talking about those loops shall we?!!
We'll do them in the following order:
We'll show you what they look like, what each does, and then; much later we'll have you writing the code for yourself and testing it out on your machine.
For Loop
We'll start by showing you what it looks like, and discuss it's properties in detail.
for (init counter; test counter; increment counter) {
code to be executed;
}
This is a sample from the w3cschools website, and it's an awesome example of what the for loop looks like.
First there is the init counter; this will be a set integer from when the loop should start count.
Remember the arrays we were talking about earlier, this counter would be the key of an array. So if we wanted to start counting from the first value in the array, our for loop would look something like this:
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
'$x = 0'
ends after ten iterations
'$x = 10',
and incremets by one at every iteration
'$x++'
.
Please note that there isn't an array involved in this loop. We are essentially just starting the loop when we want, and stopping when we want to stop it.
We will get to iterating through an array in a little bit, don't you fret :)
In the mean time, please open your text editor/ide and create a new file in your localhost called; loops.php
Paste the follwing code in the loops.php
file and load the page in your browser.
If you have a bit of a problem creating a PHP file with the text editors, and placing it in the correct folders. Please read these tutorials
What is PHP?
Settign up your developemnt Environment
But if you're comfortable creating you own php files in your localhost, then by all means; let's code :)
<?php
/*
* These are comments - they are not part of the code
* We put these in our files to tell the reader a bit about the code
*
*/
// This is also a comment, it's not run
// Anything after these two slashes is a comment - not run!!
//$array = array('cape town', 'Durban', 'Johannesburg');
$array = ['Cape Town', 'Durban', 'Johannesburg'];
//For Loop Below
for($i = 0; $i < count($array); ++$i) {
echo $array[$i]." <br>";
}
?>
You know the drill soldier ... save, reload, and voila!
"Cape Town"
"Durban"
"Johannesburg" Within the for loop , you can do whatever you want , write what ever you want, do whatever you want to the variables Please note that the limit of the count is the size of the the array. So the loop goes till the count/size of the array.
"Within the for loop , you can do whatever you want , write what ever you want, do whatever you want to the variables"
The Village Geek
"Alright, alright, alright ..." - Matthew McConaughey …
We've learnt a lot in this tutorial; arrays, loops, and programming iterations in general. Please try and play around with this FOR loop and try and modify the variable, values, and arrays when doing so. Also try to change the increment values, starting values and the limits to see what happens.
In the next tutorial, we'll look at the FOREACH loop, the WHILE loop, and the DO WHILE Loop.
Enjoy!!
Stay you or be better.
Cape Town, South Africa