PHP code example of clouding / range

1. Go to this page and download the library: Download clouding/range 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/ */

    

clouding / range example snippets


use Clouding\Range\Range;

$range = new Range(1, 10);

$range = Range::parse('1..10');

echo $range->start; // 1

echo $range->end;   // 10

$range = new Range(1, 10);

var_dump($range->contains(5)); // bool(true)

$range = new Range(1, 10);
$rangeFoo = new Range(1, 10);

var_dump($range->equals($rangeFoo)); // bool(true)

$range = new Range(1, 10);
$rangeBar = new Range(5, 10);

var_dump($range->intersect($rangeBar)); // bool(true)

$range = Range::parse('-5..5');

echo $range;            // -5..5
echo $range->toString() // -5..5

$range = new Range(1, 5);

var_dump($range->toArray());  // [1, 2, 3, 4, 5]

// with gap 2
var_dump($range->toArray(2)); // [1, 3, 5]

echo $range->format(':start ~ :end');       // 1 ~ 10

echo $range->format('From :start to :end'); // From 1 to 10

$range = new Range(1, 5);

$range->each(function ($int) {
    echo $int, ', ';
});
// 1, 2, 3, 4, 5, 

$range->each(function ($int) {
    if ($int >= 3) {
        return false;
    }
    echo $int . ', ';
});
// 1, 2, 

// with gap 2
$range->each(function($int) {
    echo $int . ', ';
}, 2);
// 1, 3, 5,

$range = new Range(1, 10);

echo $range->random();       // 3 

var_dump($range->random(3)); // [3, 2, 9]