Thursday, October 9, 2025
HomeLanguagesPHP SplObjectStorage offsetExists() Function

PHP SplObjectStorage offsetExists() Function

The SplObjectStorage::offsetExists() function is an inbuilt function in PHP which is used to check the object exist in storage or not.

Syntax: 

bool SplObjectStorage::offsetExists($object)

Parameters: This function accepts a single parameter $object which specifies the object to be checked.
Return Value: This function returns true if the object exists in storage otherwise return false.
Below programs illustrate the SplObjectStorage::offsetExists() function in PHP:

Program 1:  

php




<?php
  
// Create an empty SplObjectStorage
$str = new SplObjectStorage;
$obj = new StdClass;
  
// Attach $obj to $str 
$str->attach($obj);
  
// Print Result
var_dump($str->offsetExists($obj)); 
?>


Output: 

bool(true)

 

Program 2: 

php




<?php
  
// Create an Empty SplObjectStorage
$str = new SplObjectStorage();
   
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$obj4 = new StdClass;
  
// Attach only three objects
$str->attach($obj1, "GeksforGeeks");
$str->attach($obj2, "GFG");
$str->attach($obj3);
  
// Print result
var_dump($str->offsetExists($obj1)); 
var_dump($str->offsetExists($obj2)); 
var_dump($str->offsetExists($obj4)); 
var_dump($str->offsetExists($obj3)); 
  
?>


Output: 

bool(true)
bool(true)
bool(false)
bool(true)

 

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

RELATED ARTICLES

Most Popular

Dominic
32346 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11877 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11940 POSTS0 COMMENTS
Shaida Kate Naidoo
6835 POSTS0 COMMENTS
Ted Musemwa
7094 POSTS0 COMMENTS
Thapelo Manthata
6789 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS