What are the best practices for ensuring that a file is writable before attempting to write to it in PHP?

Before attempting to write to a file in PHP, it is important to ensure that the file is writable to avoid errors. To do this, you can use the `is_writable()` function to check if the file has write permissions. If the file is not writable, you can handle the error accordingly, such as displaying a message to the user or logging the issue.

$file = 'example.txt';

if (is_writable($file)) {
    // Perform write operation
    $data = "Hello, World!";
    file_put_contents($file, $data);
} else {
    echo "Error: The file is not writable.";
}