PHP code example of endoumame / php-monad

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

    

endoumame / php-monad example snippets


use EndouMame\PhpMonad\Option;

$name = Option\fromValue($user['name'] ?? null)
    ->map(fn($n) => strtoupper($n))
    ->filter(fn($n) => strlen($n) > 0)
    ->unwrapOr('Anonymous');

use EndouMame\PhpMonad\Result;

$result = Result\fromThrowable(
    fn() => json_decode($json, flags: JSON_THROW_ON_ERROR),
    fn($e) => "Parse error: {$e->getMessage()}"
);

$data = $result->map(fn($d) => $d['key'])->unwrapOr(null);
bash
composer