PHP unlink() Vs unset()

by Vincy. Last modified on July 3rd, 2022.

These two PHP functions are identical in performing some undo operations but differ in the type of data on which they are applied to perform such undo functionalities.

In PHP, unlink() is acting as the drop operation which will completely delete files, whereas, unset() is used to delete the file content for emptying it. Let us look into the following paragraphs to have detailed information about these functions with examples.

unlink() in PHP

PHP unlink() functions are used to delete a file within a directory completely on successful execution, Rather, it causes a PHP warning error. The possibilities for error occurrences are, if we are not having permission to access the directory of the file to be deleted, or if such file does not exist.

This function accepts two arguments, that is, the required file name to be unlinked with the file content, and an optional context parameter used to specify the supported protocols like http://, ftp://, file:// and etc.

Now, let us have an example to learn the usage of unlink() function in PHP. In this example, we are going to create a file and add the content programmatically. And then, we are going to unlink() this file after getting a copy of it. For performing the file create, write and copy operation, we can use PHP-supported file functions we have seen already.

<?php
$source = "D:/test_folder/hello.txt";
$destination = "D:/test_folder/welcome.txt";
$filePointer = fopen($source, "x");
fwrite($filePointer, "PHP FUNCTIONS\n
File Handling functions\n");
copy($source, $destination);
unlink($source);
?>

The above PHP program will preserve the copied file welcome.txt and unlink the original file hello.txt. And, we need to ensure that the target directory, in which we create a new file and a copy of it, exists. And also, it is required to have appropriate permissions for unlinking files. Otherwise, the following warning error will be displayed.

Warning: unlink(D:/test_folder/hello.txt): Permission denied in ...

To suppress this warning notice displayed to the browser, we can use the unlink() function with the prefix @ symbol.

PHP unset()

Unlike the above PHP function, unset() is used to make the target file to be empty after removing its content, meaning that, the purpose is for clearing content instead of deleting a file permanently from the folder.

unset() is not only used to clear the file content, but also used for undoing the initialization of a PHP variable, and thereby making it empty.

In the following code sample, unset() is used for clearing the content initialized with the PHP variable $welcome_text. Similarly, we can use this function for emptying a file by removing its content on specifying the path of the file as we have done with PHP unlink().

<?php
$welcome_text = "Welcome to PHPPOT";
unset($welcome_text);
if (isset($welcome_text)) {
    echo "<strong>My welcome text is: </strong><i>" . $welcome_text . "</i>";
} else {
    echo "<strong>My welcome text is: </strong> empty";
}
?>

The PHP code shown above, it calls the function unset() after initializing $welcome_text. So, the initialization will not create an effect on this variable, that we can know on getting the result of the echo statement.

Since we have unset, the isset() function will return false for the conditional statement that leads the else part to execute, and so, the browser displays,

My welcome text is: empty

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page