What are alternative methods to safely evaluate mathematical formulas in PHP without using eval()?

Using eval() in PHP to evaluate mathematical formulas can be risky as it can execute any PHP code passed to it, potentially leading to security vulnerabilities. A safer alternative is to use the evalMath library in PHP, which allows you to evaluate mathematical expressions without executing arbitrary code.

// Using the evalMath library to safely evaluate mathematical formulas

// Include the evalMath library
require_once 'evalmath.class.php';

// Create a new instance of the evalMath class
$evalMath = new EvalMath();

// Evaluate a mathematical expression
$result = $evalMath->evaluate('2 + 2 * 3');

// Output the result
echo $result;