If语句,将值与=进行比较


If statement, comparing values with =

这个if语句正确吗?

if ($row->totMED="0" or $row->MEDC="0"){
  $avgMed='N/A';
} 
else {
  $avgMed='Medical: $'.($row->totMED / $row->MEDC);
}

您正在寻找:$row->totMED == "0"$row->totMED === "0"

松散的相等

==是松散相等的,这意味着被比较的值在值上是相似的。例如,所有这些陈述都是正确的:

0 == false //true because 0 is like nothing
"" == false //true because an empty string is like nothing
1 == true //true because 1 is something

"abc" == true可能为真,具体取决于…。在php中,目前为真,而在javascript中则不然。这就是不平等的问题。检查过程可能很复杂,结果可能出乎意料。严格的平等是好的。

严格平等

===,或严格相等,在值AND类型中表示相同。所有这些都是真的:

1 === 1
true === true
'abc' === 'abc'

这些都是假的:

1 === "1" // first value is integer and second is a string
true === "true" //first value is a boolean and second is a string

基本分配运算符

单个=是一个赋值运算符,它将左边的变量设置为右边的值。使用=时,您设置的是变量的值,而不是比较两个值。

$row->totMED = "0"表示$row->totMED现在的值为"0"。