How to traverse an array using a for loop in PHP?
In PHP, you can use a for loop to iterate through an array. Here is an example code of how to use a for loop to iterate through an array:
<?php
$numbers = array(1, 2, 3, 4, 5);
for ($i = 0; $i < count($numbers); $i++) {
echo $numbers[$i] . " ";
}
?>
Output:
1 2 3 4 5
In this example, we first define an array $numbers that contains numbers. Then, we use a for loop to iterate through the array. The loop condition is $i is less than the array length count($numbers). Inside the loop body, we access and output the elements in the array using the array index $i.