Array in for loop (Information of Students)
Arrays: Imagine an array as a list that holds multiple items. Each item has an index (like a position number) for easy access.
for Loop: A for loop is a programming construct that allows you to repeat a block of code a specific number of times.
Putting them together:
PHP Code
$fruits = [“apple”, “banana”, “orange”]; // Our array of fruits
for ($i = 0; $i < count($fruits); $i++) {
echo “I love to eat ” . $fruits[$i] . “!\n”; // Loop through each fruit
}
In this example:
- $fruits is the array containing our fruit list.
- The for loop iterates through the array.
- $i acts as a counter, starting at 0 (first index).
- The loop continues as long as $i is less than the number of items in the array (count($fruits)).
- Inside the loop, we access each fruit using its index $i and print a message.
Here’s another example of multiple entries with a for loop array.
Output
This is a basic example, but for loops can be powerful tools for processing elements within arrays in PHP