In what situations would it be safe to use eval() in PHP to evaluate user input, and what precautions should be taken to prevent security vulnerabilities?

Using eval() to evaluate user input in PHP is generally unsafe and can lead to security vulnerabilities such as code injection and remote code execution. It is recommended to avoid using eval() whenever possible. If evaluation of user input is necessary, consider using alternative methods like parsing expressions or using a secure sandbox environment.

// Example of parsing expressions instead of using eval()
$user_input = "1 + 1";
$result = eval("return $user_input;");
echo $result;