PHP | Unset() vs Unlink() Function Last Updated : 26 May, 2021 Comments Improve Suggest changes Like Article Like 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() Functionunset() FunctionIt 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. Comment More infoAdvertise with us Next Article PHP | Unset() vs Unlink() Function G geetanjali16 Follow Improve Article Tags : Web Technologies PHP PHP Programs PHP-function Similar Reads session_unset() vs session_destroy() in PHP There are two very similar PHP function session_destroy() & session_unset(). Both seem to delete all variables registered to a session but there is difference between them. session_destroy() function: It destroys all of the data associated with the current session. It does not unset any of the g 3 min read Implementation of Linked List in PHP A linked list is a fundamental data structure in computer science, commonly used for efficient data management and manipulation. Unlike arrays, linked lists store elements in nodes that are not placed contiguously in memory, making them ideal for situations where the size of the data structure can c 4 min read Scope Resolution operator in PHP The scope resolution operator also known as Paamayim Nekudotayim or more commonly known as the double colon is a token that allows access to static, constant, and overridden properties or methods of a class. It is used to refer to blocks or codes in context to classes, objects, etc. An identifier is 2 min read When to use self over $this in PHP ? The self and this are two different operators which are used to represent current class and current object respectively. self is used to access static or class variables or methods and this is used to access non-static or object variables or methods. So use self when there is a need to access someth 2 min read When to use static vs instantiated classes in PHP? Prerequisite - Static Function PHP In PHP, we can have both static as well as non-static (instantiated) classes. Static class Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (m 5 min read Reset keys of array elements using PHP ? In PHP, two types of arrays are possible indexed and associative array. In case of Indexed array, the elements are strictly integer indexed and in case of associative array every element has corresponding keys associated with them and the elements can only be accessed using these keys. To access the 3 min read How to delete a file using PHP ? To delete a file by using PHP is very easy. Deleting a file means completely erase a file from a directory so that the file is no longer exist. PHP has an unlink() function that allows to delete a file. The PHP unlink() function takes two parameters $filename and $context. Syntax: unlink( $filename, 2 min read Comparison between static and instance method in PHP Static methods The static method in PHP is same as other OOP languages. Static method should be used only when particular data remains constant for the whole class. As an example, consider that some programmer is making the data of a college and in that every object needs getCollegeName function tha 3 min read How to get random value out of an array in PHP? There are two functions to get random value out of an array in PHP. The shuffle() and array_rand() function is used to get random value out of an array.Examples: Input : $arr = ("a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20") // Get one random value Output :7 Input : $arr = ("a"=>"21", "b" 3 min read What are getters and setters methods in PHP ? In object-oriented programming, getters and setters are methods used to access and modify the private or protected properties of a class. These methods provide a controlled way to retrieve and update the values of class properties, promoting encapsulation and maintaining the integrity of an object's 3 min read Like