ArrayObject offsetExists() Function in PHP
Last Updated :
26 Mar, 2019
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...