How can error reporting be used effectively in PHP to troubleshoot file writing issues?

When troubleshooting file writing issues in PHP, it is essential to enable error reporting to help identify the root cause of the problem. By setting error reporting to display all errors, warnings, and notices, you can quickly pinpoint any issues with file writing permissions, file paths, or other potential errors.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$file = 'example.txt';
$data = 'Hello, World!';

if (file_put_contents($file, $data) === false) {
    echo "Error writing to file";
} else {
    echo "Data written successfully";
}
?>