Most solutions here are unreliable for unassociated arrays, because if we have an unassociated array with the last element being false, then end and current(array_slice($array, -1)) will also return false so we can't use false as an indicator of an empty unassociated array.
// end returns false form empty arrays
>>> $arr = []
>>> end($arr)
=> false
// last element is false, so end returns false,
// now we'll have a false possitive that the array is empty
>>> $arr = [1, 2, 3, false]
>>> end($arr)
=> false
>>> $arr = [1, 2, 3, false, 4]
>>> end($arr)
=> 4
Same goes for current(array_slice($arr, -1)):
// returns false form empty arrays
>>> $arr = []
>>> current(array_slice($arr, -1))
=> false
// returns false if last element is false
>>> $arr = [1, 2, 3, false]
>>> current(array_slice($arr, -1))
=> false
>>> $arr = [1, 2, 3, false, 4]
>>> current(array_slice($arr, -1))
=> 4
Best option is to use array_key_last which is available for PHP >= 7.3.0 or for older versions, we use count to get the last index (only for unassociated arrays):
// returns null for empty arrays
>>> $arr = []
>>> array_key_last($arr)
=> null
// returns last index of the array
>>> $arr = [1, 2, 3, false]
>>> array_key_last($arr)
=> 3
// returns last index of the array
>>> $arr = [1, 2, 3, false, 4]
>>> array_key_last($arr)
=> 4
For older versions, we can use count:
>>> $arr = []
>>> if (count($arr) > 0) $arr[count($arr) - 1]
// No excecution
>>> $arr = [1, 2, 3, false]
>>> if (count($arr) > 0) $arr[count($arr) - 1]
=> false
>>> $arr = [1, 2, 3, false, 4]
>>> if (count($arr) > 0) $arr[count($arr) - 1]
=> 4
That's all for unassociated arrays. If we are sure that we have associated arrays, then we can use end.