What potential pitfalls should be considered when retrieving the second-to-last entered data in PHP?

When retrieving the second-to-last entered data in PHP, potential pitfalls to consider include ensuring that there is enough data to retrieve the second-to-last entry, handling cases where there is only one entry in the dataset, and avoiding errors when the data is not in a predictable order.

// Retrieve the second-to-last entered data from an array
$data = [1, 2, 3, 4, 5];

if(count($data) >= 2) {
    $secondToLast = $data[count($data) - 2];
    echo "Second-to-last entry: " . $secondToLast;
} else {
    echo "Not enough data to retrieve the second-to-last entry";
}