Javatpoint Logo
Javatpoint Logo

PHP Unset() vs Unlink() Function

PHP unset( ) is an inbuilt function mainly used to unset a specified variable. The function of that variable depends upon various factors. Suppose a certain function is called inside of a user-declared function. In that case, it will unset the variable associated value and leave the value initiated outside, which means the function can only unset local variables. Still, we have to $ GLOBAL array to unset global targets to perform the task.

Although both the PHP unlink( ) function and the unset( ) functions are used for undo operations, there is a vast difference between the situation where we have to implement those as both operate differently. The unset( ) function is mostly used when we want to make a certain file empty by only deleting the contents of the file, not the complete file, whereas the unlink( ) function is used in a situation when we need to completely delete a whole file not just the content of the file.

PHP Unlink( ) Function

The unlink( ) function is a PHP inbuilt function, which is mostly used when we have to delete a certain file, not only the content completely.

The unlink( ) function holds two parameters, the filename of the file which has to be eradicated is set in this parameter, and the function reads the parameter. After completing the operation, it either returns a TRUE on successful file deletion or a FALSE if it faces any error while deleting the same file.

Syntax

PHP Unlike() function has the following syntax:


S.No Parameter Description Mandatory / optional parameter
1 filename This parameter holds the file name that will be deleted. The compiler will read the file's name and then start the operation. Mandatory
2 context This parameter is used to display the context of the file that will be deleted, i.e., it specifies how to manage or handle the file and to manipulate the stream nature. optional

Example

Output

The above code output is:

1

PHP Unset( ) Function

The unset( ) function is a PHP inbuilt function, which is mostly used when we only have to delete certain content of a file, not the entire file, i.e., it helps the developer clear the content of a file.

The unlink( ) function holds only a single parameter, the $ variable, which is the context that will be used as unset and cleared from the files. The function reads the parameter, and after completing the operation, it does not return any value.

Syntax

PHP Unset() function follows this syntax:


S.No Parameter Description Mandatory / optional parameter
1 variable This parameter holds the context of the file that will be cleared. The compiler will read the variable and then start the operation. Mandatory

Example

Output

The above code gives the following output.

The value of variable ' $ variable ' before the unset ( ) function - - - > Hello world ! ! ! ! !
The value of variable ' $ variable ' after the unset ( ) function - - - >

Difference between Unlink() and Unset() Function

Below are some major differences between Unlike() and Unset() function.

S.No Unlink ( ) function Unset ( ) function
1 It is mainly used to delete a whole file completely within a directory/folder after executing successfully. It is mainly used to delete a specific file completely by erasing all the files' content one after another.
2 It holds a two-parameter file name, a mandatory parameter, and context, an optional parameter. It holds only a single parameter, variable, a mandatory parameter.
3 On successful execution, the function a true Boolean value and a false Boolean value if there's been an error deleting the file. This function does not return any value on a successful compilation.
4 This function is vastly used as file system management. This function is vastly used as variable management.
5 It is used for deleting the whole file. It is used for deleting certain content of a file, not the entire file.

Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA