What is the difference between unlink and unset function?

Last updated 5 years, 6 months ago | 1784 views 75     5

Tags:- PHP

unlink() and unset()

The unlink() and unset() function in php have same functionality (i.e.: delete) but the difference is in the type of data on which they are applied to perform such delete functionalities.

unlink() used to delete files from directory where as unset() is used to unset or delete the variable.

 

unlink()

This function is use to deletes a file. This function returns TRUE on success, or FALSE on failure.

<?php
$file = "test.txt";
if (!unlink($file))
  {
  echo ("Error deleting $file");
  }
else
  {
  echo ("Deleted $file");
  }
?>

unset()

This unset() is used to unset or delete a specified variable.

<?php

$var='StudyZone4U.com';

echo 'Before using unset() the value of $var is : '. $var.'<br>';

unset($var);

echo 'After using unset() the value of $var is : '. $var;


//output

Before using unset() the value of $var is : StudyZone4U.com

After   using unset() the value of $var is : 

?>