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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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'); 
  
?>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
       
$var = "hello"
          
// Change would be reflected outside the function  
function unset_value() { 
    unset($GLOBALS['var']); 
          
unset_value(); 
echo $var
?> 

chevron_right


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.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.