Mệnh đề For Loops

Mệnh đề For Loops

Cú pháp :

for (giá trị đầu; điều kiện; giá trị cuối) {
    lệnh;
}

Ví dụ:

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>

Kết quả:

The number is: 0 
The number is: 1 
The number is: 2 
The number is: 3 
The number is: 4 
The number is: 5 
The number is: 6 
The number is: 7 
The number is: 8 
The number is: 9 
The number is: 10 

Mệnh đề Foreach Loop

Cú pháp:

foreach ([mảng] as [giá trị]) {
    lệnh;
}

Ví dụ:

<!DOCTYPE html>
<html>
<body>

<?php  
$colors = 
array("red""green""blue""yellow"); 

foreach ($colors as $value) {
  
echo "$value <br>";
}
?>  

</body>
</html>

Kết quả:

 

red 
green 
blue 
yellow