Re: problem with eval()
| From: | Rasmus Lerdorf | Date: | Fri, 14 Jul 2000 04:12:32 +0000 |
| Subject: | Re: problem with eval() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-6595@lists.php.net to get a copy of this message | ||
> I have an 'application' which parses a given file,
> which contains HTML & PHP3. For the HTML lines the
> application writes the line to Dest_File.
> For the PHP3 lines it's suppossed to interpret these
> using the eval() statement, and depending on the PHP3
> different text would be written out to the Dest_File.
> except that doesn't work.
>
> here's my code
>
> (the contents of $complete_code has been read from
> SRC_FILE)
>
> $complete_code='if ($value == $today) echo "Hello
> World";';
> $temp_string = addslashes($complete_code);
> eval("\new_string=\"$complete_code\";");
>
> The value of $new_string is then:
>
> if (1 == 1) echo "Hello World";
>
> instead of "Hello World" or ""
>
> any help on this would be very much appreciated.
The behaviour you see makes perfect sense to me. Expand what you are
feeding eval() yourself:
(I assume missing the $ in front of new_string was just a typo)
$new_string = "if ($value == $today) echo \"Hello World\";"
You probably want something along the lines of:
$complete_code = '($value == $today) ? "Hello World" : ""';
and then
eval("\$new_string = \"$complete_code\";);
-Rasmus