AgerNic.com/ro
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL
Tutorial

Tutorial PHP» PHP Loops ( bucla ) While, Do-While, For
În acest capitol ve?i înv??a cum s? repeta?i codul folosind bucle în PHP.

Bucle PHP
Adesea, atunci când scrie?i cod, dori?i ca acela?i bloc de cod s? fie difuzat de mai multe ori, de un anumit num?r de ori. Deci, în loc s? ad?ug?m mai multe linii de cod aproape egale într-un script, putem folosi bucle.

Buclele sunt utilizate pentru a executa acela?i bloc de cod din nou ?i din nou, atât timp cât o anumit? condi?ie este adev?rat?.

În PHP, avem urm?toarele tipuri de bucl?:

while - bucleaz? un bloc de cod atâta timp cât condi?ia specificat? este adev?rat?
do ... while - executa comanda o data, apoi se repet? bucla atâta timp cât condi?ia specificat? este adev?rat?
for - bucle printr-un bloc de cod un num?r specificat
foreach - bucle printr-un bloc de cod pentru fiecare element dintr-un tablou

PHP while - bucl?
Bucla while execut? un bloc de cod atâta timp cât condi?ia specificat? este adev?rat?.

Sintax?
while (starea este adev?rat?) {
cod de executat;
}

Exemple
Exemplul de mai jos afi?eaz? numerele de la 1 la 5:

<?php 
$x = 1;

while($x <=5) {
echo "The number is: $x <br>";
$x++;
}
?>

Exemplu explicat:
$ x = 1; - Ini?ializa?i contorul buclei ($ x) ?i seta?i valoarea de pornire la 1
$ x <= 5 - Continua?i bucla atâta timp cât x este mai mic? sau egal? cu 5
$ X ++; - M?ri?i valoarea contorului buclei cu 1 pentru fiecare itera?ie

Acest exemplu afiseaza pana la 100 cu zecile:

<?php 
$x =0;
while($x <= 100) {
echo "The number is: $x <br>";
$x+=10;
}
?>

PHP do... wile bucla - Bucleaz? printr-un bloc de cod o dat?, apoi repet? bucla atâta timp cât condi?ia specificat? este adev?rat?.
În timp ce bucla do ... while va executa întotdeauna blocul de cod o dat?, va verifica apoi starea ?i va repeta bucla în timp ce condi?ia specificat? este adev?rat?.

Sintax?
do {
cod de executat;
} while (condi?ia este adev?rat?);


Exemplul de mai jos stabile?te mai întâi o variabil? $x la 1 ($x = 1). Apoi, bucla do while while va scrie o anumit? ie?ire, apoi va cre?te variabila $x cu 1. Apoi, starea este bifat? (este $x mai mic? sau egal? cu 5?), Iar bucla va continua s? func?ioneze atâta timp cât $x este mai mic sau egal? cu 10:

<?php 
$x =1;
do { echo "The number is: $x <br>";
$x++;
} while ($x <=10);
?>
Tags: PHP While, Do-While, For & Foreach Loops - Curs online gratis

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


analytics piwika