Jump to content

[SOLVED] echo <<<END


Big_J

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.