[Basic PHP] Loop Types (Loop)
When it comes to programming languages, an extremely important feature is indispensable for an application to run completely, which is loop. And while programming with WordPress in particular, you also use loops a lot. For example, you have an array containing 10 article list objects, and if you want to get all those objects out, you will have to use a loop.
What is a loop roughly?
Loop you can understand it is a feature that creates a cycle of executing a certain script with a programming language. For example, instead of having to manually enter numbers from 1 to 1000, you can use a loop so that it automatically prints from 1 to 1000. This is an extremely important technique so I want you to learn the most. , not only in this article but also refer to other articles about loops on the internet.
Loop Types in PHP
In PHP, we can currently use 4 different types of loops: for(), while() and foreach(). Each type of loop will work differently, specifically:
for– Repeat an action with a certain number of repetitions. For example, if you want to print from 1 to 1000, you will use this loop because we can set a condition for it to stop after iterating 1000 times.
while– Repeat an action based on a specific condition that it returns true. For example, in WordPress, the have_posts() function will have a function to check in the query and the object is not, if it returns true then loop, false then stop.
do while – This loop is similar to while, but you can put a set of codes in the do() function and it will repeat these codes based on a certain condition.
foreach– Used to iterate over keys and values in an array data. This is also used a lot when working with arrays.
I used to learn through PHP, so I know that if you are new to programming, the concept of loop seems very vague. But don't worry if you don't understand, just read and follow and bookmark this page. A few weeks later there is a series of Advanced WordPress Learning you will understand better.
Using a for . loop
To use loop for, we will have the following code structure:
<?php
for ($x = 0; $x <= 20; $x++) {
echo "$x <br>";
}
?>
Inside:
$x = 0 : The data needs to be repeated, because it doesn't have a value yet, so I assign it to 0.
$x <= 20 : Loop condition, this example means it will loop until $x is less than or equal to 20.
$x++ : The operator counts when the loop executes, $x++ it's the same as $x + 1, i.e. increments by 1 value each iteration cycle.
And this loop we rarely use in WordPress.
Using while . loop
The while loop is used a lot in WordPress, especially to loop a query to display post data outside. Usage is as follows:
<?php
while ( [condition] ) {
// code executes in the loop
}
?>
For example, I want to loop until $xless than or equal 20to the following:
<?php
$x = 1;
while ( $x <= 20) {
$x++;
echo "$x <br>";
}
?>
Using a do…while . loop
This loop is similar to while, but it will execute the code first and then check the following condition.
<?php
$x = 0;
do {
$x++;
echo "$x <br>";
} while ( $x <= 20 )
?>
Using a foreach loop
This loop we will iterate the values and keys in the array, we do not have any conditions or number of iterations, but it will loop when the array is exhausted.
<?php
$web = array(
‘PHP’, ‘ASP.NET’, ‘Ruby on Rail’, ‘CSS’, ‘HTML’, ‘Java’
);
foreach( $web as $lang ) {
echo "$lang <br>";
}
?>
In it, $web as $langthat is, it will take the key and value pairs in the array and $webput it in $langto display it.
Or if you have a non-sequential array and want to display key and value pairs, do the following:
<?php
$web = array(
‘dynamic’ => ‘PHP’,
‘styling’ => ‘CSS’,
‘behavior’ => ‘Javascript’
);
foreach ( $web as $key => $value ) {
echo "$key is $value <br>";
}
?>
Epilogue
Above is what you need to know about loops in PHP. Of course, the examples above are all very simple, but when you actually work, you will automatically understand more deeply, if you already have the basic foundation. Therefore, you should master the above basic knowledge and later when you work, everything will be much easier.