变量中的PHP标记和引号引起语法/解析错误

这些行导致语法/解析错误,我不知道如何解决它。 我很确定它是<?php?>标记或单引号。 有任何想法吗?

$data = '<?php
    $title = "'. $name. '";
    $keywords = "'. $title. ','. $developer. '";
    $description = "How to install '. $title. ' for PC and Mac.";

    include('templates/modheader.php');
    include('templates/modfooter.php');
    ?>';

提前致谢。


include('templates/modheader.php');
include('templates/modfooter.php');

是罪魁祸首:你混合单引号。 勉强使用

include("templates/modheader.php");
include("templates/modfooter.php");

include('templates/modheader.php');
include('templates/modfooter.php');

应该

include("templates/modheader.php");
include("templates/modfooter.php");

对于大块字符串数据,我强烈建议使用HEREDOC / NOWDOC语法。 你可以把它和sprintf结合起来注入值

$template = <<<'_PHP'
<?php
$title = '%s';
$keywords = $title . ',%s';
$description = "How to install $title for PC and Mac.";

include 'templates/modheader.php';
include 'templates/modfooter.php';
?>
_PHP;

$data = sprintf($template, $name, $developer);
链接地址: http://www.djcxy.com/p/69455.html

上一篇: PHP tags and quotes in variables causing syntax/parse errors

下一篇: Parse error: syntax error, unexpected ')'