In this tutorial, you shall learn how to get the first element in given array in PHP using array[index] notation, with the help of example programs.
PHP – Get first element in array
To get first element in array in PHP, we can use array index notation.
The expression to get the first element in array arr
is
</>
Copy
$arr[0]
index=0 represents the first element in the array.
Example
In this example, we take an array arr
with some elements. We get the first element in the array and print it to output.
PHP Program
</>
Copy
<?php
$arr = ["apple", "banana", "cherry"];
$first_element = $arr[0];
echo $first_element;
?>
Output
Conclusion
In this PHP Tutorial, we learned how to get the first element in array using array indexing notation.