Saturday, May 23, 2026
HomeLanguagesPHP | ArrayIterator offsetExists() Function

PHP | ArrayIterator offsetExists() Function

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
  
// 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:

int(4)
bool(true)
NULL
bool(false)

Program 2:




<?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:

Geeks
bool(true)
NULL
bool(false)
for
bool(true)
NULL
bool(false)

Reference: https://www.php.net/manual/en/arrayiterator.offsetexists.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS