Jump to content

Echo


slpctrl

Recommended Posts

I was browsing through some PHP, and stumbled upon a code that used echo strangely. I was wondering if anyone could clear up how this works:

 

<?php

//..code

echo <<<HERE

<b><h3><center>Chislam's Chat Box</center></h3></b>

<iframe src="chat.txt" width="700" height="250"></iframe>

<form method="post" name="form">Nickname: <input type="text' name="nickname" value="$nickname"><br>

Message:   <input type="text" name="message"><input type="submit" value="Submit">

</form>

<a href="chat.php">Refresh</a>

HERE;

 

if ($_POST['nickname'] && $_POST['message']){

    $fp = fopen("chat.txt", "a");

    $stuff = $nickname . ": " . $message . "\n";

    fputs($fp, $stuff);

} else {

    echo "Please enter nickname and message.";

}

 

?>

 

The echo <<<HERE I found peculiar. How does this work?

Link to comment
https://forums.phpfreaks.com/topic/98540-echo/
Share on other sites

It's known as HEREDOC and it is generally used for long strings with both single and double quotes and variables.

 

It means you don't need to escape anything so no \' \" or anything.

 

I use it for long amounts of text with particular formats, like emails etc...

 

Hope that helps?

 

Sam

Link to comment
https://forums.phpfreaks.com/topic/98540-echo/#findComment-504324
Share on other sites

It's known as HEREDOC and it is generally used for long strings with both single and double quotes and variables.

 

It means you don't need to escape anything so no \' \" or anything.

 

I use it for long amounts of text with particular formats, like emails etc...

 

Hope that helps?

 

Sam

 

Makes perfect sense to me. This could come in handy  :D Can it be used for other things like storing variables with quotes? How would I go about storing a variable with quotes?

Link to comment
https://forums.phpfreaks.com/topic/98540-echo/#findComment-504329
Share on other sites

If I've understood what you meant, you could use it like

 

$myvar = "variables";

$var = <<<MYVARIABLE
This is my variable containing 'single' quotes and "double" quotes.
It also parses other $myvar

I can have any characters in this variable, then to end it like this!

MYVARIABLE;

echo $var;

 

It can be used like that.

 

Sam

Link to comment
https://forums.phpfreaks.com/topic/98540-echo/#findComment-504342
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.