In PHP, unset() is a commonly used function to destroy variables or elements within an array. When handling superglobal arrays such as $_POST, $_GET, $_SESSION, and others, you might need to remove a specific variable from these arrays. This article provides a detailed guide on how to use unset() to remove variables from superglobals and points out some important considerations.
unset() is used to destroy one or more variables. When removing array elements, you can directly use unset() to specify the array key and delete it. The basic syntax is as follows:
<span><span><span class="hljs-keyword">unset</span></span><span>(</span><span><span class="hljs-variable">$array</span></span><span>[</span><span><span class="hljs-variable">$key</span></span><span>]);
</span></span>
For example, suppose we have a $_POST superglobal array containing user-submitted form data. If we want to remove a specific key-value pair (for instance, a key named username), we can do the following:
<span><span><span class="hljs-keyword">unset</span></span><span>(</span><span><span class="hljs-variable">$_POST</span></span><span>[</span><span><span class="hljs-string">'username'</span></span><span>]);
</span></span>
In PHP, $_GET, $_POST, $_SESSION, $_COOKIE, and others are superglobal arrays commonly used to store user input, session information, or browser-related variables. Sometimes, we may need to delete a specific element while processing a request. Here are some common scenarios:
When a URL contains unnecessary query parameters, you can remove them using unset(). For example:
<span><span><span class="hljs-keyword">unset</span></span><span>(</span><span><span class="hljs-variable">$_GET</span></span><span>[</span><span><span class="hljs-string">'id'</span></span><span>]);
</span></span>
This will remove the id parameter from $_GET.
Session management often uses the $_SESSION array to store user login status and other information. If a user logs out, we can remove specific session variables:
<span><span><span class="hljs-keyword">unset</span></span><span>(</span><span><span class="hljs-variable">$_SESSION</span></span><span>[</span><span><span class="hljs-string">'user'</span></span><span>]);
</span></span>
To remove the entire session, you can use session_destroy().
Although unset() cannot directly remove a cookie from the browser, you can “delete” it by setting an expired cookie:
<span><span><span class="hljs-title function_ invoke__">setcookie</span></span><span>(</span><span><span class="hljs-string">'username'</span></span><span>, </span><span><span class="hljs-string">''</span></span><span>, </span><span><span class="hljs-title function_ invoke__">time</span></span><span>() - </span><span><span class="hljs-number">3600</span></span><span>); </span><span><span class="hljs-comment">// Set cookie to expire one hour ago</span></span><span>
</span></span>
This causes the user’s browser to remove the username cookie.
Although unset() is a powerful tool, there are some details to keep in mind:
unset() can remove a specific element within a superglobal array, but it does not delete the entire superglobal. For example, the following code:
<span><span><span class="hljs-keyword">unset</span></span><span>(</span><span><span class="hljs-variable">$_POST</span></span><span>);
</span></span>
does not delete the entire $_POST array; it only destroys the reference. The $_POST array itself still exists and can be accessed.
$_GLOBALS is used to access and modify global variables. Because $_GLOBALS is part of PHP’s internal implementation, unset() cannot remove elements from it. This is due to $_GLOBALS being tightly bound to PHP’s runtime global environment.
If a variable is passed by reference, using unset() to remove an array element does not directly affect that reference. For example:
<span><span><span class="hljs-variable">$a</span></span><span> = </span><span><span class="hljs-number">10</span></span><span>;
</span><span><span class="hljs-variable">$b</span></span><span> = &</span><span><span class="hljs-variable">$a</span></span><span>;
</span><span><span class="hljs-keyword">unset</span></span><span>(</span><span><span class="hljs-variable">$a</span></span><span>); </span><span><span class="hljs-comment">// $a is deleted</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$b</span></span><span>; </span><span><span class="hljs-comment">// Outputs 10 because $b still references $a</span></span><span>
</span></span>
Be careful when using unset() to delete array elements inside a foreach loop. foreach creates a copy of the current array element, so deleting elements directly within the loop may not work as expected. For example:
<span><span><span class="hljs-keyword">foreach</span></span><span> (</span><span><span class="hljs-variable">$_POST</span></span><span> </span><span><span class="hljs-keyword">as</span></span><span> </span><span><span class="hljs-variable">$key</span></span> => </span><span><span class="hljs-variable">$value</span></span><span>) {
</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-variable">$key</span></span><span> == </span><span><span class="hljs-string">'username'</span></span><span>) {
</span><span><span class="hljs-keyword">unset</span></span><span>(</span><span><span class="hljs-variable">$_POST</span></span><span>[</span><span><span class="hljs-variable">$key</span></span><span>]);
}
}
</span></span>
Although unset() removes $_POST['username'], the foreach loop already created a copy of the array during iteration. As a result, the $_POST array remains unchanged after the loop. To avoid this, consider collecting the elements to be deleted in a separate array and removing them after the loop, or use array_filter() for filtering.
In PHP, unset() is a convenient method to remove variables from superglobal arrays. By using unset() correctly, you can manage superglobal data effectively, ensuring that only necessary data remains in the arrays. However, pay attention to the details to avoid accidentally deleting variables that should remain or introducing logical errors.