inactive Posted August 25, 2008 Share Posted August 25, 2008 Hey guys, I'm running eval() on a var containing some php code (should be OK, right?). Well it works, but as soon as I add some heredoc code (i.e. <<<EOF), it has a fit. Here is the code from the parent script: <?php $handle = @fopen("foo.inc", "r"); if ($handle) { $buffer = ''; while (!feof($handle)) { $buffer .= fgets($handle, 4096); } fclose($handle); eval(buffer); } else { echo 'shit! something went wrong'; } ?> And foo.inc is: $var1 = 'test text'; $var2 = 'some more test text'; $arr1 = array('alpha','beta','charlie'); $var_heredoc_1 = <<<ENDBODY foo bar foobar ENDBODY; And for my trouble I get: Parse error: syntax error, unexpected $end in index.php(11) : eval()'d code on line 10 If I remove the heredoc part, it works as expected (note that the code above has been simplified a fair bit). Any ideas? I'm sure the syntax in foo.inc is correct (as I renamed it foo.php, and put <?php ?> around, and it worked correctly. Is there limitations on what can and cannot be aval()'ed? Any help would be much appreciated. Thanks guys. Quote Link to comment Share on other sites More sharing options...
inactive Posted August 25, 2008 Author Share Posted August 25, 2008 *bump* Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 25, 2008 Share Posted August 25, 2008 First of all, you left the $ off of $buffer...: eval(buffer); Quote Link to comment Share on other sites More sharing options...
inactive Posted August 25, 2008 Author Share Posted August 25, 2008 Sorry, that got in there when I was simplifying my code for the post. The actual code is correct. Any other ideas? Quote Link to comment Share on other sites More sharing options...
inactive Posted August 25, 2008 Author Share Posted August 25, 2008 *bump* Quote Link to comment Share on other sites More sharing options...
tibberous Posted August 25, 2008 Share Posted August 25, 2008 Code looks right, might be a limitation of PHP. Is there a reason you are eval'ing the code rather than including it? Quote Link to comment Share on other sites More sharing options...
inactive Posted August 25, 2008 Author Share Posted August 25, 2008 Maybe you're right. Hmmm yeah I suppose I could include it. Thanks heaps. Quote Link to comment Share on other sites More sharing options...
inactive Posted October 29, 2014 Author Share Posted October 29, 2014 HEREDOC syntax is ended by the delimiter defined at the start, followed by a semicolon, followed by a newline. There was no newline, therefore it is not being recognised as the end of the HEREDOC. Adding an extra \n did the trick. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.