- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP unset() | Unset a Variable
The PHP unset() function is used when we need to unset a variable. For example:
<?php $x = "fresherearth.com"; echo "The value of \$x is $x"; unset($x); echo "The value of \$x is $x"; ?>
The output produced by the above PHP example on the unset() function is:
That is:
PHP unset() Syntax
The syntax of the unset() function in PHP is:
unset(variableOne, variableTwo, variableThree, ..., variableN);
At least one variable is required. All the variables given as parameters will get unset.
PHP unset a session variable
The PHP unset() function is used most of the time to unset a session variable. So to unset a session variable, follow this way:
unset($_SESSION['login']);
Now the variable login is unset or deleted from the session.
Remove an element from an array using unset() in PHP
unset() can be used to remove an element from an array in PHP. As an example, I'm going to remove the second element (or the element at index number 1) from the array "$fruits". Keep in mind that the array's index always starts with 0. Meaning that $fruits[0] is equal to the first element, $fruits[1] is equal to the second element, and so on.
<?php
$fruits = array("apple", "grape", "orange", "banana");
print_r($fruits);
echo "<hr>";
// The following statement removes the second element ("grape") from the array.
unset($fruits[1]);
print_r($fruits);
?>
Array ( [0] => apple [1] => grape [2] => orange [3] => banana )
Array ( [0] => apple [2] => orange [3] => banana )
The step-by-step explanation of the above PHP example is as follows:
$fruits = array("apple", "grape", "orange", "banana");
initializes an array named $fruits with four values: "apple", "grape", "orange", and "banana".print_r($fruits);
outputs the contents of the $fruits array. The print_r() function is used to display the array's elements in a readable format.echo "<hr>";
prints a horizontal line to visually separate the two arrays.unset($fruits[1]);
deletes the element at index 1 of the array $fruits. "grape" will be removed from the array in this case.print_r($fruits);
prints the contents of the $fruits array after removing the "grape" element.
Advantages of the unset() function in PHP
- Memory management: unset() frees the variable's associated memory. This is useful when a large amount of memory is being used and the variable is no longer required.
- Performance: unset() can improve script performance by reducing memory usage and increasing execution speed.
- Unwanted data removal: The unset() function can be used to remove unwanted data from an array or object.
Disadvantages of the unset() function in PHP
- Unintended consequences: If unset() is used incorrectly, it can have unintended consequences. If an array is unset, for example, all of its elements are lost.
- Traceability is difficult: Once a variable is unset, it cannot be traced back. This can make debugging problems in your code difficult.
- Data loss risk: If unset() is used incorrectly, it can result in the loss of important data. This can be a serious issue, especially in manufacturing environments.
« Previous Tutorial Next Tutorial »