Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-使if语句缩短两条语句_Php_If Statement - Fatal编程技术网

PHP-使if语句缩短两条语句

PHP-使if语句缩短两条语句,php,if-statement,Php,If Statement,我有下面的If语句,我想把它缩短一行,而不是把If语句写在一行 if ($start + $count > $total) { $count = $total; } 基本上,我想实现$count+$total永远不会高于$total,如果是这种情况,我想将$count设置为等于$total。您可以使用min: $count = min($total, $start + $count); 你想要的是所谓的三元运算 $count = (($start + $

我有下面的If语句,我想把它缩短一行,而不是把If语句写在一行

    if ($start + $count > $total) {
        $count = $total;
    }
基本上,我想实现$count+$total永远不会高于$total,如果是这种情况,我想将$count设置为等于$total。

您可以使用min:

$count = min($total, $start + $count);

你想要的是所谓的三元运算

$count = (($start + $count) > $total ? $total : null);
参考资料:

你可以用这个


这将if逻辑放在一行。

你想让它成为一行而不是一行吗?我的意思是我不想把if语句写在一行而只是让它成为一行。我的意思是要有一个不同的解决方案,然后把我的代码写进一行。这可能有点让人困惑。这假设一个不存在的$count=$start+$count;在原始if.@axiac-From-OP语句的else分支上,实现$count+$total永远不会高于$total,您可以看到别处还有其他部分
    if ($start + $count > $total) {
        $count = $total;
    }