What is the difference between unlink() and unset() in PHP?

unlink() deletes a file from the file system. unset() removes a variable from memory. They do very different jobs.

unlink()unset()
Deletes a file from the serverDestroys a variable
Takes the file name or pathTakes the variable
Returns true or falseReturns nothing
The file is gone for everOnly the variable becomes undefined
<?php
$file = "notes.txt";
file_put_contents($file, "Hello");   // create a file

unlink($file);
echo file_exists($file) ? "File exists<br>" : "File deleted<br>";

$name = "Robin";
unset($name);
var_dump(isset($name));
?>

Output:

Tutorials Class - Output Window

File deleted
bool(false)

Be careful with unlink(), a deleted file can not be taken back. Never pass a file name that comes from user input without checking it.


Learn more about the similar topics:
Tutorials
PHP Advanced
PHP File Upload
PHP File Handling
PHP File Inclusion
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What is include() function in PHP?
Can we include a file two times in any PHP page?
Which is the PHP function to delete a file from server?
What is fopen function PHP?
What is the difference between unlink() and unset() in PHP?
How to check if a file exists in PHP?
How to get the size of a file in PHP?