PHP code example of better / nanoid

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

    

better / nanoid example snippets

 php
use Better\Nanoid\Client;
use Better\Nanoid\GeneratorInterface;

$client = new Client();

# default random generator
echo $client->produce($size = 32);

# more secure generator with more entropy
echo $client->produce($size = 32, true);
 php
$client = new Client('0123456789abcdefg');
$client->produce();
 php
# PS: anonymous class is new feature when PHP_VERSION >= 7.0

$client = new Client();

echo $client->produceUsing(new class implements GeneratorInterface {
    /**
     * @inheritDoc
     */
    public function random(int $size): string
    {
        $ret = [];
        
        while ($size--) {
            $ret[] = mt_rand(0, 255);
        }

        return $ret;
    }
});