[thelist] String Concatenation ??

R.Livsey R.Livsey at cache-22.co.uk
Sat Dec 7 09:53:01 CST 2002


> I am not sure what you are doing wrong but I have this and it
> send html perfectly fine:
>
> /* Lets send a HTML formatted message. */
> 	$message1 .= "<HTML>\n";
> 	$message1 .= "<HEAD>\n";
> 	$message1 .= "<TITLE>Enquiry $reference_number</TITLE>\n";
> 	$message1 .= "<META HTTP-EQUIV=\"Content-Type\"
...
<SNIP>
...
> 	$message1 .= "enquiry at blahblah.co.uk<BR></P>\n";
> 	$message1 .= "</BODY>\n";
> 	$message1 .= "</HTML>\n";
>
> Andrew

<tip type="PHP and the Heredoc">

Get rid of lines and lines ugly concatenation by using Heredocs.
Heredocs behave just like double quoted strings, without the double
quotes so you do not need to escape double quotes!

The heredoc synatx is to use <<< followed by a delimiter (I generally
use EOD). The same identifier is then used to close the quotation.

For example.

$str = "<html>\n";
$str.= "<head>\n";
$str.= "<title>$title</title>\n";
$str.= "</head>\n";
$str.= "<body bgcolor=\"$bgColor\">\n";
$str.= $bodyContent."\n";
$str.= "</body>\n";
$str.= "</html>\n";

Can be replaced with:

$str=<<<EOD

<html>
<head>
<title>$title</title>
</head>
<body bgcolor="$bgColor">
$bodyContent
</body>
</html>

EOD;

Which looks nicer and easier to maintain to you?

</tip>

--
R.Livsey
Incutio Web Developer
[ PHP | Perl | Java | C# ]
w : www.cache-22.co.uk
  / www.incutio.com




More information about the thelist mailing list