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.
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’
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.
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.
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.|
- PHP | unlink() function
- PHP | unset() Function
- PHP | Program to delete an element from array using unset() function
- Node.js | fs.unlink() Method
- CSS | unset keyword
- PHP | Unset Session Variable
- Difference between revert and unset keyword in CSS
- How to get the function name inside a function in PHP ?
- D3.js | d3.rgb() Function
- p5.js | pow() function
- PHP | pos() 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 email@example.com. 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.