Open In App

PHP | Unset() vs Unlink() Function

Last Updated : 26 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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
 
// 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!');
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 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.
Syntax: 
 

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.
Example: 
 

php




<?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.

 



Previous Article
Next Article

Similar Reads

PHP | unlink() Function
The unlink() function is an inbuilt function in PHP which is used to delete files. It is similar to UNIX unlink() function. The $filename is sent as a parameter that needs to be deleted and the function returns True on success and false on failure. Syntax: unlink( $filename, $context ) Parameters: This function accepts two parameters as mentioned a
2 min read
Node fs.unlink() Method
`fs.unlink()` in Node.js removes files or symbolic links, while for directories, it's advisable to use `fs.rmdir()` since it doesn't support directory removal. Syntax: fs.unlink( path, callback )Parameters: This method accepts two parameters as mentioned above and described below: path: It is a string, Buffer or URL that, represents the file or sym
2 min read
PHP | unset() Function
The unset() function is an inbuilt function in PHP which is used to unset a specified variable. The behavior of this function depends on different things. If the function is called from inside of any user defined function then it unsets the value associated with the variables inside it, leaving the value which is initialized outside it. It means th
2 min read
PHP | Program to delete an element from array using unset() function
Given an array of elements, we have to delete an element from the array by using the unset() function. Examples: Input : $arr = array("Harsh", "Nishant", "Bikash", "Barun"); unset($arr[3]); Output : Array ( [0] =&gt; Harsh [1] =&gt; Nishant [2] =&gt; Bikash ) Input : $arr = array(1, 2, 6, 7, 8, 9); unset($arr[3]); Output : Array ( [0] =&gt; 1 [1] =
2 min read
PHP | Unset Session Variable
Whenever data are stored using cookies, there is a possibility of a hacker to insert some harmful data in the user's computer to harm any application. So its always advisable to use PHP sessions to store information on the server than on a computer. Whenever data is needed across many pages of a website or application, PHP Session is used. This ses
3 min read
Which one is better (unset() or $var = null) to free memory in PHP ?
In this article, we will discuss freeing the memory with unset() and using NULL value to any variable. unset(): The unset() function is an inbuilt function in PHP that is used to unset a specified variable. The unset() function just destroys or removes the variable from the symbol table. After the unset() applied on a variable, it's marked for PHP
2 min read
What is the Difference Between unset and null in PHP ?
In PHP, unset and null are used to handle variables differently. unset is a language construct used to destroy a variable, while null is a special value representing a variable with no value assigned to it. Syntaxunset($variable);$variable = null;Difference Between unset and nullunset()nullDestroys a variable and frees up memoryAssigns a null value
1 min read
How to unset JavaScript variables?
To know how to unset a variable we must know what are JS variables and how to declare a variable first. JavaScript variables are containers for storing data values. Variables can be declared using the 'var' keyword. Example : var x = 5; In the above code declaration, the value 5 has been assigned to the variable 'x'. The unsetting of a variable mea
3 min read
Difference between revert and unset keyword in CSS
Both of them are keywords in CSS. Both of them act similarly in web development but having differed for some properties for some elements. Revert: The revert keyword is an inbuilt keyword in CSS which has been used to revert the cascading style to its parent element. If any similar elements hold the same cascading style, but one child holds the rev
2 min read
CSS | unset keyword
The CSS unset keyword is an inbuilt keyword in CSS. The CSS unset keyword is an inbuilt keyword in CSS which has been used to unset the cascading style to its parent element. It behaves like the inherit keyword in the first case, and like the initial keyword in the second caseSyntax: property: unset; Below example illustrates the unset keyword in C
1 min read