What are the potential pitfalls of using for loops in PHP scripts, and how can they be avoided or optimized for better performance?
Potential pitfalls of using for loops in PHP scripts include inefficiency due to unnecessary iterations, which can slow down script execution, especially for large datasets. To optimize for better performance, consider using foreach loops when working with arrays or collections, as they are often more efficient and concise.
// Example of using foreach loop instead of for loop for iterating over an array
$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
echo $number . PHP_EOL;
}