How to assign the contents of a file to a variable in PHP

前端 未结 7 701
Happy的楠姐
Happy的楠姐 2020-12-30 07:22

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 = in

7条回答
  •  庸人自扰
    2020-12-30 07:39

    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
    

提交回复
热议问题