【问题标题】:How to fix this html to be able to assign to php variable?如何修复此 html 以便能够分配给 php 变量?
【发布时间】:2014-07-27 17:03:01
【问题描述】:

所以我有这 30 行长的 HTML 代码。我不想将所有这些“更改为\” 我创建了一个名为 page 的 php 类并保存在 .inc 文件中。

我必须将这些 HTML 代码分配给 $home->content。无论如何我可以将我的html代码复制并粘贴到它吗?

这是页面的显示方式:

public function Display()
{
    echo "<html>\n<head>\n";
    $this -> DisplayTitle();
    $this -> DisplayKeywords();
    echo "</head>\n<body>\n";
    echo "<div class = \"container\">\n";
    $this -> DisplayHeader();
    echo $this -> content;
    $this -> DisplayFooter();
    echo "</div>\n</body>\n</html>\n";
}

提前谢谢你

【问题讨论】:

  • 我试过 $home->content = ?> "html code"

标签: php html class


【解决方案1】:

如果您的 HTML 代码中包含所有 ",那么您可以在 echo 语句的开头和结尾使用单引号 ',您无需更改任何内容。

public function Display()
{
    echo '<html>\n<head>\n';
    $this -> DisplayTitle();
    $this -> DisplayKeywords();
    echo '</head>\n<body>\n';
    echo '<div class = "container">\n';
    $this -> DisplayHeader();
    echo $this -> content;
    $this -> DisplayFooter();
    echo '</div>\n</body>\n</html>\n';
}

【讨论】:

    【解决方案2】:

    虽然 Usman 的回答似乎解决了您的问题,但了解 PHP 中不同引号样式之间的一些差异可能会很有用。 This answer explains them in detail。单引号和双引号之间的区别之一是双引号将评估字符串,并且您放置在其中的任何变量都会将它们的值放置在您的字符串中。

    例如:

    $myNum = 2;
    echo 'I have $myNum apples.'; //will output: I have $myNum apples.
    echo "I have $myNum apples."; //will output: I have 2 apples.
    

    这显然是对差异的非常基本的解释。

    【讨论】:

    • 太棒了!非常感谢rar。因为有了这个基本的解释,现在我可以做更多的事情了!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多