You are reading the article Guide To Use Cases For Unset() Along With Examples updated in September 2023 on the website Lanphuongmhbrtower.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Guide To Use Cases For Unset() Along With Examples
Introduction to PHP unset()The following article provides an outline on PHP unset(). The primary operation of the method unset() is to destroy the variable specified as an input argument for it. In other words, it performs a reset operation on the selected variable. However, its behavior can vary depending on the type of variable that is being targeted to destroy. This function is supported by version PHP4 onwards.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax of PHP unset()
unset(mixed $selectedvar, mixed $selectedvar1,….., mixed $selectedvarN): void
selectedvar: The mandatory argument for unset() method. At least one variable to unset, needs to be given as input argument for the method.
selectedvarN: The optional parameter which can be given as input argument, to unset() method to reset it.
Use Cases for unset()Given below are the different cases:
1. Applying unset() for local variableExample:
Code:
<?php $input = "I have value!!!"; unset($input); echo "The value of 'input' after unset: " . $input;Output:
The value contained in the variable ‘input’ is destroyed on execution of the unset() method.
2. Applying unset for variable inside a function which is global variableWhen user attempts to use Unset for a variable within a function and it is also defined as global variable, then unset() resets only the local one. The global one remains unaffected.
Example:
Code:
<?php function Afunction() { $Avariable = 'local value'; global $Avariable; unset($Avariable); } $Avariable = 'Global Value'; Afunction();Output:
The local version of the variable ‘Avariable’ is destroyed where as the global version remains intact.
3. Applying unset for global variable within a functionIf the variable within the function is also declared as global variable and user needs to destroy the global one, then it can be achieved using the array[$GLOBAL].
Example:
Code:
<?php function Afunction() { $Avariable = 'local value'; global $Avariable; unset($GLOBALS['Avariable']); } $Avariable = 'Global Value'; Afunction();Output:
The local version of the variable ‘Avariable’ is not affected by the execution of the unset function whereas the global version of the variable is set to null value.
4. Applying unset() for pass by reference variableIf unset() is called on a variable that is passed to the function as reference, unset() resets only the local one. The variable instance in the calling environment retains as it is.
Example:
Code:
<?php function Afunction(&$Avariable) { $Avariable = 'Internal value'; unset($Avariable); } $Avariable = 'External Value'; Afunction($Avariable);Output:
The unset() method called in the pass by reference variable ‘Avariable’ resets only the content of the variable in the local scope without affecting the content from the external scope.
5. Applying unset() for static variableWhen a static variable is set as input argument to unset() method, the variable gets reset for the remaining command in the function scope after the function unset() is called.
Example:
Code:
<?php function UnsetStatic() { static $staticvar; $staticvar++; unset($staticvar); } UnsetStatic(); UnsetStatic(); UnsetStatic();Output:
6. Applying unset() on an array elementThe application of unset() method on an array element deletes the element from the array without exhibiting re-indexing operation.
Example:
Code:
<?php unset($arrayinput[1]); Echo $arrayinput[0]." ". $arrayinput[1]." ". $arrayinput[2]." ";Output:
7. Applying unset() on more than one element at a timeThe unset() method supports deleting multiple variable at once.
Example:
Code:
<?php $input1 = "I am value1"; $input2 = "I am value2"; $input3 = "I am value3"; unset($input1,$input2,$input3);Output:
Note: (unset) casting is not same as the function unset(). (unset)casting is used only as cast of type NULL whereas unset() method alters the variable. unset() is a language construct and hence is not supported by variable function. unset() method can be used to reset object properties that are visible in the current scope except ‘$this’ variable within any object method. In order to perform unset operation on the object properties that are not accessible in the current scope, an overloading method __unset() needs to be declared and called.
Recommended ArticlesThis is a guide to PHP unset(). Here we discuss the introduction to use cases for unset() along with examples for better understanding. You may also have a look at the following articles to learn more –
PHP References
PHP Data Object
PHP MD5()
PHP chop()
You're reading Guide To Use Cases For Unset() Along With Examples
Update the detailed information about Guide To Use Cases For Unset() Along With Examples on the Lanphuongmhbrtower.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!