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 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.
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.|
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.