Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

end

Returns the last element of an array.

Syntax

end(array|object &$array): mixed

Parameters

ParameterTypeDescription
$arrayarray|objectAn array held in a variable.

Return value

mixedThe last element, or `false` for an empty array.

Examples

The last element:

<?php
 
$numbers = [10, 20, 30];
 
echo end($numbers);

Output:

30

The same without the internal pointer:

<?php
 
$numbers = [10, 20, 30];
 
echo $numbers[array_key_last($numbers)];

Output:

30

Notes

The function takes only a variable: end(explode(',', $text)) raises a notice. It moves the array's internal pointer and leaves the array itself untouched.

See also

  • reset — Returns the first element of an array.
  • array_key_first — Returns the first key of an array.
  • array_pop — Removes the last element of an array and returns it.

In the tutorial

  • Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.

Thanks for reading!

Donate to the site — we work hard for you.