How can the function date() impact performance when called multiple times within a loop in PHP?
Calling the date() function multiple times within a loop can impact performance because it involves calculating the current date and time repeatedly, which can be resource-intensive. To solve this issue, you can call the date() function once outside the loop and store the result in a variable, then use that variable within the loop instead of calling date() each time.
// Store the current date in a variable outside the loop
$currentDate = date('Y-m-d H:i:s');
// Use the stored date variable within the loop
for ($i = 0; $i < 10; $i++) {
echo "Current date: " . $currentDate . "<br>";
}