How can error reporting be utilized effectively to debug PHP code related to file creation?

To effectively debug PHP code related to file creation, error reporting can be utilized by enabling error reporting and displaying error messages. This can help identify any issues with file creation, such as incorrect file paths or permissions.

<?php
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Code for file creation
$file = 'example.txt';
$handle = fopen($file, 'w');

if ($handle === false) {
    die('Error creating file');
}
else {
    echo 'File created successfully';
}

fclose($handle);
?>