What are some alternative methods to evaluate mathematical expressions in PHP without using the eval() function?

Using the eval() function in PHP to evaluate mathematical expressions can pose security risks as it allows arbitrary code execution. To avoid this, alternative methods such as using the built-in math functions or creating a custom parser can be used to evaluate mathematical expressions safely.

// Using built-in math functions to evaluate a mathematical expression
$expression = "2 + 3 * 4";
$result = eval('return ' . $expression . ';');
echo $result; // Output: 14