【问题标题】:How can I write PHP syntax in a string如何在字符串中编写 PHP 语法
【发布时间】:2014-09-10 04:08:37
【问题描述】:

我尝试编写一些内容以写入 php 文件,并在我的 $content 变量中使用 Heredoc 和 nowdoc,但 php 标签似乎不起作用并且仍然显示。

$content = <<< EOT
<?php
    include $_SERVER['DOCUMENT_ROOT'] . '/home/index2.php';
?> 
EOT;

有什么想法吗?

【问题讨论】:

    标签: php heredoc nowdoc


    【解决方案1】:

    尝试使用Output buffering 而不是heredoc:

    ob_start();
    include $_SERVER['DOCUMENT_ROOT'] . '/home/index2.php';
    $content = ob_get_clean();
    

    参考:

    【讨论】:

    • 最好的解决方案,我知道\使用它,只需向上和评论 $)
    【解决方案2】:

    你可以这样做:

    $content = "<" . <<< EOT
    ?php
        include $_SERVER['DOCUMENT_ROOT'] . '/home/index2.php';
    > 
    EOT;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-23
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多