如果用户登录我想显示这个,当我尝试使用 echo 编码时,我失败了,我该怎么做?
<?php
if(isset($_SESSION['userID'])){
echo 'BELOW CODE HERE';
}
?>
我尝试在回显"中添加代码。但是没有工作我该怎么做?
<div class="col">
<div class="card" >
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Cras justo odio</li>
<li class="list-group-item">Dapibus ac facilisis in</li>
<li class="list-group-item">Vestibulum at eros</li>
</ul>
<div class="card-body">
<a href="#" class="card-link">Card link</a>
<a href="#" class="card-link">Another link</a>
</div>
</div>
您收到错误,因为您将 HTML 包装在单引号中,并在 HTML 中使用了单引号(...卡的大部分内容...
使用以下HEREDOC
尝试以下操作:
if($_SESSION['userID']){
echo <<<EOF
<div class="col">
<div class="card" >
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Cras justo odio</li>
<li class="list-group-item">Dapibus ac facilisis in</li>
<li class="list-group-item">Vestibulum at eros</li>
</ul>
<div class="card-body">
<a href="#" class="card-link">Card link</a>
<a href="#" class="card-link">Another link</a>
</div>
</div>
EOF;
}
这是存储HTML或任何可能包含单引号或双引号的文本的更好方法。
试试这个您必须转义此行中存在的单引号 一些快速示例文本以构建卡片标题并构成card's
内容的大部分。
if(isset($_SESSION['userID'])){
echo '<div class="col">
<div class="card" >
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the `card's` content.</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Cras justo odio</li>
<li class="list-group-item">Dapibus ac facilisis in</li>
<li class="list-group-item">Vestibulum at eros</li>
</ul>
<div class="card-body">
<a href="#" class="card-link">Card link</a>
<a href="#" class="card-link">Another link</a>
</div>
</div> ';
}