Page 1 of 1

eval problem - unexpected T_STRING in...

Posted: Tue Mar 07, 2006 11:16 am
by tomfra
I have this string:


$str = "<a href=\"http://$domain_var/\">$domain_var</a><br />\n";


And I need to eval it so I use:


eval("\$str = \"$str\";");


Which results in this error:


Parse error: syntax error, unexpected T_STRING in C:\somepath\somefile.php : eval()'d code on line 1


Any ideas what is causing the problem? The culprit seem to be the unescaped quotes in $str but they have to be there.

Thanks!

Tomas

Posted: Tue Mar 07, 2006 11:18 am
by feyd

Code: Select all

$str = "<a href=\\\"http://\$domain_var/\\\">\$domain_var</a><br />\\n";

Posted: Tue Mar 07, 2006 11:38 am
by tomfra
That was super quick feyd!

But it doesn't seem to work. I no longer get the error but there is no output at all now.

Tomas

Posted: Tue Mar 07, 2006 11:49 am
by feyd
echoing it doesn't show anything? var_dump($str) to make sure.

Posted: Tue Mar 07, 2006 12:15 pm
by tomfra
You are right feyd, it does work. My mistake, sorry for that.

Unfortunately, it does not solve my problem completely. I found a difference in eval's behaviour under PHP 4.4.2 & PHP 5+. I just have to make work some code written by someone else which is not easy because there are limits of what I can do with it not to break compatibility.

There is a file called, for example, phpcode.txt. In this file there is some PHP code including this line:

Code: Select all

echo "<a href=\"http://$domain_var/\">$domain_var</a><br />\n";
The echo is within a while loop but that should not matter.

Now, in index.php there is this code:

Code: Select all

eval(file_get_contents("phpcode.txt"));
I am not sure why they didn't name phpcode.txt phpcode.php and didn't simply include is but the point is, on my PC which has PHP 5 installed it works perfectly. On another server with PHP 4.4.2 it does not work at all. In the PHP manual there is no mention of any change in the eval function in PHP 5 so I don't get it. It could just as well be a bug in 4.4.2.

Strange.

Tomas