unset
unset(mixed $var, mixed ...$vars): void
Destroys the given variable or array key (a language construct that accepts multiple targets).
C C23
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *p = malloc(sizeof(int));
*p = 1;
free(p);
p = NULL; // idiomatic teardown: null the pointer to avoid dangling references
printf("%d\n", p == NULL);
return 0;
}
1
C++ C++26
#include <iostream>
#include <memory>
int main() {
auto p = std::make_unique<int>(1);
p.reset();
std::cout << std::boolalpha << (p == nullptr) << "\n";
return 0;
}
true
C# v11.0.100
object a = new object();
a = null; // a C# variable cannot be destroyed; drop the reference and let GC collect
Console.WriteLine(a == null);
True
Go v1.27.4
package main
import "fmt"
func main() {
m := map[string]int{"x": 1, "y": 2}
delete(m, "x") // variables themselves cannot be destroyed; delete on a map key is the closest counterpart
fmt.Println(m)
}
map[y:2]
Java v27.0.1
public class Main {
public static void main(String[] args) {
Object a = new Object();
a = null; // Java has no unset either; null the reference and let GC collect
System.out.println(a == null);
}
}
true
JavaScript v26.13.1
// delete works on object properties only; let/const bindings cannot be deleted
const arr = { x: 1, y: 2 };
delete arr.x;
console.log("x" in arr, JSON.stringify(arr));
false {"y":2}
PHP v8.6.0
$a = 1;
unset($a);
$arr = ["x" => 1, "y" => 2];
unset($arr["x"]);
echo json_encode([isset($a), $arr]);
[false,{"y":2}]
Python v3.15.2
a = 1
del a
arr = {"x": 1, "y": 2}
del arr["x"]
print("a" in globals(), arr)
False {'y': 2}
Rust v1.99.1
fn main() {
let mut a: Option<i32> = Some(1);
let taken = a.take(); // Option::take moves the value out and leaves None — the closest unset idiom
println!("{} {:?}", taken.is_some(), a);
}
true None
TypeScript v26.13.1
// delete works on object properties only; let/const bindings cannot be deleted
const arr = { x: 1, y: 2 };
delete arr.x;
console.log("x" in arr, JSON.stringify(arr));
false {"y":2}
Language differences
C: there is no unset — lifetime is governed by scope and storage duration; after free, setting the pointer to NULL is the idiomatic safeguard against dangling pointers (example).
C++: destruction happens automatically at scope exit; manual teardown goes through smart-pointer reset() (example) or an explicit destructor call — there is no construct to destroy a local variable.
C#: variables cannot be destroyed; null out the reference and let GC reclaim it (example); the name stays in scope, only the value becomes null.
Go: variables cannot be destroyed, they are reclaimed at scope exit; the collection-level counterpart is delete(m, key) (example).
Java: like C# — null the reference (example) and let GC do the rest; no mechanism removes a variable binding.
JavaScript: delete works on object properties only (example); let/const bindings cannot be deleted — a key difference from PHP's unset($var) destroying the variable itself; after deletion the property reads undefined (in yields false).
PHP: unset is a language construct that immediately unbinds the variable (reference count drops, memory may be freed); afterwards isset is false and access raises a Warning; deleting an array key never renumbers numeric keys.
Python: del is a true unbinding statement (example) — the name vanishes from the scope and later access raises NameError, the closest behavior to PHP unset; dict keys are deletable too.
Rust: no unset — under ownership semantics values drop automatically when leaving scope; the runtime 'clear' idiom is Option::take() (example), moving the value out and leaving None.
TypeScript: same delete semantics as JavaScript; since TS4 the checker narrows deleted optional properties more precisely (deleting a property whose type does not include undefined is an error).