Friday, December 12, 2025
HomeLanguagesPHP SplObjectStorage contains() Function

PHP SplObjectStorage contains() Function

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

Syntax:

bool SplObjectStorage::contains( $value )

Parameters: This function accepts a single parameter $value which specifies the storage object which is going to check.

Return Value: This function returns true if storage object contains specified object otherwise return false.

Below programs illustrate the SplObjectStorage::contains() function in PHP:

Program 1:




<?php
$gfg1 = new StdClass;
$gfg2 = new StdClass;
  
// Declare Empty SplObjectStorage
$str = new SplObjectStorage();
  
$str[$gfg1] = "neveropen";
  
// Print result
var_dump($str->contains($gfg1));
var_dump($str->contains($gfg2));
  
?>


Output:

bool(true)
bool(false)

Program 2:




<?php
$gfg1 = new StdClass;
$gfg2 = new StdClass;
  
// Declare Empty SplObjectStorage
$str = new SplObjectStorage();
  
$str[$gfg1] = "neveropen";
  
// Print result
var_dump($str->contains($gfg1));
var_dump($str->contains($gfg2));
  
// detach and print result
$str->detach($gfg1);
var_dump($str->contains($gfg1));
  
?>


Output:

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

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

RELATED ARTICLES

Most Popular

Dominic
32445 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6813 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12028 POSTS0 COMMENTS
Shaida Kate Naidoo
6947 POSTS0 COMMENTS
Ted Musemwa
7198 POSTS0 COMMENTS
Thapelo Manthata
6893 POSTS0 COMMENTS
Umr Jansen
6881 POSTS0 COMMENTS