unset() and unlink() are two very useful php functions. The difference between unset() and unlink() in php is unset() deletes variables and unlink() deletes files.

$variable = "I'm a variable";
unset($variable);

unlink("./folder/file_name1.txt");

In php, there are many different functions which allow us to create complex programs. Some of the functions have similar names which make it hard to keep straight what certain functions do.

The php unset() and unlink() functions are two powerful, commonly used functions. With similar names, it might be easy to be confused on what each function does.

The php unset() function works on variables and allows us to destroy variables.

With unset(), you can delete any variable previously declared.

The php unlink() function on the other hand works on files and allows us to delete files.

Therefore, the difference between unset() and unlink() is that unset() deletes variables while unlink() deletes files.

An Example of What the unset() Function Does in php

As you found out above, the unset() function in php deletes variables.

unset() can delete one or more than one variable depending on how many variable names you pass it.

Below is an example of how to use unset() to delete a variable in php. We can verify that the variable has been deleted with the php isset() function.

$variable = "I'm a variable";

echo var_dump(isset($variable));

unset($variable);

echo var_dump(isset($variable));

//Output:
bool(true)
bool(false)

An Example of What the unlink() Function Does in php

As you found out above, the unlink() function in php deletes files.

unlink() can delete one or more than one file depending on how many file name strings you pass it.

Below is a simple example of if we wanted to delete a file named “file_name.txt” using the php unlink() function.

unlink("./folder/file_name.txt");

Hopefully this article has been useful for you to learn what the difference between unset() and unlink() is in php.

Categorized in:

PHP,

Last Update: March 13, 2024