Example 1 — Walk Backward from the End
end() puts the pointer on the last element, and prev() steps backward from there.
<?php
$numbers = [1, 2, 3];
end($numbers);
echo prev($numbers), PHP_EOL;
echo prev($numbers), PHP_EOL; How It Works
- end() moves the pointer to the last element, 3.
- The first prev() moves back to 2 and returns it.
- The second prev() moves back to 1 and returns it.
- Each echo prints the value that the pointer reached.

