What is the best practice for storing values in an array within a loop in PHP?

When storing values in an array within a loop in PHP, it is important to initialize the array outside of the loop and then push the values into the array during each iteration of the loop. This ensures that the array is not reinitialized with each iteration, preserving the values accumulated from previous iterations.

$values = array(); // Initialize an empty array outside of the loop

for ($i = 0; $i < 5; $i++) {
    $value = $i * 2; // Example value to store in the array
    $values[] = $value; // Push the value into the array
}

print_r($values); // Output the array to see the stored values