English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
unset() 주어진 변수를 파괴하는 함수입니다。
PHP 버전 요구: PHP 4, PHP 5, PHP 7
void unset ( mixed $var [, mixed $... ] )
파라미터 설명:
반환 값 없음。
<?php // 단일 변수 파괴 unset ($foo); // 단일 배열 요소 파괴 unset ($bar['quux']); // 여러 변수를 파괴 unset($foo1, $foo2, $foo3); ?>
함수 내에서 전역 변수를 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() 하면, 이 변수는 함수 내에서만 파괴되지만, 함수를 다시 호출할 때는 이 변수가 마지막으로 파괴된 값으로 복원됩니다。
<?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