PHP | Unset() vs Unlink() Function
Both the function are used to do some undo operations but used in different situations cause both acts differently. The unlink() function is used when you want to delete the files completely. The unset() Function is used when you want to make that file empty.
Unlink() function: The unlink() function is an inbuilt function in PHP which is used to delete a file. The filename of the file which has to be deleted is sent as a parameter and the function returns True on success and False on failure. The unlink() function in PHP accepts two-parameter.
Syntax:
unlink( filename, context )
Parameters: This function accepts two parameters as mentioned above and described below:
- filename: It is a mandatory parameter which specifies the filename of the file which has to be deleted.
- context: It is an optional parameter which specifies the context of the file handle which can be used to modify the nature of the stream.
Return Value: It returns True on success and False on failure.
Suppose there is a file named ‘gfg.txt’
Example:
<?php // PHP program to delete a file named gfg.txt // using unlike() function $file_pointer = fopen('gfg.txt'); // Writing on a file named gfg.txt fwrite($file_pointer, 'A computer science portal for geeks!'); fclose($file_pointer); // Using unlink() function to delete a file unlink('gfg.txt'); ?> |
Output:
1
Note: If we don’t have permissions to the file “gfg.txt”, the unkink() function generates an E_WARNING level error on failure.
Unset() function: The Unset() function is an inbuilt function in PHP which is used to remove the content from the file by emptying it. It means that the function clears the content of a file rather deleting it. The unset() function not only clears the file content but also is used to unset a variable, thereby making it empty.
Syntax:
unset( $variable )
Parameter: This fucntion accepts single parameter variable which is required. It is the variable which is needed to be unset.
Return Value: This function does not return any value.
Example:
<?php $var = "hello"; // Change would be reflected outside the function function unset_value() { unset($GLOBALS['var']); } unset_value(); echo $var; ?> |
Output:
No output
Difference between unlink() and unset() function:
| unlink() Function | unset() Function |
|---|---|
| It is used to delete a file within a directory completely on successful execution. | It is used to make a specific file empty by removing its content. |
| There are two parameter filename and the other one is context. | There is only one parameter variable. |
| Return True on success and false on failure. | This function does not return any value. |
| This is a function for file system handling. | This is a function for variable management. |
Recommended Posts:
- PHP | unlink() function
- PHP | unset() Function
- PHP | Program to delete an element from array using unset() function
- How to unset JavaScript variables?
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- CSS | hsl() Function
- p5.js | abs() function
- PHP | exp() Function
- p5.js | max() function
- p5.js | arc() Function
- p5.js | day() function
- p5.js | second() function
- p5.js | sq() function
- D3.js | d3.map.set() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



