isset() function in PHP
By Emiley J. Viewed: 31888 times Emailed: 184 times Printed: 199 times
isset() determines whether a certain variable has already been declared by PHP. It returns a boolean value true if the variable has already been set, and false otherwise, or if the variable is set to the value NULL.
Consider the following script:
if (isset($first_name)) {
print '$first_name is set';
}
This code snippet checks whether the variable $first_name is defined. If $first_name is defined, isset() returns true , which will display ' $first_name is set. ' If it isn’t, no output is generated.
isset() can also be used on array elements and object properties. Here are examples for the relevant syntax, which you can refer to later:.
Checking an array element:
if (isset($arr["offset"])) {
...
}
Checking an object property:
if (isset($obj->property)) {
...
}
Note that in both examples, we didn’t check if $arr or $obj are set (before we checked the offset or property, respectively). The isset() construct returns false automatically if they are not set.
isset() is the only one of the three language constructs that accepts an arbitrary amount of parameters. Its accurate prototype is as follows:
isset($var1, $var2, $var3, ...);
It only returns true if all the variables have been defined; otherwise, it returns false. This is useful when you want to check if the required input variables for your script have really been sent by the client, saving you a series of single isset() checks.
Comments(4)
| 1. | Simple and straightforward. Thats what I like about this site. |
| 2. | Thanks, how about using isset() function on page/UR? e.g. if(isset($page)) {} ??? What is it for? |
| 3. | Fine,you r very straightforward I like that |
| 4. | i can understand easily what is isset but i am not clear in array example (second one) partially understood |
Latest Tutorials
More Latest News
Most Viewed Articles (in last 30 days)

