1. Go to this page and download the library: Download avadim/math-executor 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/ */
avadim / math-executor example snippets
$calculator = new \avadim\MathExecutor\MathExecutor();
print $calculator->execute('1 + 2 * (2 - (4+10))^2 + sin(10)');
// cascade execution - variable $_ has result of previous calculation
print $calculator
->calc('4+10')
->calc('1 + 2 * (2 - $_)^2')
->calc('$_ + sin(10)')
->getResult();
use avadim\MathExecutor\Generic\AbstractToken;
use avadim\MathExecutor\Generic\AbstractTokenOperator;
use avadim\MathExecutor\Token\TokenScalarNumber;
class TokenOperatorModulus extends AbstractTokenOperator
{
protected static $pattern = 'mod';
/**
* Priority of this operator (1 equals "+" or "-", 2 equals "*" or "/", 3 equals "^")
* @return int
*/
public function getPriority()
{
return 3;
}
/**
* Association of this operator (self::LEFT_ASSOC or self::RIGHT_ASSOC)
* @return string
*/
public function getAssociation()
{
return self::LEFT_ASSOC;
}
/**
* Execution of this operator
* @param AbstractToken[] $stack Stack of tokens
*
* @return TokenScalarNumber
*/
public function execute(&$stack)
{
$op2 = array_pop($stack);
$op1 = array_pop($stack);
$result = $op1->getValue() % $op2->getValue();
return new TokenScalarNumber($result);
}
}
$calculator = new avadim\MathExecutor\MathExecutor();
$calculator->addOperator('mod', '\TokenOperatorModulus');
echo $calculator->execute('286 mod 100');