What are some best practices for managing memory allocation in PHP scripts to avoid errors like "out of memory"?

Memory allocation issues in PHP scripts, such as running out of memory, can be avoided by properly managing memory usage. One way to do this is by limiting the amount of memory allocated for each script execution and freeing up memory when it is no longer needed. This can be done by using functions like `memory_limit()` and `unset()` to release memory resources.

// Set memory limit to 128MB
ini_set('memory_limit', '128M');

// Allocate memory for a large array
$largeArray = range(1, 1000000);

// Process the array

// Free up memory when done
unset($largeArray);