Forum Moderators: coopster

Message Too Old, No Replies

Trouble with Eval & variable declarations

         

Sagaris

4:25 pm on Apr 20, 2008 (gmt 0)

10+ Year Member



Hi guys,

I am trying to find an appropiate method by which I can store php code in a database, and then parse the code when it is retrieved from the database using PHP.

Eval seems to be the way to go (open to suggestions on this), but I am just trying to get a simple example working. What is stumping me at the moment is being able to include the declaration of PHP variables in the string stored in the database. Escaping the $ in front of the variable declarations was suggested in some eval tutorials but to no avail.

Here is my test code


$php = "<? \$1 = \"abc\"; ?>this is the text with php code in it. <? echo \$1; ?>";
ob_start();
eval(" ?>" . $php . "<? ");
$output = ob_get_contents();
ob_end_clean();
echo $output;

and this is the error I am receiving

Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /blah/web09/b1130/xyz.blah/htdocs/eval-test.php(31) : eval()'d code on line 1

If I change the value of the $php variable to


$php = "This is the text with php code in it. <? echo date('H:M:S'); ?>";

The PHP parses fine.

If anybody can shed some light on this I would be really grateful. Alternatively if you can think of a more suitable method for getting PHP out of a database and then parsed please shout up!

IanKelley

9:38 pm on Apr 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



One thing I always do when possible with evals is use single instead of double quotes. This means the data is not parsed and you therefore don't have to worry about escaping:

$php = '<? $1 = "abc"; ?>this is the text with php code in it. <? echo $1; ?>';

Possibly the problem you're having is a result of your eval line:

eval(" ?>" . $php . "<? ");

This is ultimately creating something like this:

?><? $1 = "abc"; ?>this is the text with php code in it. <? echo $1; ?><?

Try just eval($php) instead.

Sagaris

7:19 pm on Apr 21, 2008 (gmt 0)

10+ Year Member



Thanks for the suggestion, it hasn't quite solved my problem yet as I am now getting the error

Parse error: syntax error, unexpected '<' in /blah/web09/b1130/xyz.blah/htdocs/eval-test.php(32) : eval()'d code on line 1

I can't remove the opening <? as it won't parse so that leaves me a bit stumped. Any further ideas very welcome!

IanKelley

7:46 pm on Apr 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you post the modified code you're using?

Sagaris

9:57 pm on Apr 21, 2008 (gmt 0)

10+ Year Member



Sure, my code is


$php = '<? $1 = "abc"; ?>this is the text with php code in it. <? echo $1; ?>';
ob_start();
eval($php);
$output = ob_get_contents();
ob_end_clean();
echo $output;

IanKelley

10:49 pm on Apr 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ahh... I should noticed this earlier...

First, $1 is reserved, second, when you run the eval you're already in PHP mode so there's no need for the opener.

This should work:

$php = '$test = "abc"; ?>this is the text with php code in it. <? echo $test;'; 
ob_start();
eval($php);
$output = ob_get_contents();
ob_end_clean();
echo $output;
exit();

Sagaris

12:01 pm on Apr 22, 2008 (gmt 0)

10+ Year Member



It works! I knew it would be something really obvious with regards to the opening or closing of a tag (straightens up dunces cap).

I didn't know that $1 was a reserved variable so off to read up on that.

Thanks for your help IanK - very much appreciated!

[edited by: Sagaris at 12:02 pm (utc) on April 22, 2008]

IanKelley

10:14 pm on Apr 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yeah it's always something obvious isn't it? :-)

Reserved is the wrong choice of word, ${number} is reserved in perl. In PHP it's just plain illegal.

coopster

1:57 pm on Apr 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



... except when capturing subpatterns in regular expressions [php.net]. Other than that, variable names [php.net] follow the same rules as other labels in PHP.