• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / What is the Difference Between unset() and unlink() in php?

What is the Difference Between unset() and unlink() in php?

April 3, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Get Array Length with php count() Function
  • 2.  php Delete Files with php Unlink Function
  • 3.  php cos – Find Cosine of Number in Radians Using php cos() Function
  • 4.  Reversing an Array in php with array_reverse() Function
  • 5.  php var_dump() Function – Print Information about Variable Type and Value
  • 6.  Remove HTML Tags from String in php
  • 7.  Remove Specific Character from String Variable in php
  • 8.  php Print Array – How to Print All Elements in Array
  • 9.  Replace Space with Dash in php String
  • 10.  How to Parse URLs with php parse_url() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy