How do you define a PHP include as a string?

前端 未结 6 1400
我在风中等你
我在风中等你 2021-01-01 12:48

I tried:

$test = include \'test.php\';

But that just included the file normally

6条回答
  •  不知归路
    2021-01-01 13:21

    The other answers, for reasons unknown to me, don't quite reach the correct solution.

    I suggest using the buffer, but you have to get the contents and then clean the buffer before the end of the page, otherwise it is outputted. Should you wish to use the output from the included file, you should use op_get_contents(), which will return a string of the contents of the buffer.

    You also don't need to loop over the includes as each will just add to the buffer (unless you clean it first).

    You therefore could use the following;

    ob_start();
    include_once('test.php');
    include_once('test2.php');
    $contents = ob_get_contents();
    ob_end_clean();
    

    Hope this helps.

提交回复
热议问题