What are the potential pitfalls of using eval() to evaluate PHP code stored in variables?

Using eval() to evaluate PHP code stored in variables can pose security risks as it allows for arbitrary code execution, making the application vulnerable to injection attacks. To mitigate this risk, it is recommended to avoid using eval() whenever possible and find alternative solutions such as using functions or language constructs to achieve the desired functionality.

// Avoid using eval() to evaluate PHP code stored in variables
$code = "echo 'Hello, World!';";
eval($code); // This is not recommended due to security risks

// Instead, consider using functions or language constructs
$code = "Hello, World!";
echo $code; // This is a safer alternative