To access array elements in PHP, you can use the indexing syntax with square brackets.
In this example,
$numbers
with integer values.2
using the indexing syntax $numbers[2]
.<?php
$numbers = [1, 2, 3, 4, 5];
echo "Element at index 2: " . $numbers[2];
?>
Element at index 2: 3
In this example,
$fruits
with string values.<?php
$fruits = ['Apple', 'Banana', 'Orange'];
foreach ($fruits as $fruit) {
echo $fruit . "\n";
}
?>
Apple Banana Orange
In this tutorial, we learned How to Access Array Elements in PHP language with well detailed examples.