1. Go to this page and download the library: Download nxp/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/ */
nxp / math-executor example snippets
use NXP\MathExecutor;
$executor = new MathExecutor();
echo $executor->execute('1 + 2 * (2 - (4+10))^2 + sin(10)');
$executor->setVarValidationHandler(function (string $name, $variable) {
// allow all scalars, array and null
if (is_scalar($variable) || is_array($variable) || $variable === null) {
return;
}
// Allow variables of type DateTime, but not others
if (! $variable instanceof \DateTime) {
throw new MathExecutorException("Invalid variable type");
}
});
$calculator = new MathExecutor();
$calculator->setVarNotFoundHandler(
function ($varName) {
if ($varName == 'trans') {
return transmogrify();
}
return null;
}
);