The first appearance of PHP iterable was in PHP version ( 7.1 ) – It is the word that can be defined in the for loop or for each. it accepts arrays or objects and it can be used as a PHP data type – Or in another definition, The iterable is everything that can be used as a loop. The followings are examples of PHP iterable.

The iterable word can be passed to the PHP function as a parameter ( PHP data type ).

The followings are examples of the PHP iterable, But before you read that you have to know what the PHP function is.

What is PHP Function

In general, the PHP function is a block that contains a lot of codes and it can be used many times in the track, it consists of two parts, the defined word in the PHP called function and beside it the name then the braces function func( $param ) then the function body or block between the curly braces { .. }. Let’s take an example.

<?php 
   function func( $param ) {
      // Your code here
   }
?>

In the following iterator function, we will path an array with its data as a parameter in the function parameter. That can be done with the iterable predefined ( data-type ) word.

<?php 
   function exact_loop( iterable $data ) {
	 foreach( $data as $number ) {
		 echo $number;
	 }
   }
   
   exact_loop([1,2,3,4,5,6,7,8,9]);
?>

Example Of PHP Iterable with Object

Or you can pass an object with the iterable pseudo-type word to the PHP function. But make sure you use the 5 functions in the implementation from the interface such as:

  • The key function returns the key of the current element regarding the list.
  • The next function is to move the current indicator to the next element.
  • The current function refers to the current element.
  • The rewind function moves to the first element through the indicator.
  • The valid function to detect if the current indicator is already moved or not.

In the following example, we will init a class object and pass it to an iterable function as a parameter. But the class should be implemented from the iterable interface

<?php 
   class animals implements Iterator {
       
       private $indicator = 0;
       private $elements = array();

       // Store the array values into the elements
       public function __construct( $item ) {
         $this->elements = $item;
       }
     
       // the current element assigned to the index
       public function current() {
         return $this->elements[$this->indicator];
       }
      
       // Increase the indicator by one
       public function next() {
         $this->indicator++;
       }
      
       // Go back to the first index in the array
       public function rewind() {
         $this->indicator = 0;
       }
      
       // to return the current indicator key 
       public function key() {
         return $this->indicator;
       }
     
       // Detect the current value is more than the count
       public function valid() {
         $length = count( $this->elements );
         if( $this->indicator < $length ) {
	      return true;
         }
         return false;
       } 
   }

   function exact_loop( iterable $data ) {
     foreach( $data as $number ) {
        echo $number;
     }
   }
   
   $obj = new animals(array(15,52,33,65,41));
   exact_loop($obj);
?>

Anyway, you understood how to use the iterable with examples and how to use it with the objects. To see more examples check the PHP data Types tutorial.