PHP code example of omnicron / infix-calculator

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

    

omnicron / infix-calculator example snippets




use \Omnicron\InfixCalculator\Calculator;

// create a Calculator object
$calculator = new Calculator;

// calculate the result of an expression
$result = $calculator->solve('3 + 5 * (2^2 + 1)');

// write the result (28)
echo $result.PHP_EOL;



use \Omnicron\InfixCalculator\Calculator;

// create a Calculator object
$calculator = new Calculator;

// get steps to solve an expression
$steps = $calculator->getSteps('3 + 5 * (2^3 + 1) / (2^2 - 1)');

// show the steps
echo implode(PHP_EOL, $steps).PHP_EOL;

/*
the steps will be shown like this:
  (3 + ((5 * ((2 ^ 3) + 1)) / ((2 ^ 2) - 1)))
  (3 + ((5 * (8 + 1)) / (4 - 1)))
  (3 + ((5 * 9) / 3))
  (3 + (45 / 3))
  (3 + 15)
  18
*/