What alternatives to using eval() can be considered for evaluating a string in PHP?

Using eval() in PHP can be risky as it allows for the execution of arbitrary code, which can pose security vulnerabilities. Instead of using eval(), a safer alternative is to use built-in functions like intval(), floatval(), or casting variables to specific types. Another option is to use PHP's own language constructs like include or require to execute code from external files.

// Using intval() to evaluate a string as an integer
$string = "42";
$integerValue = intval($string);
echo $integerValue;

// Using include to execute code from an external file
include 'external_code.php';