PHP SplObjectStorage valid() Function
Last Updated :
23 Jun, 2023
The SplObjectStorage::valid() function is an inbuilt function in PHP which is used to check the current storage entry is valid or not.
Syntax:
bool SplObjectStorage::valid()
Parameters: This function does not accept any parameter.
Return Value: This function returns true if the iterator entry is valid, false otherwise.
Below programs illustrate the SplObjectStorage::valid() function in PHP:
Program 1:
<?php
$str = new SplObjectStorage();
$obj = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$str ->attach( $obj , "GFG" );
$str -> rewind ();
print ( $str ->valid());
?>
|
Program 2:
<?php
$gfg = new SplObjectStorage();
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$gfg [ $obj1 ] = "GFG" ;
$gfg [ $obj2 ] = "GeeksClasses" ;
$gfg [ $obj3 ] = "SUDO" ;
$gfg -> rewind ();
while ( $gfg ->valid()) {
var_dump( $gfg ->getInfo());
$gfg ->next();
}
?>
|
Output:
string(3) "GFG"
string(12) "GeeksClasses"
string(4) "SUDO"
Reference: https://www.php.net/manual/en/splobjectstorage.valid.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...