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

时间:2013-12-18 23:50:02

标签: php parsing variables syntax syntax-error

这些行导致语法/解析错误,我不知道如何修复它。我很确定它是<?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');
    ?>';

提前致谢。

3 个答案:

答案 0 :(得分:3)

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

是罪魁祸首:你混合单引号。完全使用

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

答案 1 :(得分:0)

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

应该是

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

答案 2 :(得分:0)

对于大块字符串数据,我强烈推荐使用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);