How can PHP be used to delete directories and their contents on a server?

To delete directories and their contents on a server using PHP, you can use the `rmdir()` function to delete the directory itself and the `unlink()` function to delete all files within the directory. You can recursively delete directories by using a combination of these functions along with `scandir()` to get the list of files and subdirectories within a directory.

function deleteDirectory($dir) {
    if (!file_exists($dir)) {
        return;
    }
    
    $files = array_diff(scandir($dir), array('.', '..'));
    
    foreach ($files as $file) {
        (is_dir("$dir/$file")) ? deleteDirectory("$dir/$file") : unlink("$dir/$file");
    }
    
    rmdir($dir);
}

// Usage
deleteDirectory('/path/to/directory');