Saturday, November 22, 2025
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

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6784 POSTS0 COMMENTS
Nicole Veronica
11931 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6848 POSTS0 COMMENTS