-4

I want to do a quick if-then to echo the s if it has more than 1 hour. The variables are already assigned if I were to do it on the next line. Any kind of help I can on this is greatly appreciated.

$img3['tdiff'] = $img3['tdiff']." Hour".if($img3['tdiff']>1) 
{ 
    echo "s";
}; 

I pretty much want to add the s if it has more than one hour... Please help me if you can, and thank you in advance!

4

2 回答 2

2

使用三元运算符:?:

$img3['tdiff'] = $img3['tdiff'] . " Hour" . ( $img3['tdiff'] > 1 ? "s" : "" ); 

虽然在这种情况下你会想要使用复数库。

于 2013-04-14T17:02:11.837 回答
-1
if($img3['tdiff'] > 1) {
    $s = "s";
} else {
    $s = "";
}
$img3['tdiff'] = $img3['tdiff']. " Hour". $s;

编辑:添加了一个 else 子句以避免未定义的变量

于 2013-04-14T17:03:15.037 回答