I've used heredoc syntax in server-side php to clean up the code -- my understanding of the utility of heredoc is "allows you to send client-side code to the browser and avoid individual echo's for each browser-side code statement"

My understanding of heredoc is that it just makes it easier to encapsulate a chunk of code you need to send to the browser without having to write an explicit 'echo' on every line, basically "Ok web server -- between the '<<<' and the end of the heredoc, all this should be sent to the browser."

So I'm not sure why I'm getting syntax errors for the following code:

 <?php
 echo <<<_SENDITALL 
 <script type="text/javascript">
 function foo(theArg1, theArg2, theArg3)
 {
 }
 </script>
 _SENDITALL;
 ?>

I've got heredocs all over the place in my project and they work fine so I'm not understanding why my Netbeans IDE keeps flagging the above with syntax errors.

For example, I use heredoc to send forms to the browser. And I've got javascript doing input validation on my html forms, with 'onsubmit' and 'onblur' etc.

So surely it can't be that php is of the mind "See here, it's fine if you heredoc html script to the browser, but your attempt to heredoc some client-side javascript will not stand' -- that wouldn't make sense both html and javascript are client-side, why would the heredoc syntax "care" whether html script or javascript was being sent to the browser.

share|improve this question
NetBeans may be confusing the JavaScript function() definition for a PHP function definition inside a HEREDOC, which would be syntactically incorrect. What you have should work fine. – Michael Berkowski Jan 26 '12 at 13:22
Does it work in the PHP engine though? Post your syntax error if you're indeed getting one – Evert Jan 26 '12 at 13:29

1 Answer

up vote 4 down vote accepted

There is a space after echo <<<_SENDITALL , remove it.

There also is a space before _SENDITALL; .

I'm not sure if it exists inside the original code(may come from the formatting here), remove it too.

share|improve this answer
+1 I've been up coding for 2 hours already, it's 5:30am here in Silicon Valley, a facepalm is a helluva way to start my coding day. Thanks. The space before _SENDITALL; was formatting and not in the original code, but there was a trailing space after the opening _SENDITALL. – wantTheBest Jan 26 '12 at 13:35

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.