PHP isset() Function
The isset() function in PHP is used to determine if a variable is set and is not NULL. It returns a boolean value of true if the variable exists and has a value other than NULL, and false otherwise.
Syntax
The syntax for the isset() function in PHP is as follows:
isset($variable)
Example
<?php
$name = "John";
$age = 25;
if(isset($name)) {
echo "Name is set. ";
}
if(isset($gender)) {
echo "Gender is set. ";
}
if(isset($age)) {
echo "Age is set.";
}
?>
Output
Name is set. Age is set.
Explanation
In the above example, the isset() function checks if the variables $name, $gender, and $age are set. Since $name and $age are set, the first and third conditions are true and they are printed, but $gender is not set so the second condition is false and it is not printed.
Use
The isset() function is typically used to check if a form input value is set before processing it and to avoid errors due to uninitialized variables. It can also be used to check if a variable has been assigned a value and to prevent null pointer exceptions.
Important Points
- isset() function returns a boolean value of true or false
- It checks if a variable is set and is not NULL
- It can be used to check if a form input has a value before processing
- It is used to avoid uninitialized variable errors.
Summary
In summary, the isset() function is an important tool in PHP that allows developers to determine whether a variable has been assigned a value or not. With this function, you can check if a form input value is set before processing it and avoid null pointer exceptions.