13

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.

2
  • In the file that's included, are there any PHP operations, or is it just plain text? Commented Jun 27, 2012 at 15:54
  • Yes @lonesomeday ... Something I didn't mention in the Q is that my email_template.php file contains embedded php. So I figured I needed to include() it rather than file_get_contents() it. Is that true, would get_file-contents() still parse as php? Commented Jun 27, 2012 at 15:57

7 Answers 7

25

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
Sign up to request clarification or add additional context in comments.

4 Comments

What's the difference between ob_get_clean() and ob_end_flush() ? Which one is best to use in my case?
@Dr.DOT ob_get_clean returns the content. ob_end_flush stops buffering and sends the output to the browser and returns a boolean. ob_end_flush is not what you want here.
tested it and it worked perfectly. Thanks @lonesomeday -- I learned something cool today!
Or shorter: ob_start(); include('email_template.php'); $content = ob_get_clean();
14

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();

1 Comment

Thanks @TimCooper. Something I didn't mention in the Q is that my email_template.php file contains embedded php. So I figured I needed to include() it rather than file_get_contents() it. Is that true, would get_file-contents() still parse as php?
3

file_get_contents()

$file = file_get_contents('email_template.php');

Or, if you're insane:

ob_start();
include('email_template.php');
$file = ob_end_flush();

Comments

1

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

Comments

0

Try using PHP's file_get_contents() function.

See more here: http://php.net/manual/en/function.file-get-contents.php

Comments

0

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

1 Comment

Technically the trick is to define the variable name before you require_once the file, then you can unset the variable to prevent it overwriting other parts of your script if necessary or you can leave it like any other variable.
0

You have two optional choices

[Option 1]

  1. Create a file called 'email_template.php'
  2. inside the file add a variable like so

    $body = '<html>email content here</html>';

  3. in another file require_once 'email_template.php'

  4. then echo $body;

[Option 2]

$body = require_once 'email_template.php';

Comments

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.