
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Generators.
Introduction
Traversing a big collection of data using looping construct such as foreach would require large memory and considerable processing time. With generators it is possible to iterate over a set of data without these overheads. A generator function is similar to a normal function. However, instead of return statement in a function, generator uses yield keyword to be executed repeatedly so that it provides values to be iterated.
The yield keyword is the heart of generator mechanism. Even though its use appears to be similar to return, it doesn't stop execution of function. It provides next value for iteration and pauses execution of function.
yielding values
A for loop yields each value of looping variable is used inside a generator function
Example
<?php function squaregenerator(){ for ($i=1; $i<=5; $i++){ yield $i*$i; } } $gen=squaregenerator(); foreach ($gen as $val){ echo $val . " "; } ?>
As foreach statement tries to display $val for first time, the squaregenerator yields first element, retains $i and pauses execution till foreach takes next iteration. The output is similar to a regular foreach loop
Output
1 4 9 16 25
PHP's range() function returns a list of integers from $start to $stop with interval of $step between each numbers. Following program implements range() as generator
Example
<?php function rangegenerator($start, $stop, $step){ for ($i=$start; $i<=$stop; $i+=$step){ yield $i; } } foreach (rangegenerator(2,10,2) as $val){ echo $val . " "; } ?>
Output
Output is similar to range(2,20,2)
2 4 6 8 10
An associative array can also be implemented as generator
Example
<?php function arrgenerator($arr){ foreach ($arr as $key=>$val){ yield $key=>$val; } } $arr=array("one"=>1, "two"=>2, "three"=>3, "four"=>4); $gen=arrgenerator($arr); foreach ($gen as $key=>$val) echo $key . "=>" . $val . "
"; ?>
Output
one=>1 two=>2 three=>3 four=>4
- Related Articles
- PHP Generators vs Iterator objects
- Generators in Python?
- Types DC Generators
- Types of Generators
- Applications of DC Generators
- What are free Online Favicon Generators?
- Types of YouTube channel name generators
- Different online Hashtag generators for YouTube
- Construction and Working Principle of DC Generators
- What are generators and decorators in Python?
- Explain Different kinds of Generators in JavaScript
- Characteristics of DC Generators – Series, Shunt and Compound
- Types of AC Generators – Single Phase and Three Phase AC Generator
- Types of DC Generator – Separately Excited and Self-Excited DC Generators
- Name the petroleum product which is commonly used for electric generators.
