4

Possible Duplicate:
heredoc with eval code execution

So I have the following in function.php:

eval("\$content = <<<TEMPLATE\n
               asdf
\nTEMPLATE;");

And I keep getting an error saying:

Parse error: syntax error, unexpected $end, expecting T_VARIABLE or T_END_HEREDOC or T_DOLLAR_OPEN_CURLY_BRACES or T_CURLY_OPEN in /var/www/function.php(10) : eval()'d code on line 5

I cannot figure out what the problem is. There is obviously an ending for the heredoc syntax, does heredoc just not like to play nice with eval?

Community
  • 1
  • 1
csteifel
  • 2,854
  • 6
  • 35
  • 61
  • 6
    Why would you do such a thing? – Jason McCreary Oct 22 '12 at 18:43
  • 2
    What are you trying to accomplish? This looks like all kinds of bad. See: http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – NullUserException Oct 22 '12 at 18:45
  • Even with TEMPLATE; on its own line it doesn't work. – csteifel Oct 22 '12 at 18:46
  • @NullUserException I thought that too, but wouldn't the \n be interpreted as a newline *before* the string is evalled at all? – GolezTrol Oct 22 '12 at 18:46
  • @GolezTrol Hmmm, it makes sense. I didn't catch the quotes. – NullUserException Oct 22 '12 at 18:47
  • 1
    I agree with @NullUserException. Please explain what you are trying to do so we can properly help you / prevent you from doing "stupid" things. `eval()` is considered `evil`. – PeeHaa Oct 22 '12 at 18:47
  • I wouldn't do this either, but it makes you wonder why it isn't possible. I think it would be more challenging to answer the question than to save this user from doing stupid things. We have made our points. :0 – GolezTrol Oct 22 '12 at 18:48
  • Very interesting theoretical question. But I see no actual uses in practice. What are you trying to do? I'm 99% sure there's a better solution. – Madara's Ghost Oct 22 '12 at 18:49
  • HTML templates where you can use php variables, I know MyBB uses it as do many other packages that use templating. – csteifel Oct 22 '12 at 18:50
  • 1
    @legion Don't do that. Just because someone else is doing it doesn't mean they're doing it right. In general, I would avoid using templating engines in PHP, period - because PHP itself *is* a templating engine. There *is* a better solution, it's called [MVC](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller). Look at how an MVC framework handles views (templates): http://codeigniter.com/user_guide/general/views.html. You get (somewhat) clean HTML, without losing performance or flexibility. – NullUserException Oct 22 '12 at 18:51
  • I'm not too worried about it I'm just doing it for a class so that my partner who doesn't know PHP can just create the HTML/CSS files elsewhere and I don't have to worry about integrating the PHP in those files. I would normally just use php directly in the template itself but its easier for him this way – csteifel Oct 22 '12 at 18:59

1 Answers1

6

HEREDOC syntax is ended by the delimiter defined at the start, followed by a semicolon, followed by a newline. You do not have the newline, therefore it is not being recognised as the end of the HEREDOC. Add an extra \n after TEMPLATE; and it should work fine.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592