herdoc:help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

herdoc:help

Post 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:
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

so sad that it could not be used for displaying html tags.
developed to work only for pure large alphanumerical text. :cry:
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post 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;
?>
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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:
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

raghavan20 wrote:does not work ... :cry:
It does without comments and whitespace. Take a look at working examble
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

does macromedia put a \r or \n when we hit enter and go to the next line???
i hope notepad does... :roll:
Last edited by raghavan20 on Sat Aug 20, 2005 10:50 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
Post Reply