👨🏼‍💻 Part 6: Loops

Loops are something that are used for running repeated actions.

For example, if you want to print numbers from 1-100, you should never write 100 lines of code like this:

echo "1 <br>";
echo "2 <br>";
echo "3 <br>";
echo "4 <br>";
etc...

Instead, we use a simple loop for that.
I will show you two of the most common loops, with what you can achieve printing numbers from 1-100.

While-loop

We need to define the starting number, and then go inside the loop.

$number = 1; // define a starting number

// while number is smaller or equal to 100, repeat the follwing cycle
while($number <= 100) { 
	echo $number;
	echo "<br>"; // add an HTML linebreak after printing the number
	$number = $number+1;  // adds +1 to the current number, so the next loop cycle would be 1 number bigger
        
        // Alternatively, instead of $number = $number+1, you can also just write $number++, which will add +1 to the existing
        // value.
}

For-loop

The starting number and the finishing condition is written as the first line of the loop function.

for($number = 1; $number <= 100; $number++) {
        echo $number;
        echo "<br>";
}

When to use for-loop, when to use while-loop?

While loop is used when the number of iterations is unknown or depends on a condition. (Repeat the task until anything else equals something).
For loop is used when you know the exact number of times the code needs to be repeated. (Repeat the task until the number of defined iteration is reached).

For example, in Patchstack App, when you navigate to Sites view, it shows you all the sites that you have added under your account.
On the Sites page, a loop is being used.

Basic example in Patchstack App

If a users loads their Sites page in the Patchstack App, they will see all their sites they have added to their account.

The algorithm behind showing all users’ sites? Very generally, it’s this:

  1. Select all the sites from Patchstack database, that belong to the user with the same ID as the current logged in user ID
  2. Start a while-loop, to iterate through all these selected database rows
  3. Draw a new table row with HTML code; echo the site status, URL, groups, protection modules etc; end the table row with HTML code
  4. End the while loop

Independent exercise #1 – A boring image gallery

Resource: https://picsum.photos/200/300
Let’s use a random image API, which will return a random image URL each time you load that URL.
Refresh this URL and see if you can see different images?
Cool 😎

Let’s write our own random image gallery, using this API.
For that, we need to print an HTML <img> tag, with this Image API URL on our website for many of times:

So, an HTML code which will need to be repeated is this:

<img src='https://picsum.photos/200/300'>

Write a for-loop, which would echo this line of HTML repeatedly.
Refresh your PHP file and count, if you see the correct amount of images?

But isn’t that kinda boring?

Independent exercise #2 – A much better gallery

This image link does generate a new image with each page load, but if you load it in the same document, it won’t get randomized with each load.
Let’s see if we can fix this. For that, we need to read about their API a bit:
https://picsum.photos/
Scroll down, and find the “Advanced Usage” part.

What can you find from there?
We need to inject a ?random variable into that image URL there, so each time our loop iterates through the cycle, a new number is injected into that <img src URL.
Fix your loop, by adding your iteration variable value into that URL, and refresh the PHP file.

Hint: If you want to inject a variable into the URL, you need to escape from the string with the same marks that the string started with, so either double quotation marks ("), or single ones (').

Examples
echo "I want to print a PHP variable, so I will escape " . $somevariable . " and now I'm back in the string level";
echo "https://someurl.com?variable=" . $something;

Result should look like this:

Scroll to Top