The Wayback Machine - https://web.archive.org/web/20240623133628/https://www.geeksforgeeks.org/which-one-is-better-unset-or-var-null-to-free-memory-in-php/
Open In App

Which one is better (unset() or $var = null) to free memory in PHP ?

Last Updated : 02 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss freeing the memory with unset() and using NULL value to any variable.

unset(): The unset() function is an inbuilt function in PHP that is used to unset a specified variable. The unset() function just destroys or removes the variable from the symbol table. After the unset() applied on a variable, it’s marked for PHP garbage collection.

Syntax:

unset($variable)
  • $variable which is unset

Example: The following example demonstrates the unset() function. In the following example, the $a memory is removed from the variable stack, the $a does not exist anymore after the unset action.

PHP




<?php
  
    // Declare a variable and set
    // to some string
    $a = "hello geeks";
    echo "Before unset : $a";
          
    // Unset this variable
    unset($a);
    echo "<br>";
  
    // Display the variable
    echo "After unset : $a";
?>



 

Output:

Before unset : hello geeks
After unset :

null: null is used to empty the variable. We can create a null variable by simply assigning it to null. The memory is not freed, but NULL data is re-written or re-assigned on that particular variable.

Syntax:

$variable = null;

Example:

PHP




<?php
  
    // Declare a variable and
    // set to string
    $a = "Hello geeks";
    echo "Before null : $a";
          
    // Assign null to this variable
    $a = null;
    echo "<br>";
  
    // Display result
    echo "After null : $a";
?>


Output:

Before null : Hello geeks
After null :

Which one is better?

unset() function:

  • unset() does not force immediate memory freeing, and it is used to free variable usage.
  • PHP garbage collector cleans up the unset variables.
  • CPU cycles are not wasted
  • It takes time to free memory

null variable:

  • null variable immediately frees the memory.
  • CPU cycles are wasted and it takes longer execution time.
  • It speedily frees the memory.

Conclusion: NULL is better if the memory needed is less for a variable.



Similar Reads

Difference between $var and $$var in PHP
In PHP, $var is used to store the value of the variable like Integer, String, boolean, character. $var is a variable and $$var stores the value of the variable inside it. $var: Syntax: $variable = value;The $variable is the variable nameThe value is the initial value of the variable. Example 1: This example stores and displays values with $. C/C++
2 min read
What is the Difference Between unset and null in PHP ?
In PHP, unset and null are used to handle variables differently. unset is a language construct used to destroy a variable, while null is a special value representing a variable with no value assigned to it. Syntaxunset($variable);$variable = null;Difference Between unset and nullunset()nullDestroys a variable and frees up memoryAssigns a null value
1 min read
Web Developer or Android Developer - Which One is Better Career Choice?
Web development and Android development are the two most emerging career sectors for developers. Both of these sectors require different skills, have different markets, and most important have diversified usability. While making choice, we need to compare them on the basis of the frameworks required for development steps in learning Web Development
5 min read
PHP | var keyword
The var keyword in PHP is used to declare a property or variable of class which is public by default. The var keyword is same as public when declaring variables or property of a class.Note: The var keyword was deprecated from version 5.0.0 up to version 5.1.2. Since PHP 5.1.3 it has been added again.Syntax: class className { var $variable = "Geeksf
2 min read
PHP | unset() Function
The unset() function is an inbuilt function in PHP which is used to unset a specified variable. The behavior of this function depends on different things. If the function is called from inside of any user defined function then it unsets the value associated with the variables inside it, leaving the value which is initialized outside it. It means th
2 min read
PHP | Program to delete an element from array using unset() function
Given an array of elements, we have to delete an element from the array by using the unset() function. Examples: Input : $arr = array("Harsh", "Nishant", "Bikash", "Barun"); unset($arr[3]); Output : Array ( [0] =&gt; Harsh [1] =&gt; Nishant [2] =&gt; Bikash ) Input : $arr = array(1, 2, 6, 7, 8, 9); unset($arr[3]); Output : Array ( [0] =&gt; 1 [1] =
2 min read
PHP | Unset() vs Unlink() Function
Both the function are used to do some undo operations but used in different situations cause both acts differently. The unlink() function is used when you want to delete the files completely. The unset() Function is used when you want to make that file empty.Unlink() function: The unlink() function is an inbuilt function in PHP which is used to del
3 min read
PHP | Unset Session Variable
Whenever data are stored using cookies, there is a possibility of a hacker to insert some harmful data in the user's computer to harm any application. So its always advisable to use PHP sessions to store information on the server than on a computer. Whenever data is needed across many pages of a website or application, PHP Session is used. This ses
3 min read
CSS var() Function
The var() function in CSS is used to insert a value for a custom property. Syntax: var( custom_property, value ) Parameters: This function accepts two parameters which are listed below: custom_property: It is the required parameter. The name of the custom property must start with two dashes(--).value: It is an optional parameter. It is used if the
1 min read
What is the use of curly brackets in the `var { ... } = …` statements ?
Destructuring assignment allows us to assign the properties of an array or object to a bunch of variables that are sometimes very convenient and short. Consider the following illustration. Both of the below-mentioned methods are right and produce the same result. Without Destructuring: var array = [1, 20, 40]; var first = array[0] var second = arra
2 min read
three90RightbarBannerImg