Othman
Othman

Reputation: 3018

How to know if the code needs eval()

I have serval blocks in my CMS. some blocks contains PHP code and some HTML code. if I use eval to show the code while the code is an html code I get this error:

Parse error: syntax error, unexpected '<' in /Applications/XAMPP/xamppfiles/htdocs/mob/main_content.php(17) : eval()'d code on line 1

I need to add print or echo before the code.

can I build a function that can now if the code needs eval() or echo()

Upvotes: 0

Views: 118

Answers (2)

Barmar
Barmar

Reputation: 781721

There's no automatic way to know if something needs eval(). Just because it looks like PHP code doesn't mean it's intended to be eval'ed.

If you need to go forward with this approach, I suggest two possibilities:

  1. Use different columns in the database for storing literal text and dynamic PHP; when the data comes from the PHP column you will use eval().

  2. Require the admin to mark up dynamic PHP in his input, e.g. [php]...[/php]. Remove the markup and then call eval().

Upvotes: 2

Jeff Hubbard
Jeff Hubbard

Reputation: 9902

To answer your actual question, you should use require() instead of trying to figure out if you should eval() or echo(). However, you should never use eval(). There are almost zero cases where eval() is the only way to do something. You should use a template engine that allows you to insert PHP code blocks into place and use that.

I personally prefer PHPTAL, but there are plenty of others like Smarty, Twig, and many others (those were the top few I found with a quick google search).

Upvotes: 2

Related Questions