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
$arr = array ( "geeks100" , "geeks99" , "geeks1" , "geeks02" );
$arrObject = new ArrayObject( $arr );
print_r( $arrObject );
if ( $arrObject ->offsetExists(1))
echo "\nThe key 1 is present!" ;
else
echo "\nThe key 1 is not present!" ;
if ( $arrObject ->offsetExists(20))
echo "\nThe key 20 is present!" ;
else
echo "\nThe key 20 is not present!" ;
?>
|
Output:
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
$arr = array ( "Welcome" => "1" , "to" => "2" , "GfG" => "3" );
$arrObject = new ArrayObject( $arr );
print_r( $arrObject );
if ( $arrObject ->offsetExists( "Welcome" ))
echo "\nThe key Welcome is present!" ;
else
echo "\nThe key Welcome is not present!" ;
if ( $arrObject ->offsetExists( "GfG" ))
echo "\nThe key GfG is present!" ;
else
echo "\nThe key GfG is not present!" ;
?>
|
Output:
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
26 Mar, 2019
Like Article
Save Article