Online visitors

People learning around the world

Online

—

Active in the last 10 minutes

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    PHP array_first() and array_last() Functions

    PHP 8.5 adds the array_first() and array_last() functions for reading the first and last values of an array. These functions remove common boilerplate that previously required combining array_key_first() or array_key_last() with an array lookup.

    Both functions return the array value, not the key. If the array is empty, they return null.

    array_first() Syntax

    $value = array_first($array);

    The function accepts one array and returns its first value.

    array_last() Syntax

    $value = array_last($array);

    The function accepts one array and returns its last value.

    These functions are available in PHP 8.5 and later. They are especially useful when you care about values rather than keys.

    Get the First and Last Values

    Example:

    <?php
    
    $events = [
        'login',
        'checkout',
        'logout'
    ];
    
    echo array_first($events) . PHP_EOL;
    echo array_last($events);

    Output:

    login
    logout

    Associative Arrays

    The functions follow array iteration order, so they work naturally with associative arrays whose keys are strings or non-sequential integers.

    <?php
    
    $user = [
        'name' => 'Asha',
        'role' => 'admin',
        'status' => 'active'
    ];
    
    echo array_first($user);
    echo PHP_EOL;
    echo array_last($user);

    Output:

    Asha
    active

    The keys are not returned. If you need the first or last key, use array_key_first() or array_key_last().

    Empty Arrays

    For an empty array, both functions return null.

    <?php
    
    $items = [];
    
    var_dump(array_first($items));
    var_dump(array_last($items));

    Output:

    NULL
    NULL

    This behavior makes the functions convenient with the null coalescing operator.

    <?php
    
    $queue = [];
    
    $first = array_first($queue) ?? 'No items';
    
    echo $first;

    Output:

    No items

    Why Not Use reset() and end()?

    Older PHP code sometimes uses reset() and end() to obtain boundary values. Those functions interact with the array's internal pointer, which makes them less direct for simple value access.

    The PHP 8.5 functions express the intention clearly and do not require pointer-oriented array operations.

    PHP 8.4 and Earlier

    Before PHP 8.5, a common pattern was to get the boundary key and then use it to read the corresponding value.

    <?php
    
    $items = ['red', 'green', 'blue'];
    
    $first = $items === []
        ? null
        : $items[array_key_first($items)];
    
    $last = $items === []
        ? null
        : $items[array_key_last($items)];

    PHP 8.5 reduces that to direct function calls.

    Need PHP 8.5 function
    First value array_first()
    Last value array_last()
    First key array_key_first()
    Last key array_key_last()

    Use with Function Results

    Because the functions accept normal array expressions, they can be used directly with arrays returned from another function.

    <?php
    
    function getScores(): array
    {
        return [72, 88, 95];
    }
    
    echo array_last(getScores());

    Output:

    95

    Common Mistakes

    • Expecting a key: these functions return values.
    • Forgetting the PHP version: they require PHP 8.5 or later.
    • Treating null as proof that the array is empty: the actual first or last value can itself be null.
    • Assuming numeric indexes: array order works independently of whether keys are numeric or associative.

    Null Values and Empty Arrays

    Remember that a non-empty array can legitimately contain null as its first or last value. If you must distinguish an empty array from a boundary value that is null, check the array itself with $array === [] or count() before interpreting the result.

    Conclusion

    array_first() and array_last() make boundary-value access simpler in PHP 8.5. They work with indexed and associative arrays, return null for empty arrays, and replace verbose key-lookup patterns with direct, readable function calls.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.

    Test browser code

    Use this code in admin to send a test notification to this browser.