How does php get the value of the penultimate element of the array?

how does php get the value of the penultimate element of the array?
such as the following array:

$arr=["apple","pear","banana","peach"];
Php
Mar.09,2021

$arr = ['apple','pear','banana','peach'];
print_r(array_slice($arr,-2,1));

echo $arr [count ($arr)-2];

you try this


$i = count($arr)-1;
echo $arr[$i];
Menu