doz15449
2016-09-12 18:38
浏览 42
已采纳

在echo语句中显示html [重复]

This question already has an answer here:

Ok the below information works im just trying to figure my problem is that if that it displays like this title grade...

but i want to put a | inbetween like this

title | grade.

my below code works but if there is no grade it still echos the |,

how can i get it to only echo when there is a value in that field.

because when the field is empty it shows

title |

<div class="sto-info">
<span><?php echo $title; ?> <?php echo '|', $grade; ?></span>
</div>
</div>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • 如何编写PHP三元运算符 9 answers

    好以下信息的工作原理我只是想弄清楚我的问题是,如果它显示出来 像这样 title grade ...

    但是我想放一个| 像这样的中间

    标题| 等级。

    我的下面的代码可以工作,但是如果没有等级,它仍会回显|,

    我怎么才能得到它只在那里回声 是该字段中的值。

    因为当该字段为空时显示

    标题|

      &lt; div class =“sto-info”&gt; 
    &lt; span&gt;&lt;?php echo $ title;  ?&GT;  &lt;?php echo'|',$ grade;  ?&gt;&lt; / span&gt; 
    &lt; / div&gt; 
       
     
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doucang2831 2016-09-12 18:39
    已采纳

    You can use a ternary operator:

    <span><?php echo $title; ?> <?php echo $grade ? '|' . $grade : ''; ?></span>
    
    点赞 评论
  • dougu9895 2016-09-12 18:43

    Absolutely what Dave said is correct. You can use "IF Statement" too if you aren't familiar with this operator.

    if ($grade !== '') {
        echo $title.', | ,'.$grade;
    } else {
        echo "No Grade !";
    }
    
    点赞 评论
  • dougui1977 2016-09-12 18:49

    You can use the isset() or is_null() functions

    <span><?php echo $title, !is_null($grade) ?  '|' . $grade : '' ; ?></span>
    

    or:

    <span><?php echo $title, isset($grade) ?  '|' . $grade : '' ; ?></span>
    

    If grade exists but is empty then you need another test, for example:

    <span><?php echo $title, isset($grade) && strlen($grade) > 0 ?  '|' . $grade : '' ; ?></span>
    
    点赞 评论

相关推荐 更多相似问题