Reputation: 8106
I have a document file containing HTML markup. I want to assign the contents of the entire file to a PHP variable.
I have this line of code:
$body = include('email_template.php');
When I do a var_dump()
I get string(1) "'"
Is it possible to assign the contents of a file to a variable?
[Note: the reason for doing this is that I want to separate the body segment of a mail message from the mailer script -- sort of like a template so the user just modifies the HTML markup and does not need to be concerned with my mailer script. So I am including the file as the entire body segment on mail($to, $subject, $body, $headers, $return_path);
Thanks.
Upvotes: 13
Views: 22247
Reputation: 2916
You have two optional choices
[Option 1]
'email_template.php'
inside the file add a variable like so
$body = '<html>email content here</html>';
in another file require_once 'email_template.php'
echo $body;
[Option 2]
$body = require_once 'email_template.php';
Upvotes: 0
Reputation: 2916
Yes you can its easy.
In the file you want to use the variable place this
require_once ‘/myfile.php';
if(isset($responseBody)) {
echo $responseBody;
unset($responseBody);
}
In the file you are calling /myfile.php place this
$responseBody = 'Hello world, I am a genius';
Thanks Daniel
Upvotes: 0
Reputation:
You should be using file_get_contents()
:
$body1 = file_get_contents('email_template.php');
include
is including and executing email_template.php
in your current file, and storing the return value of include()
to $body1
.
If you need to execute PHP code inside the of file, you can make use of output control:
ob_start();
include 'email_template.php';
$body1 = ob_get_clean();
Upvotes: 14
Reputation: 237845
If there is PHP code that needs to be executed, you do indeed need to use include
. However, include
will not return the output from the file; it will be emitted to the browser. You need to use a PHP feature called output buffering: this captures all the output sent by a script. You can then access and use this data:
ob_start(); // start capturing output
include('email_template.php'); // execute the file
$content = ob_get_contents(); // get the contents from the buffer
ob_end_clean(); // stop buffering and discard contents
Upvotes: 25
Reputation: 4029
As others have posted, use file_get_contents
if that file doesn't need to be executed in any way.
Alternatively you can make your include return the output with the return statement.
If your include does processing and outputs with echo [ed: or leaving PHP parsing mode] statements you can also buffer the output.
ob_start();
include('email_template.php');
$body1 = ob_get_clean();
TimCooper beat me to it. :P
Upvotes: 1
Reputation: 219804
$file = file_get_contents('email_template.php');
Or, if you're insane:
ob_start();
include('email_template.php');
$file = ob_end_flush();
Upvotes: 3
Reputation: 17451
Try using PHP's file_get_contents()
function.
See more here: http://php.net/manual/en/function.file-get-contents.php
Upvotes: 0