include inside a variable - 包含而不打印变量

include inside a variable - includes without print the variable

这很奇怪...我想在变量中包含一个文件以便稍后打印它但是它已经包含文件而不打印变量...

$var = include('file.php');
// stuff
echo $var;

file.php:

echo $stuff;

输出:

Notice: Undefined variable: stuff in file.php

我评论了 echo $var 以确保它包含在内...

有什么方法可以只加载变量中的内容而不是包含文件吗?

尝试file_get_contents

<?php
  $var  = file_get_contents('file.php');
 echo $var;
 ?>

你可以像以前那样做

$var = include 'file.php';

不过,包含的文件需要 return 一个值。 最后 file.php:

// Do some stuff (but don't echo anything)
return 'This will be put in the var-variable to be echoed or used when you see fit';