PHP code example of fintara / calculator-php

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

    

fintara / calculator-php example snippets


use \Fintara\Tools\Calculator\Calculator;

$calculator = Calculator::create(); // use default tokenizer
echo $calculator->calculate('1+2*3/4'); // 2.5

$calculator->addFunction('cbrt', function($x) {
    return pow($x, 1/3);
});

echo $calculator->calculate('cbrt(27)'); // 3

$tokenizer = new Tokenizer();
$tokens = $tokenizer->tokenize('1+2*3.5'); // [1, '+', 2, '*', 3.5]

$tokens = $tokenizer->tokenize('1+sqrt(4)', ['sqrt']); // [1, '+', 'sqrt', '(', 4, ')']

$tokens = $tokenizer->tokenize('2 (1 + 3)'); // [2, '*', '(', 1, '+', 3, ')']