PHP 설정 해제()

王林
풀어 주다: 2024-08-29 12:53:09
원래의
980명이 탐색했습니다.

다음 기사에서는 PHP unset()에 대한 개요를 제공합니다. unset() 메서드의 기본 작업은 해당 메서드의 입력 인수로 지정된 변수를 삭제하는 것입니다. 즉, 선택한 변수에 대해 재설정 작업을 수행합니다. 그러나 해당 동작은 삭제하려는 변수의 유형에 따라 달라질 수 있습니다. 이 기능은 PHP4 이후 버전에서 지원됩니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

PHP unset() 구문

unset(mixed $selectedvar, mixed $selectedvar1,….., mixed $selectedvarN): void
로그인 후 복사
  • selectedvar: unset() 메소드의 필수 인수입니다. 설정을 해제할 하나 이상의 변수를 메소드의 입력 인수로 제공해야 합니다.
  • 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 설정 해제()

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 설정 해제()

3. 함수 내 전역 변수에 설정 해제 적용

함수 내의 변수도 전역 변수로 선언되어 있고 사용자가 전역 변수를 삭제해야 하는 경우 배열[$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 설정 해제()

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 설정 해제()

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 설정 해제()

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 설정 해제()

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)캐스팅은 NULL 유형의 캐스트로만 사용되는 반면 unset() 메소드는 변수를 변경합니다. unset()은 언어 구성이므로 변수 함수에서 지원되지 않습니다. unset() 메소드는 객체 메소드 내의 '$this' 변수를 제외하고 현재 범위에 표시되는 객체 속성을 재설정하는 데 사용할 수 있습니다. 현재 범위에서 접근할 수 없는 객체 속성에 대해 설정 해제 작업을 수행하려면 오버로딩 메서드 __unset()을 선언하고 호출해야 합니다.

위 내용은 PHP 설정 해제()의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!