Let's consider the function:

function foo() {
    static bar++;
    unset($bar);
}
foo(); //static $bar is 1
foo(); //static $bar is 2

The function compiles to:

function name:  foo
number of ops:  11
compiled vars:  !0 = $bar
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   2     0  >   EXT_NOP                                                  
   4     1      EXT_STMT                                                 
         2      FETCH_W                      static              $0      'bar'
         3      ASSIGN_REF                                               !0, $0
   5     4      EXT_STMT                                                 
         5      POST_INC                                         ~1      !0
         6      FREE                                                     ~1
   6     7      EXT_STMT                                                 
         8      UNSET_VAR                                                !0
   7     9      EXT_STMT                                                 
        10    > RETURN                                                   null

A variable actually exists outside each function call to foo() and, on each call, it's fetched and a reference to it is assigned to $bar. In fact, it's very similar to this:

function foo() {
    global bar++;
    unset($bar);
}

When you call unset(), you're only destroying the reference you created, not the underlying value.

I didn't confirm, but what I'd guess that happens is this:

  • The underlying representation of the variabe (the zval) is stored so that its reference count is 1.
  • When foo() is called, the symbol $bar is associated with this zval, its reference count is increased to 2 and the reference flag is set.
  • When unset is called, the zval has its reference count decreased to 1, the reference flag is probably cleared and the symbol $bar is removed.

See reference count basics.

Answer from Artefacto on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.unset.php
PHP: unset - Manual
If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
๐ŸŒ
IcoMoon
icomoon.io
Free SVG Icon Libraries, SVG Editor, Multicolor Icon Font Generator โ IcoMoon
font-palette: unset; - + Baseline widely available ยท The font used in the demo above was generated in the new app, featuring Harmonicons and a subset of Inconsolata, Semi Bold, which was imported and combined with icons from Harmonicons. IcoMoon's library features thousands of high quality, free and open source SVG icon sets.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_casting.asp
PHP Type Casting
Note: The (unset) statement was deprecated in PHP 7.2.0, and removed in PHP 8.0.0.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_var_unset.asp
PHP unset() Function
<?php $a = "Hello world!"; echo "The value of variable 'a' before unset: " . $a . "<br>"; unset($a); echo "The value of variable 'a' after unset: " .
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-unset-and-unlink-in-PHP
What is the difference between 'unset' and 'unlink' in PHP? - Quora
Suppose you have uploaded a file and wants to delete this file through the coding then unlink() function is used to delete the file. unset() is a function for variable management.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-program-delete-element-array-using-unset-function
PHP | Program to delete an element from array using unset() function - GeeksforGeeks
July 11, 2025 - Approach: This idea to solve this problem using the unset function is to pass the array key of the respective element which we want to delete from the array as a parameter to this function and thus removes the value associated to it i.e. the element of an array at that index. Below programs illustrate the above approach: Program 1: ... <?php $a = array("Harsh", "Bikash", "Nishant", "Barun", "Deep"); // unset command accepts 3rd index and // thus removes the array element at // that position unset($a[3]); print_r ($a); ?> Output:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-unset-function
PHP | unset() Function - GeeksforGeeks
April 9, 2025 - In PHP, the unset() function is used to destroy a variable, array element, or object. Once a variable or array element is "unset", it is completely removed from memory, and it no longer exists in the program.
Find elsewhere
๐ŸŒ
Mise-en-place
mise.jdx.dev โ€บ getting-started.html
Getting Started | mise-en-place
2 weeks ago - This will show you how to install mise and get started with it. This is a suitable way when using an interactive shell like bash, zsh, or fish ยท By default, mise will be installed to ~/.local/bin (this is simply a suggestion. mise can be installed anywhere).
๐ŸŒ
Anime Characters Database
animecharactersdatabase.com โ€บ ux_search.php
Visual Search
Get to know which anime/game/manga/etc characters look like you for your cosplay using our Visual Search.
๐ŸŒ
Docker Docs
docs.docker.com โ€บ reference โ€บ compose file reference โ€บ services
Services | Docker Docs
Environment variables can be declared by a single key (no value to equals sign). In this case Compose relies on you to resolve the value. If the value is not resolved, the variable is unset and is removed from the service container environment.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-php-unset-function
What is the PHP unset() function?
The unset() function in PHP resets any variable. If unset() is called inside a user-defined function, it unsets the local variables. If a user wants to unset the global variable inside the function, then he/she has to use ๏ผ„GLOBALS array to do so.
๐ŸŒ
Moodle
docs.moodle.org โ€บ 501 โ€บ en โ€บ Moodle_app_FAQ
Moodle app FAQ - MoodleDocs
1 month ago - This error message indicates that that plugin is not correctly installed (or uninstalled) on the Moodle site you use (see Installing plugins). Please review the mod/certificate folder to check if the version.php file is missing (or has incorrect permissions) as well as purge cache too.
๐ŸŒ
Blue Light Card
support.bluelightcard.co.uk โ€บ hc โ€บ en-gb
Blue Light Card
Logging In Need help logging in? Hereโ€™s where youโ€™ll find everything you need to get into your account and manage it with ease ยท Offers Discover thousands of exclusive offers and discounts available only to Blue Light Card members ยท My Account/Card View and manage your Blue Light Card ...
๐ŸŒ
Homebrew
docs.brew.sh โ€บ Installation
Installation โ€” Homebrew Documentation
The default Git remote will be used if the corresponding environment variable is unset and works best for most users.
๐ŸŒ
Kubernetes
kubernetes.io โ€บ docs โ€บ tasks โ€บ inject-data-application โ€บ define-environment-variable-container
Define Environment Variables for a Container | Kubernetes
June 28, 2025 - This page shows how to define environment variables for a container in a Kubernetes Pod. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Douglas_Dare
Douglas Dare - Wikipedia
December 29, 2025 - Douglas Samuel Charles Dare (born 6 June 1990) is an English singer-songwriter and pianist based in London. Dare is from Bridport in Dorset. He is the son of a piano teacher and studied music at the Liverpool Institute for Performing Arts, where ...