Skip to main content

Debate: Identical Operator vs. Equality

Sometimes I call this "loose equality" (==) and "strong equality" (===) but regardless of what you call it, the inclusion of loose equality is one of the most maligned features of PHP.  Traditional programmers say using loose equality operators is a terrible practice - and for the most part, they are right.  In general, when you are checking if two variables are equal, you really mean to check if they are identical.  In other words, a string should not equate with an integer, even though they may *look* the same to a human.

However, what traditional programmers tend to miss is that one of PHP's strengths is the ability to be portable.  Strong equality is one of the things that breaks portability.  Therefore, assuming you are validating input and working in the realm of positive integers (non-zero), loose equality is the only way to go!

An example of this issue came up for me recently as I was moving my code from MySQL to Azure/MS-SQL in Laravel 5.3.  For whatever reason, when queried from MS-SQL, integer column values may be returned as either integers or strings.  This does not happen with MySQL!  So suddenly two values that you'd expect to be identical are only *equal* (one is a string, one is an integer).  I don't know if this is a quirk only with MS-SQL but when data sources disagree on how to return data it argues in favor of using loose equality for portability over strong equality (and bugs!).

I will say that Laravel has ways to ensure repository data typing, called "accessors".  Accessors allow you to specify how data gets returned from an Eloquent model.  They can be especially useful if you want to "friendly format" a date (like, "2 days ago" versus the actual date).  However, defining accessors for every integer value in a large database can be a monumental task that is not even necessary for all platforms.  With that in mind, loose equality can be very useful in avoiding bugs based on varying data type.

Of course, it is fair to still expect programmers to validate user input and sanitize or type cast.  When dealing with user input, strong equality is definitely the way to go.

Comments

Popular posts from this blog

jQuery noUIslider Pip Label Customization

Recently, I was tasked with creating a slider element for a user to select their credit rating, of which the user can select from among: 'Poor', 'Fair', 'Good', 'Excellent' and 'Not Sure'.  Several problems presented themselves here: A drop-down box would be better, but that's not what the requirements specified. I already have several numeric sliders on my page and I want them all to match. I selected the jQuery noUi Slider library to create the numeric sliders. noUi Slider does not support string values - only numeric values. The "pips" (slider scale/labels) also does not support string values. Since the solution involved shifting my mindset, I wanted to document it here for future reference and maybe to help someone facing the same requirements. Here's my solution: Since there is really no numeric correlation to credit score (especially considering one of the options is "Not Sure"), I will just u...

Regarding Technology (a departure from WebDev)

I'm departing from WebDev for a minute to blog about technology. I'm as guilty of over-indulging in technology as anyone, especially given that my career demands a technology overload. Today at noon is the solar eclipse and that prompted me to think about the past and the future. I started to think about where the world stands and a song came up on my playlist that seemed perfect for my mood. 27 years ago, Bad Religion wrote a song about the current generation of kids. It would be prophetic if it weren't for the fact that the trend had already begun back then. In any event, it seems now is a good time to look back at the lyrics: 'Cause I'm a twenty-first century digital boy I don't know how to live but I got a lot of toys My daddy's a lazy middle-class intellectual My mommy's on Valium, so ineffectual What child these days does not have multiple devices to play with? Gaming systems, television, computers and tablets are in nearly every home. Kid...

Java...Still Sucking Hard, Compared to Other Languages

I know there are a bunch of Java devotees out there.  I frequently have them evangelizing the language to me as a "strongly-typed" language that is so much better than loosely-typed languages, like PHP.  So here is something that just came up for me and I'd like to ask all those devotees WHY they like the type enforcement. Here's a bit of code to store a random integer as a string in PHP: $myString = rand(5); And now...the Java version: String myString = String . valueOf ( Integer . valueOf ( Math . random () * 10000 )); I get it...you need to keep your phony-baloney job and writing more  code and making it more complicated can help you keep it out of the hands of less "intelligent" programmers. As for myself, I will always prefer a language that lets me write shorter, more eloquent code and get my applications built faster.  But then...maybe I'm just simple.