Change language

PHP Unset () versus Unlink ()

Unlink() function: unlink() function - it is a built-in function in PHP that is used to delete a file. The filename of the file to be deleted is sent as a parameter, and the function returns True on success and False on error. The unlink() function in PHP takes two parameters.Syntax:
unlink (filename, context) 
Parameters :This function takes two parameters as above and described below:
  • filename:This is a required parameter and specifies the name of the file to be deleted.
  • context:this is an optional parameter that specifies a file descriptor context that can be used to change the nature of the stream.
Return value :returns True on success and False on failure.Suppose there is a file named ’gfg.txt’Example :  
// PHP program to delete a file named gfg .txt
// using the different() function  $file_pointer = fopen ( ’gfg.txt’ );  
// Write to file named gfg.txt fwrite ( $file_pointer , ’A computer science portal for engineer!’ ); fclose ( $file_pointer );  
// Using the unlink() function to delete a file unlink ( ’gfg.txt’ );  
?>
Output:
1 
Note.If we do not have permission to access the "gfg.txt" file, the unkink() function generates an E_WARNING level error on error. Unset() function: Unset() function - it is a built-in function in PHP that is used to remove content from a file by cleaning it up. This means that the function cleans up the contents of the file, but rather deletes it. The unset() function not only clears the contents of a file, but is also used to reset a variable, making it empty.Syntax:
unset ($variable) 
Parameter:This function accepts a variable withone parameter, which is required. This is a variable that must be reset.Returned value:This function does not return any value.Example: $var = "hello" ;  
// The change will be reflected outside the function function unset_value() { unset ( $GLOBALS [ ’ var’ ]); }  
unset_value(); echo $var ; ?>
Output:
No output 
Difference between unlink() and unset() functions: unlink() Function unset() Function It is used to delete a file within a directory completely on successful execution. It is used to make a specific file empty by removing its content. There are two parameter filenameand the other one is context . There is only one parameter variable. Return True on success and false on failure. This function does not return any value. This is a function for file system handling. This is a function for variable management.

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method