Syntax
function countTo(int $max): Generator {
for ($i = 1; $i <= $max; $i++) yield $i;
}Examples
PHP generators example
A focused example you can adapt while practising.
function countTo(int $max): Generator {
for ($i = 1; $i <= $max; $i++) yield $i;
}Best practices
- Start with the smallest working example, then adapt it to your project.
- Use the language’s standard library and idioms before introducing extra dependencies.
At a glance
- Purpose
- Server-side web applications
- File extension
- .php
- Runs in
- PHP runtime on a server or CLI
- Usually used with
- HTML, databases and web servers
Specifications & further reading
Related PHP documentation
PHP Functions & Type Declarations
PHP functions use the function keyword. Parameter and return type declarations can document and enforce the values a function accepts and returns.PHP function definitions
PHP reference for php function definitions.PHP union types
PHP reference for php union types.PHP default parameters
PHP reference for php default parameters.
PHP functions use the function keyword. Parameter and return type declarations can document and enforce the values a function accepts and returns.PHP function definitions
PHP reference for php function definitions.PHP union types
PHP reference for php union types.PHP default parameters
PHP reference for php default parameters.