eval problem - unexpected T_STRING in...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

eval problem - unexpected T_STRING in...

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$str = "<a href=\\\"http://\$domain_var/\\\">\$domain_var</a><br />\\n";
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

echoing it doesn't show anything? var_dump($str) to make sure.
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post 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
Post Reply