PHP | SplObjectStorage valid() Function
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 // Create an empty SplObjectStorage $str = new SplObjectStorage(); $obj = new StdClass; $obj2 = new StdClass; $obj3 = new StdClass; // Use attach() function to // add object $str->attach($obj, "GFG"); // Use rewind() function to rewind the // iterator to the first storage element $str->rewind(); // Use valid() function to check current // iterator is valid entry or not print($str->valid()); ?> |
1
Program 2:
<?php // Create an empty SplObjectStorage $gfg = new SplObjectStorage(); $obj1 = new StdClass; $obj2 = new StdClass; $obj3 = new StdClass; $gfg[$obj1] = "GFG"; $gfg[$obj2] = "GeeksClasses"; $gfg[$obj3] = "SUDO"; // Use rewind function $gfg->rewind(); while($gfg->valid()) { var_dump($gfg->getInfo()); // Moving to next element $gfg->next(); } ?> |
string(3) "GFG" string(12) "GeeksClasses" string(4) "SUDO"
Reference: https://www.php.net/manual/en/splobjectstorage.valid.php
Recommended Posts:
- PHP | SplObjectStorage key() Function
- PHP | SplObjectStorage contains() Function
- PHP | SplObjectStorage next() Function
- PHP | SplObjectStorage getinfo() Function
- PHP | SplObjectStorage setInfo() Function
- PHP | SplObjectStorage removeAll() Function
- PHP | SplObjectStorage detach() Function
- PHP | SplObjectStorage attach() Function
- PHP | SplObjectStorage current() Function
- PHP | SplObjectStorage addAll() Function
- PHP | SplObjectStorage offsetSet() Function
- PHP | SplObjectStorage removeAllExcept() Function
- PHP | SplObjectStorage rewind() Function
- PHP | SplObjectStorage serialize() Function
- PHP | SplObjectStorage unserialize() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



