group-icon
PHP Programming
Group for all things PHP, Coding Help, Problems and Solutions
101 Members
Join this group to post and comment.
Vijay Khatri
Vijay Khatri
Computer Science
07 Feb 2019

What’s the difference between unset() and unlink()

Please explain with a program as well?

DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
1mo ago

The unlink() function is used to delete the files. Like for example you are in a online shopping portal you want to remove a product from your cart then you can use the unlink() function.

The unset() function is used to destroy a variable in PHP. It can be used to destroy a single variable, multiple variable or variable from an array.

consider the following example for unset() function,

$a ="test";

?>

echo $a;

unset($a);

echo $a;

?>

In the above code only once the value of variable will be displayed, since the next echo is followed by the unset function.

For unlink() function place a image folder in the html folder and write the bellow code,

unlink("give the path of your image file in your folder");

?>

After running the code you will find that the image file is been deleted.

Shruthi Pillai
Shruthi Pillai
Information Technology
1mo ago

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. 

PHP unlink() functions are used to delete a file within a directory completely on successful execution,  rather it causes php warning error.

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