PHP code example of jdwx / result

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

    

jdwx / result example snippets


function ExampleFunction() : Result {
    return match( random_int( 0, 1 ) ) {
        0 => Result::err( 'This is an error message.' ),
        1 => Result::ok( 'This is an example message.', 42 ),
    };
}

$result = ExampleFunction();
if( $result->isOK() ) {
    echo "Success ({$result}) with value: ", $result->unwrap(), "\n";
} else {
    echo "Error: {$result}\n";

    # This will throw an exception
    $result->unwrap();
}