![]() With double equals, anything can be surprise = to anything or, or can be surprise casted against your will and != to something of which it should obviously be equal.Īnywhere you use = in PHP is a bad code smell because of the 85 bugs in it exposed by implicit casting rules that seem designed by millions of programmers programming by brownian motion. If you are using PHP, Thou shalt not use the double equals operator because if you use triple equals, the only edge cases to worry about are NAN and numbers so close to their datatype's maximum value, that they are cast to infinity. If you add 1 to number and they are already holding their maximum value, they do not wrap around, instead they are cast to infinity.įalse is the most dangerous value because False is = to most of the other variables, mostly defeating it's purpose. But "0x10" = "16" and "1e3" = "1000" exposing that surprise string conversion to octal will occur both without your instruction or consent, causing a runtime error. ![]() PHP Variables that have not been declared yet are false, even though PHP has a way to represent undefined variables, that feature is disabled with =. = is not transitive because "0"= 0, and 0 = "" but "0" != "" So 123 = "123foo", but "123" != "123foo"Ī hex string in quotes is occasionally a float, and will be surprise cast to float against your will, causing a runtime error. ![]() = will convert left and right operands to numbers if left is a number. Those who wish to keep their sanity, read no further because none of this will make any sense, except to say that this is how the insanity-fractal, of PHP was designed. This is not plagiarismĪ picture is worth a thousand words: PHP Double Equals = equality chart: Type comparison tableĪs reference and example you can see the comparison table in the manual:Įditor's note - This was properly quoted previously, but is more readable as a markdown table. So with strict comparison the type and value have to be the same, not only the value. If you are using the = operator, or any other comparison operator which uses strict comparison such as != or =, then you can always be sure that the types won't magically change, because there will be no converting going on.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |