The offsetExists() function of the ArrayObject class in PHP is used to whether a given offset or index is present in the ArrayObject or not. If it is present then the function returns a boolean True value otherwise it returns False.
Syntax:
bool offsetExists($index)
Parameters: This function accepts a single parameter $index which is the index which is to be checked if it is present in the ArrayObject.
Return Value: This function returns a boolean value True or False based on whether the index is present in the ArrayObject or not.
Below programs illustrate the above function:
Program 1:
<?php // PHP program to illustrate the // offsetExists() function $arr = array("geeks100", "geeks99", "geeks1", "geeks02"); // Create array object $arrObject = new ArrayObject($arr); // Print the ArrayObject print_r($arrObject); // Check if the Key 1 is present if($arrObject->offsetExists(1)) echo "\nThe key 1 is present!"; else echo "\nThe key 1 is not present!"; // Check if the Key 20 is present if($arrObject->offsetExists(20)) echo "\nThe key 20 is present!"; else echo "\nThe key 20 is not present!"; ?> |
ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[0] => geeks100
[1] => geeks99
[2] => geeks1
[3] => geeks02
)
)
The key 1 is present!
The key 20 is not present!
Program 2:
<?php // PHP program to illustrate the // offsetExists() function $arr = array("Welcome"=>"1", "to" => "2", "GfG" => "3"); // Create array object $arrObject = new ArrayObject($arr); // Print the ArrayObject print_r($arrObject); // Check if the Key "Welcome" is present if($arrObject->offsetExists("Welcome")) echo "\nThe key Welcome is present!"; else echo "\nThe key Welcome is not present!"; // Check if the Key GfG is present if($arrObject->offsetExists("GfG")) echo "\nThe key GfG is present!"; else echo "\nThe key GfG is not present!"; ?> |
ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[Welcome] => 1
[to] => 2
[GfG] => 3
)
)
The key Welcome is present!
The key GfG is present!
Reference: http://php.net/manual/en/arrayobject.offsetexists.php
Recommended Posts:
- PHP | SplDoublyLinkedList offsetExists() Function
- PHP | SplObjectStorage offsetExists() Function
- PHP | SplFixedArray offsetExists() Function
- PHP | ArrayIterator offsetExists() Function
- ArrayObject uksort() Function in PHP
- ArrayObject append() function in PHP
- ArrayObject asort() Function in PHP
- ArrayObject exchangeArray() function in PHP
- ArrayObject getArrayCopy() Function in PHP
- ArrayObject getIterator() Function in PHP
- ArrayObject natsort() Function in PHP
- ArrayObject natcasesort() Function in PHP
- ArrayObject ksort() Function in PHP
- ArrayObject getIteratorClass() Function in PHP
- ArrayObject offsetGet() Function in PHP
- ArrayObject offsetSet() Function in PHP
- ArrayObject offsetUnset() Function in PHP
- ArrayObject uasort() Function in PHP
- PHP | ArrayObject setFlags() Function
- PHP | ArrayObject serialize() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

