How does the unlink() function in PHP work and what are the potential outcomes when attempting to delete a file?

The unlink() function in PHP is used to delete a file from the server. When attempting to delete a file using unlink(), there are several potential outcomes: if the file is successfully deleted, unlink() will return true; if the file does not exist or cannot be deleted due to permissions, unlink() will return false; if an error occurs during the deletion process, unlink() may trigger a warning or error message.

$file_path = 'path/to/file.txt';

if (file_exists($file_path)) {
    if (unlink($file_path)) {
        echo 'File deleted successfully.';
    } else {
        echo 'Unable to delete file.';
    }
} else {
    echo 'File does not exist.';
}