0

I am having a bit of trouble wrapping my head around this, so I thought I will ask it..

I have this code:

$x="string";
var_dump($x==0); //says true
var_dump($x==true); //says true
var_dump(true==0); //says false

What I understand is:

In 1, `string` gets converted to number, which becomes `0` so condition is true
In 2, `string` is a value, so condition is true
In 3, `true` is not equal to `0` so condition is false

Individually they all make sense, but in a sequence they just don't! I have heard many people say it is because the conditional operator in PHP is not transient. Can someone explain what that means, and how to make sense of this?

4

1 回答 1

0

为什么这世界上没有意义?您永远不会更改x的值,因此这些语句取决于x的原始值,即“字符串”。

您假设x在第一个 var_dump 中更改为 0,但事实并非如此。它仅与 0/false 进行比较。此语句将执行您认为正在发生的变量更改:

var_dump(($x = 0) == 0);

或者:

var_dump(($x = false) == 0);
于 2013-04-25T14:05:58.570 回答