2

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.

2
  • 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. Commented Jan 26, 2012 at 13:22
  • Does it work in the PHP engine though? Post your syntax error if you're indeed getting one Commented Jan 26, 2012 at 13:29

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.