What are the common pitfalls to avoid when working with loops in PHP, especially when dealing with file manipulation tasks?

One common pitfall when working with loops in PHP, especially when dealing with file manipulation tasks, is forgetting to properly close file handles after opening them. This can lead to memory leaks and potential issues with file locking. To solve this, always remember to close file handles using fclose() after you are done working with a file.

$file = fopen("example.txt", "r");

// Read file contents or perform other file manipulation tasks here

fclose($file);