2

For example, I would like to print total classes in the school

If.. that course has only one class, it will print "Total 1 Class" but if there are many classes in that course, it will print "Total 15 Classes"

So, I want to use syntax like

<?php echo "Total ".$attendance_setting->class_number.  
 if($attendance_setting->class_number==1) {echo " Class";} else {echo "
 Classes";} ?>

Is it possible or I have to use If statement before I print it.

Thank you

4

5 回答 5

0

我只是在回显语句之前执行 if 语句,如下所示:

<?php 

if($attendance_setting->class_number==1) {
    $class = " Class";
}
else {
    $class = " Classes";
} 

echo "Total " . $attendance_setting->class_number . $class;

?>
于 2012-08-29T17:08:18.540 回答
0

尝试关注

echo class_number.($attendance_setting->class_number==1)?'class':'classes';
于 2012-08-29T17:01:04.343 回答
0

您可以为此使用三元运算:

echo "Total " . $attendance_setting->class_number . " ";
echo ($attendance_setting->class_number > 0)
     ? echo "Classes"
     : echo "Class";
于 2012-08-29T17:01:52.490 回答
0

您也可以使用 switch() 语句

于 2012-08-29T17:14:28.953 回答
0

我一般定义一个这样的函数。

function add_plural_s($int=0){
    return $int > 1 ? 's' : '';
}

然后我写如下:

echo 'Class'.add_plural_s($attendance_setting->class_number);

Class如果不应该是复数并且如果应该是复数,则打印Classs

但是,我刚刚意识到,在您的情况下,它不会像我定义的函数那样工作,因为您需要的不仅仅是s. 但是你还是明白了。

于 2012-08-29T17:05:06.267 回答