![]() ![]() ![]() Want more examples? Check out the official documentation.It's a known flaw in PHP that functions in standard library are inconsistent. In the latter two examples, you can see that you can check the sub-properties of an object without first checking the parent properties – if any parent property is missing at any depth, isset() will return FALSE. Var-dump(isset($car->details->year)) // FAlSE as while the $car object has a 'details' property, the 'year' subproperty is NULL ![]() Var-dump(isset($car->details->colour)) // TRUE as the $car object has a 'details' property with a 'colour' subproperty, which has a value which is not NULL Var_dump(isset($car->wheels)) // FALSE as the $car object does not have a 'wheels' property Var_dump(isset($car->make)) // TRUE as the $car object has a 'make' property which is not NULL Just as isset() can be used to check if an array key is set, it can be used to check whether an object property is set: $car = (object) array('make' => 'ford', 'model' => 'fairlane', 'details' => array('colour' => 'blue', 'year' => NULL)) Example – Checking if an Object has an Attribute/Property I’m using the PHP var_dump() function to print out information on the return value from isset() – find out more here. Var_dump(isset($fruit_colours)) // FALSE as though $fruit_colours contains a value at the array key 'apples', the array stored at 'apples' doesn't contain a value at array key 'gala' Var_dump(isset($fruit_colours)) // TRUE as $fruit_colours contains a value at the array key 'apples', and the array stored at 'apples' has an array key with value 'macintosh' which is not NULL Var_dump(isset($fruit_colours)) // FALSE as though the array key 'lemons' is set, it has a value of NULL ![]() Var_dump(isset($fruit_colours)) // FALSE as the array key 'limes' does not exist inf the $fruit_colours array Var_dump(isset($fruit_colours)) // TRUE as the array key 'oranges' exists in the $fruit_colours array and is not NULL It can also check for deeper array values for multidimensional arrays: $fruit_colours = array('oranges' => 'orange', 'lemons' => NULL, 'apples' => array('granny_smith' => 'green', 'macintosh' => 'red')) Isset() can also be used to check whether a value is set for an array key or position in an array. } Example – Checking if an Element Exists in an Array # The variable $var3 is not set, so isset() returned FALSE # The variable $var2 is set but has a NULL value, so isset() returned FALSE # The variable $var is set and is not NULL, so isset() returned TRUE Here is some simple example usage of the isset() function: $var = 1 FALSE if it is not set or has a value of NULL.At least one variable should be passed to isset().You can provide more variables, separated by commas.Passing a value directly to isset() will result in an error – it must be a variable.It can also be an array element or object attribute if you wish to check whether a value is set in an existing variable – see examples below.Syntax for isset() isset ( $var1, $var2.) So, being able to check that a variable is set and is populated is pretty useful. You can’t perform actions on a variable that doesn’t exist or that has no value – you’ll either get unexpected output or an error. This article shows you how to use the PHP isset() function to check whether a variable exists and is populated. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |