What are some common pitfalls to avoid when attempting to optimize PHP code for efficiency?

One common pitfall to avoid when optimizing PHP code for efficiency is using inefficient loops, such as nested loops or unnecessary iterations. To improve performance, it's better to use built-in array functions like array_map or array_filter to manipulate arrays efficiently.

// Inefficient loop example
$numbers = [1, 2, 3, 4, 5];
$sum = 0;
foreach ($numbers as $number) {
    $sum += $number;
}

// Efficient array_map example
$numbers = [1, 2, 3, 4, 5];
$sum = array_sum($numbers);