繁体   English   中英

用if()在php中进行计算

[英]Calculations in php with if()

我正在尝试根据从数据库中收到的一些数据进行一些计算。 我认为我做的不对。 例:

if ($progressing > $constant && $regressing) {
    //Do something if $progressing is more than BOTH $constant and $regressing
}

这个对吗? 我似乎无法获得正确的结果。因此,我想我在这里做错了吗? 但是我不知道是什么。

PHP并没有像您认为的那样起作用:

if (($progressing > $constant) && $regressing)

在这里,PHP首先检查$progressing是否大于$constant 之后,第一部分( ($progressing > $constant) )和$regressing必须求值为TRUE,才能输入if语句。

您想要的是:

if ($progressing > $constant && $progressing > $regressing)

解释为:

if (($progressing > $constant) && ($progressing > $regressing))

表格更多信息请参见: 运算符优先级

如果进行比较并且还使用“ &&”,则需要在括号中加上以下内容:

if ( ($progressing > $constant) && ($regressing > $constant) ) {
    //Do something if $progressing is more than BOTH $constant and $regressing
}

您应该使用

 if ($progressing > max($constant, $regressing))

要查看进度是否较大,则两个变量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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