What are the best practices for utilizing unset in PHP to optimize memory usage and script performance?

When working with large datasets or objects in PHP, it is important to free up memory by properly unsetting variables that are no longer needed. This can help optimize memory usage and improve script performance. By using the unset function to remove variables that are no longer in use, you can ensure that memory is released back to the system for other processes.

// Example of unsetting variables to optimize memory usage
$largeDataSet = [/* large dataset */];

// Process data...

// Unset the variable to free up memory
unset($largeDataSet);