1

i am trying to use a generator function inside a codeigniter controller.

class Test extends CI_Controller {

   public function __construct()
   {
    parent::__construct();
        // Your own constructor code
   }

   public function testc()
   {

    $generator = $this->_gen_one_to_three();
    foreach ($generator as $value) {
        echo "$value\n";
    }

   }

   function _gen_one_to_three() 
   {
      for ($i = 1; $i <= 3; $i++) 
      {
        yield $i;
      }
   }


}

But its gives me a

Parse error: syntax error, unexpected '$i' (T_VARIABLE) in /var/www/redsnail/application/controllers/test.php on line 30.

is there any difference while using the yield keyword inside a class ?

Gopakumar Gopalan
  • 1,187
  • 14
  • 22
Fazil Uruniyengal
  • 286
  • 1
  • 3
  • 15

0 Answers0