How to Install Operating System ??Complete Guide Installing The Operating System

Repeat Logic Structure in PHP Discussion for Beginners

Discuss the repetition of the logic structure in PHP for beginners, so every programming language must have loops, especially loops in php. The most mandatory thing to master in programming languages is looping. Many algorithms require repetition or repetition as a realization of algorithm facilities such as using arrays, searching, sorting and so on.

Before going forward, there are a number of articles that you might be looking for.

READ TOO :

The repetition of the logic structure in PHP, there are several looping methods in PHP, such as for, while, do while, and for each. From each repetition method in different PHP each. The previous loop type is usually used to break arrays, but the array itself can be simplified with all repetitions, returning to each need.

Basics of Repetition in PHP

As the name suggests looping or repetition is used to make it easier to do the process repeatedly according to the request. A simple example is, it is agreed that we sort numbers 1 to 100, so the easiest thing in programming is using looping, that is, the algorithm adds a value of 1 to each repeated number. For example, starting from number 1, plus 1 then it produces number 2 and exceeds reaching 100. What if it is not repeated? Time takes time, energy and other hassles.

As the beginning of the discussion, there are several types of repetition or looping methods on PHP that can be used, let’s peel one by one using the PHP programming language.

Repetition Writing for PHP

Repetition to be used to repeat whenever we want. The use of loop for PHP is to determine the number or value that you want to repeat “variable = value; variable <limit; ++ variable “, a simple example:

<?php
for ($i = 1; $i <= 9; $i ++)
{
echo $i. '<br />';
}
?>

From the looping code for php above it will print numbers 1-9, like the picture below:

Repeat Logic Structure in PHP
Repeat Logic Structure in PHP

How that can happen, along with the explanation:

1. $i = 1

This $i variable has a value of 1

2. $i <= 9

Then here we will do the variable repeat boundary $i by using the comparison operator on PHP

3. $i++

Next, this is where the value of $i will change by adding each number starting from 1 plus 1 using the post-increment method, but the first loop will not occur with the addition process and number 1 will still be printed as number 1.

While Repetition Writing in PHP

The while loop is similar to for, but to determine the condition only by using a comparison operator, for example, $i <= limit, a simple example:

<?php
$i = 1;
while ($i <= 9)
{
    echo $i. '<br />';
    $ i ++;
}
?>

For the output, it is more or less the same as the explanation of the previous loop, namely looping using for. Where the browser will display numbers 1 – 9, a striking difference by using for is the condition not written in the loop function, and the process of adding numbers using the post-increment method written in the iteration process.

Writing Do-While Repetition in PHP

The fundamental difference between Do-While and While repetitions is that which is done by executing the statement first, after reading the conditions, for example, a simple progression:

<?php
$i = 1;
do {
     echo $i. '<br />';
     $i ++;
}
while ($i <= 9); 
?>

Actually what determines the calculation is the position of the post-increment operator ($i ++), that is, the variable will be printed first and then added the value 1. However, if the post-increment position ($i ++) is above the print command, the program will first add value one in the $i variable, for example:

<?php
$i = 1;
do {
    $i ++;
    echo $ i. '<br />';
}
while ($i <= 9);
?>

From the example of the transfer of the post-increment operator above it will print as shown below:

Repeat Logic Structure in PHP
Repeat Logic Structure in PHP

There is a change in process, that is, the value will increase first so it will produce numbers 2-10, different from before. Post-gradual increase.

For each Repeat Writing in PHP

This foreach loop type in PHP is the most different from the previous loop, ie Foreach looping in PHP is useful for breaking array contents, or more simply to simplify array values so that they can be read easily, here is an example of a pseudo-code:

Foreach (Array as Key => Value)

{
# Code …
}

Next, for more details, just write a program using Foreach in PHP.

Foreach Array Repeat Without Key:

<?php
$array = array ('PHP', 'Python', 'Ruby');
foreach ($array as $value)
{
    echo $value. '<br />';
}
?>

The Pseudo-code previously contained in Foreach is to use a key to retrieve the array key, but can only take the value, and will produce the output as shown below:

Repeat Logic Structure in PHP
Repeat Logic Structure in PHP

Foreach Array Repeat With Key:

<?php
$array = array ('PHP', 'Python', 'Ruby');
foreach ($ array as $ key => $ value)
{
     echo $key. 'is the key of'. value. '<br />';
}
?>

It can be seen that the $key variable is used to display the key of the array, different from the previous one, which is only by displaying the value. The simple example above will display the output as shown below:

Repeat Logic Structure in PHP
Repeat Logic Structure in PHP

Thus the looping tutorial on this PHP, from all the above loop types you can use one according to the needs of each program you want to make. Thank you!

Before proceeding, there are a number of articles that might be looking for.

ALSO READ :

Sending
User Review
0 (0 votes)

Add a Comment

Your email address will not be published. Required fields are marked *