What are some common errors that may occur when trying to write to a file in PHP?

One common error when trying to write to a file in PHP is not having the correct file permissions set. Make sure the file you are trying to write to has the appropriate write permissions for the PHP script to modify it. Additionally, make sure the file path is correct and that the file actually exists before attempting to write to it.

$file = 'example.txt';

if (is_writable($file)) {
    $handle = fopen($file, 'w');
    fwrite($handle, 'Hello World');
    fclose($handle);
    echo 'Successfully wrote to file.';
} else {
    echo 'Cannot write to file. Check file permissions.';
}