2

I have some html spanning over EOF:

$message = <<<EOF

<p style="font-size: 9px; font-family: Verdana, Helvetica; width: 100%; text-align:left;">Clcik to remove <a href="http://www.mysite.com/remove.php?email=' $email '">clicking here.</a></p>

EOF;

I've tried single quotes, single quotes with . escaping the double quotes. Can't seem to find the right combination. Any help appreciated.

TIA

Sammitch
  • 30,782
  • 7
  • 50
  • 77
James MV
  • 8,569
  • 17
  • 65
  • 96

3 Answers3

2
<?php

$email="test@example.com";

$message = <<<EOF
<p style="font-size: 9px; font-family: Verdana, Helvetica; width: 100%; text-align:left;">Click to remove <a href="http://www.mysite.com/remove.php?email=$email">clicking here.</a></p>
EOF;

echo $message;

?>

However, from your example, I don't see the purpose of HEREDOC. Why not just:

<p style="font-size: 9px; font-family: Verdana, Helvetica; width: 100%; text-align:left;">Click to remove <a href="http://www.mysite.com/remove.php?email=<?=$email?>">clicking here.</a></p>
showdev
  • 28,454
  • 37
  • 55
  • 73
  • 1
    Some people (myself included) hate dropping in and out of PHP. – Evert Jan 16 '13 at 18:26
  • Why's that? That's part of the beauty of PHP. – showdev Jan 17 '13 at 18:26
  • Beauty is definitely in the eye of the beholder ;). All my templates are done using Twig, and all my PHP code follows PSR-0, and PSR-1. – Evert Jan 17 '13 at 19:21
  • I also make absolutely sure my PHP code doesn't contain any HTML ever, so I would personally also not be a fan of this particular heredoc block :P – Evert Jan 17 '13 at 19:21
1

Your code should work, but with Heredocs [which is what this syntax is actually called] you don't usually need to escape anything, or use specific quotes. @showdev's first example hits this.

However, a cleaner, more reusable syntax is found with sprintf().

$email1 = "bill@example.com";
$email2 = "ted@example.com";

$message_frame = '<p>Click to remove <a href="http://www.mysite.com/remove.php?email=%s">clicking here.</a></p>';

$message .= sprintf($message_frame, $email1);
$message .= sprintf($message_frame, $email2);

/* Output:
<p>Click to remove <a href="http://www.mysite.com/remove.php?email=bill@example.com">clicking here.</a></p>
<p>Click to remove <a href="http://www.mysite.com/remove.php?email=ted@example.com">clicking here.</a></p>
*/

Lastly: large, inline style="" declarations really defeat the purpose of CSS.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
0

Heredoc is typically used for longer strings, or maybe even multiple thoughts, that you may want segmented on to separate lines.

As tuxradar put it: "In order to allow people to easily write large amounts of text from within PHP, but without the need to constantly escape things, heredoc syntax was developed"

<?php
$mystring = <<<EOT
    This is some PHP text.
    It is completely free
    I can use "double quotes"
    and 'single quotes',
    plus $variables too, which will
    be properly converted to their values,
    you can even type EOT, as long as it
    is not alone on a line, like this:
EOT;
?> 

In your case, it'd make more sense to simply echo out your string.

$message = '<p style="font-size: 9px; font-family: Verdana, Helvetica; width: 100%; text-align:left;">Clcik to remove <a href="http://www.mysite.com/remove.php?email=' $email '">clicking here.</a></p>';

echo $message;
user3871
  • 12,432
  • 33
  • 128
  • 268