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

How to Create a new Case Record Type in Salesforce

Here are the specific steps for adding a case record type. Some steps may vary by organization but the process should be pretty similar to what is shown here. 1) Create a new page layout 2) Add any new case statuses to picklist 3) Edit case statuses that should indicate case is "closed" 4) Add any new case types to picklist 5) Add any new case reasons to picklist 6) Add any new case origins to picklist 7) Add any new fields to case object 8) Under Record Types, ensure the picklists have the correct values (i.e. - Reason/Type/Origin) 9) Within the Type field, edit "Reason" field dependencies matrix and add new reason(s) to types 10) Create a new support process (if not reusing an existing one) 11) Create the new record type (enable for all profiles) 12) Finalize the page layout (if needed) and check "Default" in Case Assignment checkbox under Layout Properties 13) Create approval queues (if needed) 14) Set up approv

View Helpers - Custom Output Parser

I was reviewing some blade view code recently and decided it was too verbose. There were some long output statements that were duplicated throughout the view and that always signals a need to refactor or come up with a better method. So I wanted to share (or at least document) what I came up with here. First, the issue: fields stored as boolean "0/1" in the database must be output in "Yes/No" format. Plus, if the field is blank (or null), that must be reflected as well. Add to that the uncertainty of whether the production Microsoft Azure database will return a string or integer value for 1 or 0. This problem necessitates a blade statement like: {{ $object->property === null || $object->property === "" ? "" : $object->property == 1 ? 'Yes' : 'No' }} Like I said...verbose! One solution might be to create a Laravel accessor for the boolean field in the model. But that would mean that if I want the actual boolean