What is the function of unlink() in PHP and how does it work?

The unlink() function in PHP is used to delete a file. It takes the file path as a parameter and removes the specified file from the filesystem. It is important to note that once a file is deleted using unlink(), it cannot be recovered.

$file_path = 'example.txt';

if (file_exists($file_path)) {
    unlink($file_path);
    echo "File deleted successfully.";
} else {
    echo "File does not exist.";
}