【问题标题】:PHP tags and quotes in variables causing syntax/parse errors变量中的 PHP 标记和引号导致语法/解析错误
【发布时间】:2013-12-18 23:50:01
【问题描述】:

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

提前致谢。

【问题讨论】:

  • 你想做什么?
  • 这更有可能是因为您没有转义包含的文件名周围的'
  • 考虑一个HEREDOC 字符串。
  • @arielcr 是创建文件的变量。是的,格式有点奇怪。
  • 这看起来不像是一个理智的人应该实际投入生产的任何东西。停止。重新思考。重写。

标签: php parsing variables syntax syntax-error


【解决方案1】:
include('templates/modheader.php');
include('templates/modfooter.php');

是罪魁祸首:你混合了单引号。只需使用

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

【讨论】:

    【解决方案2】:
    include('templates/modheader.php');
    include('templates/modfooter.php');
    

    应该是

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-03
        • 1970-01-01
        • 2023-01-20
        • 2011-03-05
        • 1970-01-01
        相关资源
        最近更新 更多