【问题标题】:Pass value of a include to a variable将包含的值传递给变量
【发布时间】:2015-06-15 02:44:38
【问题描述】:

是否可以将包含的结果传递给变量?

让我们考虑这个简单的文件“example.html”:

<p>some HTML</p>

在一个 PHP 文件中,我们如何将这个文件的内容传递给一个变量? 我试过了:

$my_variable = include('example.html');

包含文件,但$my_variable的值为1

【问题讨论】:

  • 使用ob_XXX函数捕获输出缓冲区。
  • 你必须使用缓冲区。看php.net/manual/en/book.outcontrol.php
  • $my_variable 的值为 1,因为include 如果成功则返回true,如果有错误则返回false

标签: php variables include


【解决方案1】:

使用

ob_start();
include('example.html');
$output = ob_get_clean();// this will buffer your output

【讨论】:

    【解决方案2】:

    如果你只是想获取文件的内容,你可以使用这个:

    $data = file_get_contents("example.html");
    echo htmlentities($data);
    

    或者这个:

    ob_start();
    readfile("example.html");
    $data = ob_get_clean();
    echo htmlentities($data);
    

    【讨论】:

      猜你喜欢
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      相关资源
      最近更新 更多