find the first element in an array in php

To find the first element of an array in PHP, you can use the indexing notation with the index value of zero (0). Here's an example:

main.php
$array = array('apple', 'banana', 'cherry');

$first_element = $array[0];

echo $first_element; // Output: "apple"
115 chars
6 lines

In this example, the $array variable is an indexed array with three elements. We use the indexing notation to access the first element of the array using the index value of 0. We store this value in the $first_element variable and then use echo to display the value to the user.

Note that if the array is empty or doesn't have an element at index 0, this code will generate a notice error.

gistlibby LogSnag