如何在会话语句中添加HTML代码

时间:2019-06-06 11:46:45

标签: php css bootstrap-4

如果用户登录后要显示此信息,当我尝试使用echo进行编码时,我失败了怎么办?

<?php

if(isset($_SESSION['userID'])){
echo 'BELOW CODE HERE';

}
?>

我尝试在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> 

2 个答案:

答案 0 :(得分:1)

出现错误是因为您将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或任何实际上可能包含单引号或双引号的文本的更好方法。

答案 1 :(得分:0)

尝试一下 您必须转义此行中出现的单引号。一些快速的示例文本可基于卡片标题,并构成 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> ';

}