It has been noted in passing, but not stressed, that integer and double are different types, so the following code: $n = 1000 Įcho ''. Read more in What is the difference between = and = in PHPĪll of the answers so far ignore a dangerous problem with =. The following would therefore be correct: This operator also checks the datatype of the variable and returns (bool)true only if both variables have the same content and the same datatype. This tries to convert both variables into the same data type before the comparison and only then checks whether the content of these variables is the same. The standard comparison operator (=) in PHP behaves differently. In most programming languages, the comparison operator (=) checks, on the one hand, the data type and on the other hand the content of the variable for equality. So for things like that, you would use the = instead, so that the data type is checked. If (!array_search("name", $arr)) // This is the same as doing (array_search("name", $arr) = false) But actually, this is exactly the same as using =false, so if you did: $arr = array("name") Most people don't use = false when checking if a function returns false. So, do you see how this could be an issue now? actually returned false.when it didn't. in), but because we're using =, we'll think the function This would return 0 (the key of the element the val was found But, what if you did an array_search() on a value that was stored in the first element of the array (which would have the array key of 0).the array_search() function would return 0.which is equal to false. If the value could not be found in the array, it returns false. The array_search() function simply searches for a value in an array, and returns the key of the element the value was found in. Simple - let's take a look at one of PHP's functions: array_search(): That condition would not be true, as $var != true it only = true (if you know what I mean). With =, the data types are checked to make sure the two variables/objects/whatever are using the same type. So if you had a variable that is 1 (which could also be true):Īnd then compare with the =: if ($var = true)īut $var does not actually equal true, does it? It has the int value of 1 instead, which in turn, is equal to true. The = does not care about the data types when comparing: 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 |