In what scenarios would it be advisable to use a formelparser instead of directly manipulating mathematical formulas in PHP strings?

When dealing with complex mathematical formulas in PHP strings, it can be error-prone and difficult to maintain. Using a formelparser library can help parse and evaluate mathematical expressions accurately and efficiently. This is especially useful when dealing with user-inputted formulas or when the formulas are dynamic and need to be evaluated at runtime.

// Example of using a formelparser library to evaluate a mathematical expression
require_once 'path/to/formelparser/vendor/autoload.php';

use Gregwar\Formelparser\Parser;

$parser = new Parser();
$formula = "2 * (3 + 5)";
$result = $parser->parse($formula);

echo $result; // Output: 16