Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PHP | Unset() vs Unlink() Function

  • Last Updated : 26 May, 2021

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’


// PHP program to delete a file named gfg.txt
// using unlink() function
$file_pointer = fopen('gfg.txt');
// Writing on a file named gfg.txt
fwrite($file_pointer, 'A computer science portal for geeks!');
// Using unlink() function to delete a file



Note: If we don’t have permissions to the file “gfg.txt”, the unlink() 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 function 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.


$var = "hello";
// Change would be reflected outside the function 
function unset_value() {
echo $var;


No output

Difference between unlink() and unset() function: 

unlink() Functionunset() 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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!