0

Working with a phpmailer function, having to format the email inside a variable with double quotes is becoming a nightmare.

$body = "<div><h1>Hello, ".$foo."!</h1></div>";

It's like this for the entire body of the email, which is extensive, and gets hard to troubleshoot as my editor (KomodoEdit) doesn't recognize syntax errors and such which the markup is defined within a string.

I've been looking into other ways of accomplishing this such as the heredoc method, but while this looks cleaner, it still doesn't allow me the utility of my editing software's syntax check and tag collapsing.

My next step was to try something like this:

body.php

<div><h1>Hello, $foo !</h1></div>

index.php

error_reporting(E_ALL);
$foo = 'Frank';

$page = include('body.php');

$body = <<<EOD
        $page
        EOD;

But this doesn't post anything and I don't even get any errors reported. I've been through the manual but am either missing something in the syntax or it's a logical error where <<<EOD is to be used.

4 Answers 4

2

EOD; can't have any whitespace before. Move it left. However that still won't do what you want.

ob_start();
include('body.php');
$page = ob_get_clean();

I would never adjust my application to meet the syntax highlighting or any other capabilities of an editor!

Sign up to request clarification or add additional context in comments.

3 Comments

The heredoc is unneccesary (and won't work as intended here). $page will already contain the results of the template if it's modified to do <?php echo $foo ?>
I really just want to set the content of body.php to a string variable and then include it to the $body variable for use in other functions. But body.php has to include previously defined variables within from executed root doc. Also, setting <php? echo $foo; ?> seems tedious, but if that's the only way to do with without setting all the markup as a string, than so be it. Also not trying to bend to the will of the text editor, the code would just seem easier to manipulate (and expand for other email templates) if it were in it's own file.
Just remove the heredoc and use $page.
0

update your code

index.php

error_reporting(E_ALL);
$_REQUEST['foo'] = 'Frank';

ob_start();
include('body.php');
$page = ob_get_clean();

body.php

"<div><h1>Hello, ".$_REQUEST['foo']." !</h1></div>"

we can only access a public variable (request etc) in included file

4 Comments

Still using string concatenation on HTML markup which is what I'm trying to avoid.
But I do like the $_REQUEST part.
$body = <<<EOT $page EOT;
check example 2 from us2.php.net/manual/en/…
0

Try this:

1) index.php

<?php
error_reporting(E_ALL);
$foo = 'Frank';

$page = include('body.php');

echo <<<EOD
    $page
EOD;
?>

2) body.php

<?php
return <<<RESULT
    <div><h1>Hello, $foo !</h1></div>
RESULT;
?>

1 Comment

@tPlummer : please find above updated code of body.php without string concatenation. I hope this will help you.
-1

Try this:

1) index.php

<?php
error_reporting(E_ALL);
$foo = 'Frank';

$page = include('body.php');

echo <<<EOD
    $page
EOD;
?>

2) body.php

<?php
    return "<div><h1>Hello, ".$foo." !</h1></div>";
?>

1 Comment

Using string concatenation is exactly what the OP is trying to avoid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.