What is isset and unset in php?
❮ PHP Variable Handling Reference Show
ExampleCheck whether a variable is empty. Also check whether the variable is set/declared: $a = 0; $b = null; Try it Yourself » Definition and UsageThe isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false. Note: If multiple variables are supplied, then this function will return true only if all of the variables are set. Tip: A variable can be unset with the unset() function. SyntaxParameter Values
Technical Details
❮ PHP Variable Handling Reference
Purpose of isset in PHPThe isset function is used to check if a variable is set or not. That means it determines if a variable is assigned a value and is not null. Also, the isset PHP function checks if the given variable is not unset by using the unset function. Syntax of using the isset function This is how you can use the isset function:
The PHP isset function returns true if a variable is set and false if it is not or being unset by using the unset function. Example of using isset in PHPIn this example, we have simply declared a string variable and assigned it “” value. Although it does not contain any character, but as it is being set, see the output by clicking the image or link below: See online demo and codeYou can see, the output is “The variable is set” although it contains zero character. The isset function will return as true because we had set it. Example without setting a variableIn that example, we will not assign it any value, not even “”. We have just declared that string variable and see the output: See online demo and codeSo that resulted in the “The variable is not set”, even we had declared it. Now look at an example of isset with 2 variables. isset example with two variablesAs mentioned earlier, you can specify one or more variables in the isset function. However, if any variable is not set or had been sunset by using the unset function or it is a Null, it will return as false. In that case even other variables are set, the function will still return the false. See the following example and output: See online demo and codeYou see although we have set the first variable while the second variable was only declared and not given any value. The isset function returned false with the output: “The variables are not set” An example with array elementsNot only you can use isset function with simple variables to determine if those are set or not but you can also use it to check array elements as well. In the following example, an associative array of three elements is created. The first and third elements are assigned values while the second element is set to null. Then we used a foreach loop to iterate through the array elements. Inside the foreach loop, the isset function is used to check if the current element is set or null. If that is set, the true message will be shown otherwise false along with array key and element values. See the example online with code by following the link or image: See online demo and codeSyntax of unset functionFollowing is the general syntax to use PHP unset function:
You may specify one or more variable to be destroyed by using the unset in a single call. An unset exampleIn this example, a variable was declared and assigned a value. After that, it was unset by using the unset function, so it was destroyed. See the code and output before and after using the unset function. See online demo and codeWhy we use isset and unset functionsThere are a few solid reasons to use the isset function in PHP programs. One of the reasons is, while you use a lot of variables in your PHP programs, it is generally a good practice to check variables if those are set or not before using those. Basically, it checks whether a variables or index exists or not. PHP generates notice if you think a variable exists while actually it is not. By using the PHP isset function, you may avoid any unwanted notices. The unset PHP function The unset function of PHP is used to destroy a given variable. How to use PHP isset functionYou have to specify a variable to set
inside the isset function. As isset returns a Boolean value (true or false), you can assign it to a Boolean variable as shown below:
You may specify more than one variable in the isset function. In that case, the isset function will return true if all variables are set otherwise it will return false.
Also note, if you have used unset function to destroy a variable, it cannot be set by using the isset function. The examples are shown in the above section. Using unset functionThe unset function is quite simple to use. Simply specify variable(s) name inside the unset function as follows:
A few points about the unset function:
What is the use of isset and unset in PHP?PHP isset() Function
This function returns true if the variable exists and is not NULL, otherwise it returns false. Note: If multiple variables are supplied, then this function will return true only if all of the variables are set. Tip: A variable can be unset with the unset() function.
What is unset () in PHP?unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.
What is Isset PHP?The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.
What is the difference between isset and empty in PHP?The isset() function is an inbuilt function in PHP that is used to determine if the variable is declared and its value is not equal to NULL. The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not.
|