PHPのお勉強!

PHP TOP

unset

(PHP 4, PHP 5, PHP 7, PHP 8)

unset指定した変数の割当を解除する

説明

unset(mixed $var, mixed ...$vars): void

unset() は指定した変数を破棄します。

関数 unset() の内部動作は、 破棄しようとする変数の型に依存します。

あるグローバル変数が関数の中で unset() された場合、ローカル変数のみが破棄されます。呼出側の環境にある変数は、 unset() がコールされる前と同じ値を保持します。

<?php
function destroy_foo()
{
global
$foo;
unset(
$foo);
}

$foo = 'bar';
destroy_foo();
echo
$foo;
?>

上の例の出力は以下となります。

bar

グローバル変数を関数の内部で unset() するには、 $GLOBALS 配列を使います。

<?php
function foo()
{
unset(
$GLOBALS['bar']);
}

$bar = "something";
foo();
?>

参照渡しされた変数が関数内で unset() された場合に、 ローカル変数のみが破棄されます。呼出側の環境でその変数は、 unset() がコールされる前と同じ値を保持します。

<?php
function foo(&$bar)
{
unset(
$bar);
$bar = "blah";
}

$bar = 'something';
echo
"$bar\n";

foo($bar);
echo
"$bar\n";
?>

上の例の出力は以下となります。

something
something

static変数が関数の内部で unset() された場合、 unset() は、その関数の残りのコンテキスト内においてのみ 変数を破棄します。関数を再度コールすると、破棄する前の値が復元されます。

<?php
function foo()
{
static
$bar;
$bar++;
echo
"Before unset: $bar, ";
unset(
$bar);
$bar = 23;
echo
"after unset: $bar\n";
}

foo();
foo();
foo();
?>

上の例の出力は以下となります。

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23

パラメータ

var

破棄する変数。

vars

別の変数。

戻り値

値を返しません。

例1 unset() の例

<?php
// 変数を一つ破棄する
unset($foo);

// 配列の要素の一つを破棄する
unset($bar['quux']);

// 複数の変数を破棄する
unset($foo1, $foo2, $foo3);
?>

例2 (unset) によるキャスト

(unset) によるキャストは unset() 関数と混同されがちです。 (unset) によるキャストは、 単に NULL 型へのキャストを徹底するだけであり、 キャストした変数の値を書き換えるわけではありません。 (unset) によるキャストは、PHP 7.2.0 で非推奨になり、 PHP 8.0.0 で削除されました。

<?php
$name
= 'Felipe';

var_dump((unset) $name);
var_dump($name);
?>

上の例の出力は以下となります。

NULL
string(6) "Felipe"

注意

注意: これは、関数ではなく 言語構造のため、可変関数名前付き引数 を用いてコールすることはできません。

注意:

現在のコンテキストで見えるものであれば、 オブジェクトのプロパティも破棄することが可能です。

破棄されたプロパティにアクセスする場合、 もし宣言されていれば __get() がコールされます。 破棄されたプロパティに値を設定を設定する場合、 もし宣言されていれば __set() がコールされます。

注意:

オブジェクトのメソッド内で $this を破棄することはできません。

注意:

オブジェクトのアクセス不能なプロパティに対して unset() を使用した場合は、もしオーバーロードメソッド __unset() が宣言されていればそれをコールします。

参考

  • isset() - 変数が宣言されていること、そして null とは異なることを検査する
  • empty() - 変数が空であるかどうかを検査する
  • __unset()
  • array_splice() - 配列の一部を削除し、他の要素で置換する
  • (unset) casting

add a note

User Contributed Notes 30 notes

up
678
Phantasma_9 at yahoo dot com
8 years ago
This is probably trivial but there is no error for unsetting a non-existing variable.
up
15
tecdoc at ukr dot net
2 years ago
Be careful!, unset() element in array advances the internal array pointer one place forward
up
202
Hayley Watson
11 years ago
You don't need to check that a variable is set before you unset it.
<?php
unset($a);
?>
is harmless.
<?php
if(isset($a)) {
unset(
$a);
}
?>
is pointless complication.

This doesn't apply to properties of objects that have __isset() methods that visibly change object state or __unset() methods that don't properly check their arguments or have extra side effects.

The latter case means that __unset shouldn't do more than what it says on the tin, and also has the responsibility for checking (possibly using __isset()) that what it's being asked to do makes sense.

The former case is just plain bad design.
up
51
nox at oreigon dot de
16 years ago
if you try to unset an object, please be careful about references.

Objects will only free their resources and trigger their __destruct method when *all* references are unsetted.
Even when they are *in* the object... sigh!

<?php

class A {
function
__destruct() {
echo
"cYa later!!\n";
}
}

$a = new A();
$a -> a = $a;
#unset($a); # Just uncomment, and you'll see

echo "No Message ... hm, what now?\n";
unset(
$a -> a);
unset(
$a);

echo
"Finally that thing is gone\n";

?>

Of course the object completely dies at the end of the script.
up
43
Kai Kunstmann
16 years ago
Since unset() is a language construct, it cannot be passed anything other than a variable. It's sole purpose is to "unset" this variable, ie. to remove it from the current scope and destroy it's associated data. This is true especially for reference variables, where not the actual value is destroyed but the reference to that value. This is why you can't wrap 'unset()' in a user defined function: You would either unset a copy of the data if the parameter is passed by value, or you would just unset the reference variable within the functions scope if the parameter is passed by reference. There is no workaround for that, as you cannot pass 'scope' to a function in PHP. Such a function can only work for variables that exist in a common or global scope (compare 'unset($_GLOBALS[variable])').

I don't know how PHP handles garbage collection internally, but I guess this behavior can result in a huge memory leak: if a value variable goes out of scope with a second variable still holding a reference to the in-memory value, then unsetting that reference would still hold the value in memory but potentially unset the last reference to that in-memory data, hence: occupied memory that is rendered useless as you cannot reference it anymore.
up
37
j dot vd dot merwe at enovision dot net
14 years ago
A sample how to unset array elements from an array result coming from a mysql request. In this sample it is checking if a file exists and removes the row from the array if it not exists.

<?php
$db
->set_query("select * from documents where document_in_user = 0"); //1
$documents = $db->result_to_array($db->get_result()); //1

foreach ($documents as $key => $row) { //2

$file = "uploads/".rawurldecode($row['document_name']);

if (
file_exists ( $file ) == FALSE ) {
unset(
$documents[$key]); //3
}
}

$documents = array_values($documents); // reindex the array (4)
?>

variables:
mysql table = documents,
array = $documents
array key (index) = $key
array row (record sort of speak) = $row

explanation:

1.
it gets the array from the table (mysql)

2.
foreach goes through the array $documents

3.
unset if record does not exist

4.
the array_values($documents) reindexes the $documents array, for otherwise you might end up in trouble when your process will start expecting an array starting with key ($key) 0 (zero).
up
33
franckraynal at free dot fr
19 years ago
Here is another way to make 'unset' work with session variables from within a function :

<?php
function unsetSessionVariable ($sessionVariableName) {
unset(
$GLOBALS[_SESSION][$sessionVariableName]);
}
?>

May it work with others than me...
F.
up
32
andre at twg dot com dot au
20 years ago
Only This works with register_globals being 'ON'.

unset( $_SESSION['variable'] );

The above will not work with register_globals turned on (will only work outside of a function).

$variable = $_SESSION['variable'];
unset( $_SESSION['variable'], $variable );

The above will work with register_globals on & inside a function
up
28
hessodreamy at gmail dot com
17 years ago
To clarify what hugo dot dworak at gmail dot com said about unsetting things that aren't already set:

unsetting a non-existent key within an array does NOT throw an error.
<?
$array = array();

unset($array[2]);
//this does not throw an error

unset($array[$undefinedVar]);
//Throws an error because of the undefined variable, not because of a non-existent key.
?>
up
26
anon at no spam dot no address dot com
20 years ago
Adding on to what bond at noellebond dot com said, if you want to remove an index from the end of the array, if you use unset, the next index value will still be what it would have been.

Eg you have
<?php
$x
= array(1, 2);

for (
$i = 0; $i < 5; $i++)
{
unset(
$x[(count($x)-1)]); //remove last set key in the array

$x[] = $i;
}
?>

You would expect:
Array([0] => 1, [1] => 4)
as you want it to remove the last set key....

but you actually get
Array ( [0] => 1 [4] => 2 [5] => 3 [6] => 4 )

This is since even though the last key is removed, the auto indexing still keeps its previous value.

The only time where this would not seem right is when you remove a value off the end. I guess different people would want it different ways.

The way around this is to use array_pop() instead of unset() as array_pop() refreshes the autoindexing thing for the array.
<?php
$x
= array(1, 2);

for (
$i = 0; $i < 5; $i++)
{
array_pop($x); // removes the last item in the array

$x[] = $i;
}
?>

This returns the expected value of x = Array([0] => 1, [1] => 4);

Hope this helps someone who may need this for some odd reason, I did.
up
21
pfreilly at umd dot umich dot edu
13 years ago
Despite much searching, I have not yet found an explanation as to how one can manually free resources from variables, not so much objects, in PHP. I have also seen many comments regarding the merits and demerits of unset() versus setting a variable to null. Thus, here are the results of some benchmarks performed comparing unset() of numerous variables to setting them to null (with regards to memory usage and processing time):

10 variables:
Unset:
Memory Usage: 296
Time Elapsed: 1.0013580322266E-5

Null set:
Memory Usage: 1736
Time Elapsed: 5.9604644775391E-6

50 variables:
Unset:
Memory Usage: 296
Time Elapsed: 3.6001205444336E-5

Null set:
Memory Usage: 8328
Time Elapsed: 3.2901763916016E-5

100 variables:
Unset:
Memory Usage: 296
Time Elapsed: 5.6982040405273E-5

Null set:
Memory Usage: 15928
Time Elapsed: 5.8174133300781E-5

1000 variables:
Unset:
Memory Usage: 296
Time Elapsed: 0.00041294097900391

Null set:
Memory Usage: 168096
Time Elapsed: 0.00067591667175293

10000 variables:
Unset:
Memory Usage: 296
Time Elapsed: 0.0042569637298584

Null set:
Memory Usage: 1650848
Time Elapsed: 0.0076930522918701

100000 variables:
Unset:
Memory Usage: 296
Time Elapsed: 0.042603969573975

Null set:
Memory Usage: 16249080
Time Elapsed: 0.087724924087524

300000 variables:
Unset:
Memory Usage: 296
Time Elapsed: 0.13177299499512

Null set:
Memory Usage: 49796320
Time Elapsed: 0.28617882728577

Perhaps my test code for the null set was flawed, but despite that possibility it is simple to see that unset() has minimal processing time impact, and no apparent memory usage impact (unless the values returned by memory_get_usage() are flawed). If you truly care about the ~4 microseconds saved over <50 variables, more power to you. Otherwise, use unset() to minimize script impact on your system.
Note: Tested on PHP 5.3.8 installed via RPM on Fedora 14
up
20
clark at everettsconsulting dot com
19 years ago
In PHP 5.0.4, at least, one CAN unset array elements inside functions from arrays passed by reference to the function.
As implied by the manual, however, one can't unset the entire array by passing it by reference.

<?php
function remove_variable (&$variable) // pass variable by reference
{
unset(
$variable);
}

function
remove_element (&$array, $key) // pass array by reference
{
unset(
$array[$key]);
}

$scalar = 'Hello, there';
echo
'Value of $scalar is: ';
print_r ($scalar); echo '<br />';
// Value of $scalar is: Hello, there

remove_variable($scalar); // try to unset the variable
echo 'Value of $scalar is: ';
print_r ($scalar); echo '<br />';
// Value of $scalar is: Hello, there

$array = array('one' => 1, 'two' => 2, 'three' => 3);
echo
'Value of $array is: ';
print_r ($array); echo '<br />';
// Value of $array is: Array ( [one] => 1 [two] => 2 [three] => 3 )

remove_variable($array); // try to unset the array
echo 'Value of $array is: ';
print_r ($array); echo '<br />';
// Value of $array is: Array ( [one] => 1 [two] => 2 [three] => 3 )

remove_element($array, 'two'); // successfully remove an element from the array
echo 'Value of $array is: ';
print_r ($array); echo '<br />';
// Value of $array is: Array ( [one] => 1 [three] => 3 )

?>