PHP code example of albertoarena / calculator

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

    

albertoarena / calculator example snippets


use Calculator\Calculator;

$calculator = new Calculator();

// Basic operators
$calculator->number(1)
    ->operator('+')->number(5)
    ->operator('*')->number(3.5)
    ->operator('/')->number(2)
    ->execute();

echo $calculator;
// ==> 1 + 5 * 3.5 / 2 = 9.75

// Power
$calculator->number(2)
    ->operator('*')->number(2)
    ->operator('^')->number(4)
    ->execute();

echo $calculator;
// ==> 2 * 2 ^ 4 = 32

// Square root
$result = $calculator->number(9)
    ->operator('√')
    ->execute();

echo $calculator;
// ==> √ 9 = 3

// Trigonometry
$result = $calculator->number(1)
    ->operator('cos')
    ->execute();

echo $calculator;
// ==> cos (1) = 0.54030230586814