Big_J Posted June 22, 2007 Share Posted June 22, 2007 Hello. I have been looking for an answer for about a week now to no avail. I cannot find any kind of informative documentation on this. I'm trying to figure out how the <<<END works This is how I think it works, from the little explanation I found: It allows you to ouput multiple lines of text, or html code, and is used like this: echo <<<END text goes here more text goes here maybe some html tags <b>here</b> a <form></form> END; But even if I put a single word, it does not work, I keep getting "Parse error: syntax error, unexpected $end in /var/www/page.php on line xx" Where xx is the last line number, even blank. So what is missing? Here's the complete code sample: <html> <body> <?php $test = 1; if ($test == 1){ echo <<<END Monkey has brains END; } else echo "monkey has no brain!"; ?> </body> </html> Do I need to include something? Can someone please explain to me how that works, or if there is some other way to add html code inside the php script? Quote Link to comment https://forums.phpfreaks.com/topic/56741-solved-echo/ Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 why do you need the <<<END if the first place? Quote Link to comment https://forums.phpfreaks.com/topic/56741-solved-echo/#findComment-280225 Share on other sites More sharing options...
snowman15 Posted June 22, 2007 Share Posted June 22, 2007 hes just playing around with it to get the idea, he wont actually use the monkey brains code for any practical use. Quote Link to comment https://forums.phpfreaks.com/topic/56741-solved-echo/#findComment-280229 Share on other sites More sharing options...
emehrkay Posted June 22, 2007 Share Posted June 22, 2007 the problem with your example is that when using <<<VAR VAR; the everything must be flush against the beginning of the line echo <<<Mark see how this is flush against the side? there are no spaces or tabs before the text Mark; Quote Link to comment https://forums.phpfreaks.com/topic/56741-solved-echo/#findComment-280231 Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 still I see no point in doing that unless you are getting php to echo in a line based output in a cmd or something. Otherwise just use quotes and echo it out as a string of formatted text using html tags Quote Link to comment https://forums.phpfreaks.com/topic/56741-solved-echo/#findComment-280233 Share on other sites More sharing options...
audiokiwi Posted June 22, 2007 Share Posted June 22, 2007 From http://www.php.net/manual/en/language.types.string.php It is very important to note that the line with the closing identifier contains no other characters, except possibly a semicolon (. That means especially that the identifier may not be indented, and there may not be any spaces or tabs after or before the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by your operating system. This is \r on Macintosh for example. Closing delimiter (possibly followed by a semicolon) must be followed by a newline too. If this rule is broken and the closing identifier is not "clean" then it's not considered to be a closing identifier and PHP will continue looking for one. If in this case a proper closing identifier is not found then a parse error will result with the line number being at the end of the script. Quote Link to comment https://forums.phpfreaks.com/topic/56741-solved-echo/#findComment-280234 Share on other sites More sharing options...
Big_J Posted June 22, 2007 Author Share Posted June 22, 2007 still I see no point in doing that unless you are getting php to echo in a line based output in a cmd or something. Otherwise just use quotes and echo it out as a string of formatted text using html tags Well, because I'm using html tags that have attributes, and will be using quote in those tags, and a regular echo doesn't work because the quotes in those tags terminate it. I'm new to php, as you probably could tell, so I don't know if there is a different way to do this or not. Basically, I'm building a script that displays different questions depending on the answer of the previous question, so since not all question will be displayed at once, I need to have them inside the php script to display them when needed via if else statements. I'm experienced with C. the problem with your example is that when using <<<VAR VAR; the everything must be flush against the beginning of the line echo <<<Mark see how this is flush against the side? there are no spaces or tabs before the text Mark; Thanks, that solved it. I wish manuals would use clearer language. This is from the manual for echo: echo <<<END This uses the "here document" syntax to output multiple lines with $variable interpolation. Note that the here document terminator must appear on a line with just a semicolon. no extra whitespace! END; What I got from that is that END; must have no white space before it. "terminator must appear on a line with just a semicolon..." Quote Link to comment https://forums.phpfreaks.com/topic/56741-solved-echo/#findComment-280263 Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 you can "escape quotes <?php echo "<a href=\"me.com\">me.com</a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/56741-solved-echo/#findComment-280265 Share on other sites More sharing options...
Big_J Posted June 22, 2007 Author Share Posted June 22, 2007 Oh yes, the old escape sequence. Thanks. I'll stick with <<<END though, I think it looks better. Quote Link to comment https://forums.phpfreaks.com/topic/56741-solved-echo/#findComment-280335 Share on other sites More sharing options...
tcollie Posted June 22, 2007 Share Posted June 22, 2007 The problem is this. When using heredoc strings you can't have a line indention before your closing tag. So this example does work. <?php $test = 1; if($test == 1){ echo <<<END Monkey has brains END; }else{ echo "monkey has no brain!"; } ?> If you'll notice the closing tag END; has no space before it. It you add spaces, or any other information in front of it, it won't work. Hope that helps you out some. Quote Link to comment https://forums.phpfreaks.com/topic/56741-solved-echo/#findComment-280516 Share on other sites More sharing options...
redarrow Posted June 22, 2007 Share Posted June 22, 2007 variable example for u ok. <?php $test = 1; $a=<<<END Monkey has brains END; $b=<<<END monkey has no brain! END; if($test == 1){ echo $a; }else{ echo $b; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56741-solved-echo/#findComment-280517 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.