PHP code example of laxity7 / phpmoney

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

    

laxity7 / phpmoney example snippets


use Laxity7\Money\Money;
use Laxity7\Money\Currency;

$tenEur = new Money(10.50, 'EUR');
//$tenEur = new Money(10.50, new Currency('EUR')); // the same as above
$twentyOneEur = $tenEur->add($tenEur);
echo $twentyOneEur->getAmount(); // 21
echo $twentyOneEur->getCurrency(); // EUR
echo $twentyOneEur; // 21 EUR
echo json_encode($twentyOneEur); // {"amount":"21","currency":"EUR"}

$btc = new Money(0.00000001, 'BTC');
$eth = new Money(1.01, 'USDT');
$sum = $btc->add($eth); // throws \Laxity7\Money\Exceptions\InvalidArgumentException

use Laxity7\Money\Money;
use Laxity7\Money\MoneyConfig;

// You can configure your own currencies only once. You can't change it later.
// Scale is the number of decimal places in the currency (e.g. 2 for USD, 8 for BTC). Max is 14.
MoneyConfig::configure(
    new Currencies([
        ['name' => 'US Dollar', 'symbol' => 'USD', 'scale' => 2],
        ['name' => 'Euro', 'symbol' => 'EUR', 'scale' => 4],
        ['name' => 'MyCoin', 'symbol' => 'MC', 'scale' => 8],
   ]
);


//...
$tenEur = new Money(10.50, 'USD'); // ok
$tenMyCoin = new Money(10.50, 'MyCoin'); // ok
$tenBtc = new Money(10.50, 'BTC'); // throws \Laxity7\Money\Exceptions\UnacceptableCurrencyException