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 server | Destroys a variable |
| Takes the file name or path | Takes the variable |
| Returns true or false | Returns nothing |
| The file is gone for ever | Only 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 |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |