ホームページ  >  記事  >  バックエンド開発  >  PHP unset()

PHP unset()

王林
王林オリジナル
2024-08-29 12:53:09982ブラウズ

次の記事では、PHP unset() の概要を説明します。メソッド unset() の主な操作は、その入力引数として指定された変数を破棄することです。つまり、選択した変数に対してリセット操作を実行します。ただし、その動作は、破棄の対象となっている変数のタイプに応じて異なる場合があります。この機能は PHP4 以降のバージョンでサポートされています。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

PHP unset() の構文

unset(mixed $selectedvar, mixed $selectedvar1,….., mixed $selectedvarN): void
  • selectedvar: unset() メソッドの必須引数。設定を解除するには、少なくとも 1 つの変数をメソッドの入力引数として指定する必要があります。
  • selectedvarN: リセットするために unset() メソッドに入力引数として指定できるオプションのパラメーターです。

unset() の使用例

以下にさまざまなケースを示します:

1.ローカル変数

にunset()を適用しています

ローカル変数が unset 関数に渡されると、関数は変数をリセットします。

例:

コード:

<?php
$input = "I have value!!!";
echo "The value of 'input' before unset: " . $input . "<br>";
unset($input); //Applying unset() method on $input variable
echo "The value of 'input' after unset: " . $input;
?>

出力:

変数「input」に含まれる値は、unset() メソッドの実行時に破棄されます。

PHP unset()

2.グローバル変数である関数内の変数に unset を適用します

ユーザーが関数内の変数に対して Unset を使用しようとし、その変数がグローバル変数としても定義されている場合、unset() はローカル変数のみをリセットします。グローバルなものは影響を受けません。

:

コード:

<?php
function Afunction()
{
$Avariable = 'local value';
echo "Within the function scope before unset: ".$Avariable."<br>";
global $Avariable;
unset($Avariable); //Deletes the local ‘Avariable’
echo "Within the function scope after unset:  ".$Avariable."<br>";
}
$Avariable = 'Global Value'; //The global ‘Avariable’
echo "Out of the function scope before unset:  ".$Avariable."<br>";
Afunction();
echo "Out of the function scope after unset: ".$Avariable."<br>";
?>

出力:

変数「Avariable」のローカル バージョンは破棄されますが、グローバル バージョンはそのまま残ります。

PHP unset()

3.関数内のグローバル変数に unset を適用する

関数内の変数がグローバル変数としても宣言されており、ユーザーがグローバル変数を破棄する必要がある場合は、array[$GLOBAL] を使用して実現できます。

:

コード:

<?php
function Afunction()
{
$Avariable = 'local value';
echo "Within the function scope before unset: ".$Avariable."<br>";
global $Avariable;
unset($GLOBALS['Avariable']); //Resets the global ‘Avariable’
echo "Within the function scope after unset:  ".$Avariable."<br>";
}
$Avariable = 'Global Value';
echo "Out of the function scope before unset:  ".$Avariable."<br>";
Afunction();
echo "Out of the function scope after unset: ".$Avariable."<br>";
?>

出力:

変数「Avariable」のローカル バージョンは unset 関数の実行の影響を受けませんが、変数のグローバル バージョンは null 値に設定されます。

PHP unset()

4.参照渡し変数に unset() を適用しています

関数に参照として渡される変数に対して unset() が呼び出された場合、unset() はローカルの変数のみをリセットします。呼び出し環境の変数インスタンスはそのまま保持されます。

:

コード:

<?php
function Afunction(&$Avariable) //’Avariable’ is the pass by reference
{
$Avariable = 'Internal value';
echo "Within the function scope before unset: ".$Avariable."<br>";
unset($Avariable); //Resets the local ‘Avariable’
echo "Within the function scope after unset:  ".$Avariable."<br>";
}
$Avariable = 'External Value';
echo "Out of the function scope before unset:  ".$Avariable."<br>";
Afunction($Avariable);
echo "Out of the function scope after unset: ".$Avariable."<br>";
?>

出力:

参照渡し変数「Avariable」で呼び出された unset() メソッドは、外部スコープの内容には影響せず、ローカル スコープ内の変数の内容のみをリセットします。

PHP unset()

5.静的変数

に unset() を適用する

静的変数が unset() メソッドの入力引数として設定されている場合、その変数は関数 unset() の呼び出し後に関数スコープ内の残りのコマンドに対してリセットされます。

:

コード:

<?php
function UnsetStatic()
{
static $staticvar;
$staticvar++;
echo "Before unset() method is called: $staticvar"."<br>";
//Deletes ‘staticvar’ only for the below commands within the scope of this ‘UnsetStatic’ function
unset($staticvar);
echo "after unset() method is called: $staticvar"."<br>";
}
UnsetStatic();
UnsetStatic();
UnsetStatic();
?>

出力:

変数「staticvar」は、unset() メソッドの呼び出し後に続くコマンドに対してのみリセットされます。

PHP unset()

6.配列要素に unset() を適用する

配列要素に unset() メソッドを適用すると、再インデックス操作を行わずに配列から要素が削除されます。

:

コード:

<?php
$arrayinput = [0 => "first", 1 => "second", 2 => "third"];
Echo "The array elements, before unset:"."<br>";
Echo $arrayinput[0]."  ". $arrayinput[1]."  ". $arrayinput[2]."  "."<br>";
//Unset operation is called on the second element of the array ‘arrayinput’
unset($arrayinput[1]);
Echo "The array elements, after unset:"."<br>";
Echo $arrayinput[0]."  ". $arrayinput[1]."  ". $arrayinput[2]."  ";
?>

出力:

PHP unset()

7.一度に複数の要素に unset() を適用する

unset() メソッドは、複数の変数の一度の削除をサポートしています。

:

コード:

<?php
$input1 = "I am value1";
$input2 = "I am value2";
$input3 = "I am value3";
echo "The value of 'input1' before unset:  " . $input1 . "<br>";
echo "The value of 'input2' before unset:  " . $input2 . "<br>";
echo "The value of 'input3' before unset:  " . $input3 . "<br>";
echo "<br>";
//Reseting input1, input2 and input3 together in single command
unset($input1,$input2,$input3);
echo "The value of 'input1' after unset:  " . $input1."<br>";
echo "The value of 'input2' after unset:  " . $input2."<br>";
echo "The value of 'input3' after unset:  " . $input3."<br>";
?>

出力:

PHP unset()

注: (unset) キャストは関数 unset() と同じではありません。 (unset)casting は NULL 型のキャストとしてのみ使用されますが、unset() メソッドは変数を変更します。 unset() は言語構造であるため、変数関数ではサポートされていません。 unset() メソッドを使用すると、オブジェクト メソッド内の「$this」変数を除く、現在のスコープで表示されるオブジェクト プロパティをリセットできます。現在のスコープでアクセスできないオブジェクト プロパティに対して設定解除操作を実行するには、オーバーロード メソッド __unset() を宣言して呼び出す必要があります。

以上がPHP unset()の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:PHP MD5()次の記事:PHP MD5()