PHP code example of bertptrs / phpstreams

1. Go to this page and download the library: Download bertptrs/phpstreams library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bertptrs / phpstreams example snippets


// Define a generator for Fibonacci numbers
function fibonacci()
{
    yield 0;
    yield 1;

    $prev = 0;
    $cur = 1;

    while (true) {
        yield ($new = $cur + $prev);
        $prev = $cur;
        $cur = $new;
    }
};

// Define a predicate that checks for odd numbers
$isOdd = function($num) {
    return $num % 2 == 1;
};

// Create our stream.
$stream = new phpstreams\Stream(fibonacci());

// Finally, use these to create our result.
$oddFibo = $stream->filter($isOdd)  // Keep only the odd numbers
    ->limit(8)                      // Limit our results
    ->toArray(false);               // Convert to array, discarding keys