开发者

Assigning contents to a variable with include/require_once

开发者 https://www.devze.com 2023-04-13 04:00 出处:网络
Is it possible to do like $var = require_once(\'lol.php\'); so that any HTML output that lol.php does will go inside $var?

Is it possible to do like

$var = require_once('lol.php');

so that any HTML output that lol.php does will go inside $var?

I know about output buffering, but is there some special built-in function that already开发者_StackOverflow does this?


$var = require_once('lol.php'); will only put the return value of the file into $var. If you don't return anything from it, it'll just be null.

If you want the output you will need to use output buffering:

ob_start();
require_once('lol.php');
$var = ob_get_clean();


The assignment from an =include() call will only get you a possible return value from that script, not any output.

To make this possible you would have to modify the include script to capture the output:

 <?php
      ob_start();

      ...

      return ob_get_clean();
 ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号