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;
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. |
$sehirler = ['istanbul', 'izmir', 'ankara'];
foreach( $sehirler as $sehir )
{
echo $sehir . "<br>";
}
izmir
ankara
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. |
$sehirler = ['34' => 'istanbul', '35' => 'izmir', '06' => 'ankara'];
foreach( $sehirler as $plaka => $sehir )
{
echo $plaka . ":" . $sehir . "<br>";
}
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>";
}
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.
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 . |
$i = 1;
while( $i <= 3 )
{
echo $i . "<br>";
$i++;
}
2
3
$sayac = 1;
$deger = 'A';
while( $deger !== NULL )
{
echo $sayac . "<br>";
if( $sayac === 5 )
{
$deger = NULL;
}
$sayac++;
}
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.
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 . |
for( $i = 1; $i <= 3; $i++ )
{
echo $i . "<br>";
}
2
3