PHP tags and quotes in variables causing syntax/parse errors

These lines are causing a syntax/parse error, and I'm not sure how to fix it. I'm pretty sure it's either the <?php and ?> tags, or the single quotes. Any ideas?

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

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

Thanks in advance.


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

is the culprit: You mix single quotes. SImply use

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

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

应该

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

For large blocks of string data, I highly recommend the HEREDOC / NOWDOC syntax. You can combine that with sprintf to inject values

$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/69456.html

上一篇: 解析错误:语法错误,意外;

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