Loops



Cycles are used at times when more than one number of codes must be run. The code in an access field may need to be run multiple times. Listing items in ecommerce sites in order is an example of using a loop. Which turn to use is decided according to needs. The most common looping constructs in the PHP programming language are shown in the following table.

Loop Varieties
foreach The foreach structure makes it easy to iterate over arrays. foreach can only be used for arrays and objects ; with different data types or with an undefined variable.
while The while loop is the simplest loop type in PHP. The loop continues as long as the condition in the While loop is satisfied.
for The for loop is the most complex loop structure in PHP. 

 

 

# Foreach


Foreach cycle can only be used for series and objects. There are 2 types of syntax;

Standard Syntax

If an array does not contain any unspecified keys, that is, if it consists only of values, this syntax is used.

foreach( $dizi as $deger )
{
    # Kodlarınız
}
Array | Object $ array Looping array or object.
Int $ key The sequence number of the corresponding value of the index on each return of the current day.
Mixed $ Value The value of the index for each turn on the turn.
Use of
$sehirler = ['istanbul', 'izmir', 'ankara'];

foreach( $sehirler as $sehir )
{
    echo $sehir . "<br>";
}
istanbul
izmir
ankara
Key Word Syntax

If an array contains unspecified keys, that is, the key and values ​​are in pairs, this syntax is used.

foreach( $dizi as $anahtar => $deger )
{
    # Kodlarınız
}
Array | Object $ array Looping array or object.
Mixed $ key The corresponding key of the index on each turn of the turn.
Mixed $ Value The value of the index for each turn on the turn.
Use of
$sehirler = ['34' => 'istanbul', '35' => 'izmir', '06' => 'ankara'];

foreach( $sehirler as $plaka => $sehir )
{
    echo $plaka . ":" . $sehir . "<br>";
}
34: istanbul
35: izmir
06: ankara

In objets the usage logic is the same.

$sehirler = 
[
    (object) ['isim' => 'istanbul', 'plaka' => '34'],
    (object) ['isim' => 'izmir'   , 'plaka' => '35']
];

foreach( $sehirler as $sehir )
{
    echo $sehir->isim. ":" . $sehir->plaka . "<br>";
}
34: istanbul
35: izmir

 

 

# While


The while loop is the simplest loop type in PHP. The meaning of the loop is simple. while statement in $ ko şule true statement in the domain unless that PHP runs again.

Syntax

The following statement $ ko şule from expression rather than a true continues to rotate as long as the return cycle. Errors in the conditions of the cycles can cause the process to enter an infinite loop.

while( $kosul )
{
    # Kodlarınız
}
Bool $ conditions The loop continues to return as long as the condition is true .
Use of
$i = 1;

while( $i <= 3 )
{
    echo $i . "<br>";
    $i++;
}
1
2
3
$sayac = 1;
$deger = 'A';

while( $deger !== NULL )
{
    echo $sayac . "<br>";

    if( $sayac === 5 )
    {
        $deger = NULL;
    } 

    $sayac++;
}
1
2
3
4
5

 

 

# For Forward


The For loop can do both foreach and while loop operations alone. However, if you are working with arrays and objects, using a foreach loop will make your job much easier. Unlike the while loop, the For loop performs counting itself.

Syntax
for( $baslangic; $kosul; $degisim )
{
    # Kodlarınız
}
Int $ beginnings Specifies the number of counts to connect to.
Bool $ conditions The loop continues to return as long as the condition is true .
Int The change $ How much the $ starting value will increase or decrease in each turn of the turn .
Use of
for( $i = 1; $i <= 3; $i++ )
{
    echo $i . "<br>";
}
1
2
3
Note: The incrementing process is performed after the loop has been run once.