简体   繁体   中英

Echo a different value depending on a variable value

I have 2 variables in phtml and want to make them a link.

<?php
    $_reviewCount = $_ratingSummary->getReviewsCount(); 
    $_reviewUrl=$_product->getRequestPath().'#reviews';  

<!--Here if review count is 1 i want to show as "Review" else "Reviews" --> 

 <?php echo $_reviewCount ?>
<?php echo  "<a href='".$_reviewUrl."'>Review</a>" ?>
<?php ($_reviewCount == 1 ) ? __('Review') : __('Reviews') ?>

But the above shows only keyword 'review' in lowercase .

Just rewrite your code to first define the singular or plural form and then echo it:

<?php 
    if($reviewCount==1){$text='Review';}else{$text='Reviews';}    
    echo  "<a href=\"$_reviewUrl\">$_reviewCount $text</a>";
?>

BTW i'd change the if condition to >1 because your code echoes Reviews if the count of reviews is 0.

<?php 
    if($_reviewCount>1){$text='Reviews';}else{$text='Review';}    
    echo  "<a href=\"$_reviewUrl\">$_reviewCount $text</a>"; 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM