dton37910
dton37910
2015-04-16 17:14
浏览 40
已采纳

PHP单数和复数文本编辑

I have a sentence like this:

<h5>There are <?php echo $total_results_count; ?> results waiting for you!</h5>

How could I modify this to say:

There is 1 result waiting for you!

if there is only 1 result.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongnai5905
    dongnai5905 2015-04-16 17:28
    已采纳
    <h5><?php
    if ($total_results_count > 1) {
      echo 'There are ' . $total_results_count . ' results';
    }
    else {
      echo 'There is ' . $total_results_count . ' result';
    } ?> waiting for you!</h5>
    
    点赞 评论
  • douxianji6181
    douxianji6181 2015-04-16 17:22

    Get results into a variable. Then,

    if($count == 1){
      str_replace("are results","is 1 result ","There are results waiting for you!");
    }
    
    点赞 评论
  • dousi4900
    dousi4900 2015-04-16 17:23

    As easy as:<h5>There <?php if($total_results_count>1) echo "are"; else echo "is" $total_results_count. " result"; if($total_results_count>1) echo "s";?> waiting for you!</h5>

    点赞 评论
  • drudmb2594
    drudmb2594 2015-04-16 17:32
    <?php
    if( $total_results_count > 1 ) {
        echo '<h5>There are '.$total_results_count.' results waiting for you!</h5>';
    } else {
        echo '<h5>There is 1 result waiting for you!</h5>';
    }
    
    点赞 评论

相关推荐