Php Kod Örnekleri 4
While Döngü Örnekleri;
1
2
3
4
5
6
|
<?php
while ( koşul ) {
# işlemler
# değişkenin değerini artırma ya da azaltma
}
?>
|
1
2
3
4
5
6
7
|
<?php
$sayi = 0;
while ( $sayi < 10 ) {
echo $sayi . '<br>';
$sayi++; # değişkeni artırmayı unutmayın
}
?>
|
1
2
3
4
5
6
7
8
|
<?php
$sayi = $sonuc = 1;
while ( $sayi < 6 ) {
$sonuc *= $sayi;
$sayi++;
}
echo 'Sonuc = ' . $sonuc;
?>
|
For Döngüsü Örnekleri;
1
2
3
4
5
|
<?php
for ( döngü_başlangıcı; koşul; adım_miktarı) {
# işlemler
}
?>
|
1
2
3
4
5
6
7
8
9
|
<?php
$sayi = rand(1, 10);
$sonuc = 1;
for ( $i = 1; $i <= $sayi; $i++) {
$sonuc = $sonuc * $i;
}
echo $sayi . '! = '. $sonuc;
?>
|
Do While Döngü Örnekleri;
1
2
3
4
5
6
|
<?php
do {
# işlemler
# değişken değerini artırma ya da azaltma
} while ( koşul );
?>
|
1
2
3
4
5
6
7
|
<?php
$sayi = 10;
do {
echo 'Bir kez calisti.';
$sayi++;
} while ( $sayi < 5);
?>
|
Foreach Döngüsü Örnekleri;
1
2
3
4
5
|
<?php
foreach($dizi as $eleman) {
# işlemler
}
?>
|
1
2
3
4
5
6
|
<?php
$sinif = array('Ali', 'Mehmet', 'Didem');
foreach($sinif as $ogrenci) {
echo $ogrenci . '<br>';
}
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
for ($sayi = 0; $sayi < 10; $sayi++) {
# eğer değişken 4 ise döngüyü atlat
if($sayi == 4)
continue;
#eğer değişken 7 ise döngüden (tamamen) çık
if($sayi == 7)
break;
echo $sayi . '<br>';
}
?>
|
Leave a reply