Are there best practices for including PHP code from a database in a script?

When including PHP code from a database in a script, it's important to ensure that the code is sanitized to prevent any security vulnerabilities such as SQL injection attacks. One way to achieve this is by storing the PHP code in the database as a string and then using PHP's eval() function to execute it within the script. However, caution should be exercised when using eval() as it can pose security risks if not handled properly.

// Retrieve PHP code from the database
$phpCode = "echo 'Hello, World!';";

// Sanitize the PHP code
$phpCode = htmlspecialchars($phpCode);

// Execute the PHP code using eval()
eval($phpCode);