PHP | ArrayIterator offsetExists() Function
Last Updated :
21 Nov, 2019
Improve
The ArrayIterator::offsetExists() function is an inbuilt function in PHP which is used to check the existence of offset at the given index.
Syntax:
php
php
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
// Declare an ArrayIterator
$arrItr = new ArrayIterator(
array(
"a" => 4,
"b" => 2,
"g" => 8,
"d" => 6,
"e" => 1,
"f" => 9
)
);
// Display the offset value
var_dump($arrItr->offsetGet("a"));
// Check the existence of offset
var_dump($arrItr->offsetExists("a"));
// Unset the offset value
var_dump($arrItr->offsetUnset("a"));
// Check the existence of offset
var_dump($arrItr->offsetExists("a"));
?>
Output:
Program 2:
int(4) bool(true) NULL bool(false)
<?php
// Declare an ArrayIterator
$arrItr = new ArrayIterator(
array(
"for", "Geeks", "Science",
"Geeks", "Portal", "Computer"
)
);
// Print the value at index 1
echo $arrItr->offsetGet(1) . "\n";
// Check the existence of offset
var_dump($arrItr->offsetExists(1));
// Unset the offset value
var_dump($arrItr->offsetUnset(1));
// Check the existence of offset
var_dump($arrItr->offsetExists(1));
// Print the value at index 0
echo $arrItr->offsetGet(0) . "\n";
// Check the existence of offset
var_dump($arrItr->offsetExists(0));
// Unset the offset value
var_dump($arrItr->offsetUnset(0));
// Check the existence of offset
var_dump($arrItr->offsetExists(0));
?>
Output:
Reference: https://www.php.net/manual/en/arrayiterator.offsetexists.php
Geeks bool(true) NULL bool(false) for bool(true) NULL bool(false)
Article Tags :