• 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 / php unset – How to Destroy Variables in php

php unset – How to Destroy Variables in php

March 18, 2022 Leave a Comment

The php unset() function destroys one or more variables. We can use unset() to delete previously declared variables.

$variable = "I'm a variable";

echo var_dump(isset($variable));

unset($variable);

echo var_dump(isset($variable));

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

When working with variables in our php programs, it is useful to be able to delete a variable. In php, the unset() function allows us to delete variables.

You can pass one or more variables to unset() and unset() will delete those variables.

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)

Deleting Multiple Variables with unset() Function in php

With the php unset() function, we can delete multiple variables.

To delete multiple variables, pass the variables separated by commas to unset().

Below is an example of how to use unset() to destroy multiple variables in php. We can verify that all the variables has been deleted with the isset() function.

$variable1 = "I'm a variable";
$variable2 = "I'm another variable";

echo var_dump(isset($variable1, $variable2));

unset($variable1, $variable2);

echo var_dump(isset($variable1, $variable2));

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

Hopefully this article has been useful for you to learn how to use the php unset() function to destroy previously declared variables in your php code.

Other Articles You'll Also Like:

  • 1.  php array_flip() – Flip the Keys and Values of an Array in php
  • 2.  php cosh – Find Hyperbolic Cosine of Number Using php cosh() Function
  • 3.  php asin – Find Arcsine and Inverse Sine of Number Using asin() Function
  • 4.  Remove Last Character from String Using php
  • 5.  php tan – Find Tangent of Number in Radians Using php tan() Function
  • 6.  php array_splice() – Remove, Insert, and Replace Elements in Array
  • 7.  Remove First Character from String Using php
  • 8.  php preg_match – Find Pattern in String Using Regex (Regular Expression)
  • 9.  php e – Using M_E and exp() Function to Get Euler’s Constant e
  • 10.  php str_replace – Replace String in php

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