Here is an example of a PHP for loop:
<?php
// Initialize the loop counter
$i = 0;
// Loop while the counter is less than 10
while ($i < 10) {
// Print the counter value
echo $i;
// Increment the counter
$i++;
}
?>
This code will print the numbers from 0 to 9.
Here is another example of a PHP for loop:
PHP
<?php
// Initialize the loop counter and array
$i = 0;
$array = [1, 2, 3, 4, 5];
// Loop through the array
foreach ($array as $value) {
// Print the value
echo $value;
// Increment the counter
$i++;
}
?>
This code will print the values of the array, 1, 2, 3, 4, and 5.
For loops are a powerful tool for iterating through data. They can be used to print numbers, print the values of an array, or perform other tasks.
No comments:
Post a Comment