Ran into a PHP problem this morning, and Olle helped me find a workaround, so I thought I should publish here for the benefit of others.
If you are comparing two values in PHP — let’s say a password that a user has entered and a password that it stored in a database. In most cases you can compare them like this:
But — and here’s the problem — if the two values you are comparing both look to PHP like they are numbers, PHP will compare them as numbers even if they “look different.”
For example, if you are comparing 1234 with 001234, PHP will consider them to be “equal” if you compare them as above.
This is because PHP is “too smart for its own good” and “helpfully” converts all integer-like strings to integers for comparison purposes (as explained here: “If you compare two numerical strings, they are compared as integers”).
The simple solution:
By prepending a string (“s”) to each side of the comparison, PHP is prevented from helpfully converting the string to integers, and the two are thus deemed not equal as they should be.
Thanks for the help, Olle.