Introduction
While working with PHP applications, developers often need to remove data or delete resources during program execution. PHP provides different functions for handling deletion depending on what you want to remove.
Two commonly used functions for deletion are:
- unlink() is used to delete files from the server.
- unset() is used to destroy variables in PHP.
What is unlink() in PHP?
The unlink() function in PHP is used to delete a file from the server. It removes the specified file permanently from the filesystem.
Note: Once a file is deleted using unlink(), it cannot be recovered unless a backup exists.
This function is commonly used in applications where users upload files, such as:
- Image uploads
- Document management systems
- Temporary file removal
- File cleanup operations
Syntax of unlink()
unlink(filename);
Parameters
- filename – The path of the file that you want to delete.
Return Value
- Returns true if the file is deleted successfully.
- Returns false if the deletion fails.
Example of unlink()
Suppose there is a file named sample.txt stored on the server.
$file = "sample.txt";
if(unlink($file)){
echo "File deleted successfully.";
} else {
echo "File could not be deleted.";
}
Output
Explanation
In this example:
- The variable $file stores the filename.
- The unlink() function attempts to delete the file.
- If successful, a success message is displayed.
Real Life Example of unlink()
Consider a website where users upload profile pictures.
When a user uploads a new picture, the old image should be deleted to save storage space.
$oldImage = "uploads/profile1.jpg";
unlink($oldImage);
echo "Old profile picture deleted.";
What is unset() in PHP?
The unset() function in PHP is used to destroy variables. Once a variable is unset, it no longer exists in memory.
This function helps free memory resources and remove variables that are no longer needed.
Note: unset() only removes variables from the program.
Syntax of unset()
unset($variable);
You can also unset multiple variables at once.
unset($var1, $var2, $var3);
Example of unset()
$name = "Rahul";
unset($name);
echo $name;
Output
Explanation
- A variable $name is created.
- The unset() function destroys the variable.
- When we try to print it, PHP shows an error because the variable no longer exists.
Using unset() with Arrays
The unset() function can also remove specific elements from an array.
$students = array("Rahul", "Aman", "Neha");
unset($students[1]);
print_r($students);
Output
Explanation
Here:
- The element at index 1 (Aman) is removed.
- The remaining elements stay in the array.
Using unset() with Objects
You can also use unset() to remove object properties.
class Student {
public $name = "Ravi";
public $age = 22;
}
$obj = new Student();
unset($obj->age);
print_r($obj);
This removes the age property from the object.
Difference Between unlink() and unset()
Although both functions remove something, their purpose is completely different.
| Feature | unlink() | unset() |
|---|---|---|
| Purpose | Deletes a file | Destroys a variable |
| Works On | Files in filesystem | Variables, arrays, objects |
| Memory Impact | Removes file from disk | Frees memory in program |
| Usage | File management | Variable management |
| Return Value | Returns true/false | Does not return value |
Example Comparing Both Functions
$file = "data.txt";
$name = "Rahul";
unlink($file);
unset($name);
Explanation
- unlink($file) deletes the file data.txt from the server.
- unset($name) removes the variable $name from memory.
Both operations remove something, but from different locations.
When Should You Use unlink()?
Use unlink() when you need to:
- Delete uploaded files
- Remove temporary files
- Clean up storage
- Manage file systems
Example:
unlink("uploads/image.jpg");
When Should You Use unset()?
Use unset() when you want to:
- Remove variables
- Free memory
- Remove array elements
- Destroy object properties
Example:
unset($userData);
Conclusion
Both unlink() and unset() are important functions in PHP used for removing data, but they operate in different contexts.
The unlink() function is used to delete files from the server, making it useful for file management tasks such as removing uploaded images or temporary files.
On the other hand, the unset() function is used to destroy variables in PHP, helping developers free memory and remove unnecessary data from the program.
unlink() vs unset() – Objective Questions (MCQs)
Q1. unlink() is used to ______.
Q2. unset() is used to ______.
Q3. Which function affects file system?
Q4. Which function affects memory variables?
Q5. Can unset() delete a file?