PHP | ArrayIterator offsetExists() Function
Last Updated :
21 Nov, 2019
The ArrayIterator::offsetExists() function is an inbuilt function in PHP which is used to check the existence of offset at the given index.
Syntax:
bool ArrayIterator::offsetExists( mixed $index )
Parameters: This function accepts single parameter $index which holds the index value to check the existence of offset.
Return Value: This function returns TRUE if the offset exists, otherwise returns FALSE.
Below programs illustrate the ArrayIterator::offsetExists() function in PHP:
Program 1:
<?php
$arrItr = new ArrayIterator(
array (
"a" => 4,
"b" => 2,
"g" => 8,
"d" => 6,
"e" => 1,
"f" => 9
)
);
var_dump( $arrItr ->offsetGet( "a" ));
var_dump( $arrItr ->offsetExists( "a" ));
var_dump( $arrItr ->offsetUnset( "a" ));
var_dump( $arrItr ->offsetExists( "a" ));
?>
|
Output:
int(4)
bool(true)
NULL
bool(false)
Program 2:
<?php
$arrItr = new ArrayIterator(
array (
"for" , "Geeks" , "Science" ,
"Geeks" , "Portal" , "Computer"
)
);
echo $arrItr ->offsetGet(1) . "\n" ;
var_dump( $arrItr ->offsetExists(1));
var_dump( $arrItr ->offsetUnset(1));
var_dump( $arrItr ->offsetExists(1));
echo $arrItr ->offsetGet(0) . "\n" ;
var_dump( $arrItr ->offsetExists(0));
var_dump( $arrItr ->offsetUnset(0));
var_dump( $arrItr ->offsetExists(0));
?>
|
Output:
Geeks
bool(true)
NULL
bool(false)
for
bool(true)
NULL
bool(false)
Reference: https://www.php.net/manual/en/arrayiterator.offsetexists.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...