我想在HTML变量中放置一个HTML代码。所以,我有一个“sidebar.html”文件,它有各种各样的“{$ test1}”“{$ test2}”等等。
我无法弄清楚如何将HTML代码添加到该代码中。
所以简而言之,我希望在第一个HTML文件中有{$ fleet},在我的PHP代码中,我需要像
这样的东西$fleet = <div class="sidebar-content">
<div class="sidebar-header">Find a Fleet</div>
<div class="sidebar-link">
<a href="./index.php?a=profile&u=ObsidianFleet">Obsidian Fleet</a>
<a href="./index.php?a=profile&u=startrekfreedom">Star Trek Freedom</a>
</div>
<div class="sidebar-link">
</div>
</div>
}
提前致谢
答案 0 :(得分:4)
您要分配字符串值吗?
尝试heredocs:
$fleet = <<<EOF <div class="sidebar-content">
<div class="sidebar-header">Find a Fleet</div>
<div class="sidebar-link">
<a href="./index.php?a=profile&u=ObsidianFleet">Obsidian Fleet</a>
<a href="./index.php?a=profile&u=startrekfreedom">Star Trek Freedom</a>
</div>
<div class="sidebar-link">
</div>
</div>
EOF;
答案 1 :(得分:4)
您应该使用nowdoc:
$fleet = <<<'PHP_STR'
<div class="sidebar-content">
<div class="sidebar-header">Find a Fleet</div>
<div class="sidebar-link">
<a href="./index.php?a=profile&u=ObsidianFleet">Obsidian Fleet</a>
<a href="./index.php?a=profile&u=startrekfreedom">Star Trek Freedom</a>
</div>
<div class="sidebar-link">
</div>
</div>
PHP_STR;
答案 2 :(得分:1)
你需要告诉PHP它是一个字符串。使用单引号'
字符包装HTML:
$fleet = '<div class="sidebar-content">
<div class="sidebar-header">Find a Fleet</div>
<div class="sidebar-link">
<a href="./index.php?a=profile&u=ObsidianFleet">Obsidian Fleet</a>
<a href="./index.php?a=profile&u=startrekfreedom">Star Trek Freedom</a>
</div>
<div class="sidebar-link">
</div>
</div>';
答案 3 :(得分:1)
作为字符串:
<?php
$fleet = '<div class="sidebar-content">
<div class="sidebar-header">Find a Fleet</div>
<div class="sidebar-link">
<a href="./index.php?a=profile&u=ObsidianFleet">Obsidian Fleet</a>
<a href="./index.php?a=profile&u=startrekfreedom">Star Trek Freedom</a>
</div>
<div class="sidebar-link">
</div>
</div>';
?>
答案 4 :(得分:1)
$fleet = "<div class='sidebar-content'>
<div class='sidebar-header'>Find a Fleet</div>
<div class='sidebar-link'>
<a href='./index.php?a=profile&u=ObsidianFleet'>Obsidian Fleet</a>
<a href='./index.php?a=profile&u=startrekfreedom'>Star Trek Freedom</a>
</div>
<div class='sidebar-link'>
</div>
</div>
}";