View Full Version : echoing php code

02-17-2008, 04:04 PM
Let's say I have the following:
$content="<?php echo 'Hello world\n'; ?>"

and I wanted the php code to run what's inside that variable. How would I be able to do that?

The reason for this is, I'm working on a CMS and if the content in the database contains php, I want it to be ran.

-Thanks in advanced

02-17-2008, 04:17 PM
The closest thing I could find was using eval():

Scroll down to the entry dated: 31-Jan-2006

That looks similar to what you're doing.

02-17-2008, 04:40 PM
Thanks that works. It won't work of course if I have html code in it (<?php code ?> some html), but I can easily manipulate the string to only eval the php code.

02-17-2008, 05:20 PM
The eval() allows closing and opening ?> <?php tags. You can put inline html if you want.

If I remember correctly it assumes you are initially in php "mode", so you must put an initial closing ?> tag if the string starts with inline html.