What is the significance of using fclose() when writing to files in PHP?

When writing to files in PHP, it is important to use the fclose() function to properly close the file handle after writing data. This ensures that all data is flushed to the file and that the file is properly closed, preventing any potential data loss or corruption. Failing to close the file handle can lead to resource leaks and potential issues with file locking.

$file = fopen("example.txt", "w");
fwrite($file, "Hello, World!");
fclose($file);