Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generators cannot return values using “return”

Description

In PHP 5.x, generators were not allowed to have return values. This feature was added in PHP 7.0, and, after running the full course, generator deliver their return value when calling the getReturn method.

Example

<?php

function foo() {
    yield 1;
    
    return 2;
}

?>

Alternatives

  • Upgrade to PHP 7.0 or more recent.
  • Use a global variable to export data from the method.