What best practices should be followed when creating and deleting directories in PHP?

When creating directories in PHP, it is important to check if the directory already exists before attempting to create it. This can be done using the `is_dir()` function. When deleting directories, it is crucial to first check if the directory exists and is writable before attempting to delete it. This can be done using the `is_dir()` and `is_writable()` functions.

// Create directory if it does not exist
$directory = 'path/to/directory';
if (!is_dir($directory)) {
    mkdir($directory, 0777, true);
}

// Delete directory if it exists and is writable
$directory = 'path/to/directory';
if (is_dir($directory) && is_writable($directory)) {
    rmdir($directory);
}