Page 1 of 1

herdoc:help

Posted: Sat Aug 20, 2005 4:42 am
by raghavan20

Code: Select all

echo <<<EOD
	<div style='padding:5px; text-align:center; background-color:#000099;color:#FFFFFFF'>
	</div>
	EOD;
Is there anything wrong with the above syntax??? I always get this wrong. why??? :cry:

Posted: Sat Aug 20, 2005 5:13 am
by m3mn0n
it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
http://php.net/manual/en/language.types ... ax.heredoc

Posted: Sat Aug 20, 2005 5:28 am
by raghavan20
so sad that it could not be used for displaying html tags.
developed to work only for pure large alphanumerical text. :cry:

Posted: Sat Aug 20, 2005 6:03 am
by wwwapu
Sami wrote:
it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
http://php.net/manual/en/language.types ... ax.heredoc
This means the starter and closing identifier of heredoc block not the contents of it.

You must not have any space after the identifiers or php gives you variety of errors.

All below are valid ways of doing it. Except for the comments that show you possible errorplaces

Code: Select all

<?php
$str = <<<EOD /* one space after */
<div style="margin-bottom: 12px; padding:5px; text-align:center; background-color:#990000;color:#FFFFFF">
The first
</div> 
EOD; /*one space after */

print $str;

print <<<EOD
<div style="margin-bottom: 12px; padding:5px; text-align:center; background-color:#009900;color:#FFFFFF">
The second
</div> 
 EOD;/*one space before*/

echo <<<EOD
    <div style='margin-bottom: 12px; padding:5px; text-align:center; background-color:#000099;color:#FFFFFF'>
    and finally the third
    </div> 
EOD;
?>

Posted: Sat Aug 20, 2005 6:30 am
by raghavan20

Code: Select all

$str = <<<EOD /* one space after */ 
	<div style="margin-bottom: 12px; padding:5px; text-align:center; background-color:#990000;color:#FFFFFF"> 
	The first 
	</div> 
	EOD; /*one space after */
	echo $str;
still its the same code i took from you but does not work in dreamweaver or when interpreted :cry:

Posted: Sat Aug 20, 2005 6:44 am
by m3mn0n
wwwapu wrote:This means the starter and closing identifier of heredoc block not the contents of it.
Of course... as the manual page says. ;)

I should have made that more clear though. heh

Rather sleepy still... damn long day of programming. :x

Posted: Sat Aug 20, 2005 8:28 am
by wwwapu
raghavan20 wrote:does not work ... :cry:
It does without comments and whitespace. Take a look at working examble

Posted: Sat Aug 20, 2005 8:56 am
by feyd
[url=http://us2.php.net/language.types.string]String Syntax for Heredoc[/url] warning wrote: 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.

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.

It is not allowed to use heredoc syntax in initializing class members. Use other string syntaxes instead.

Posted: Sat Aug 20, 2005 10:22 am
by raghavan20
does macromedia put a \r or \n when we hit enter and go to the next line???
i hope notepad does... :roll:

Posted: Sat Aug 20, 2005 10:47 am
by feyd
Windows uses \r\n as a new line, however, you need to make sure it's right for the system where the server is. From what I remember, FTPing in ASCII mode will attempt to fix OS dependant breaks to the server..