Unexpected string comparison result

I have two arrays $oldInfo and $newInfo , where $oldInfo is the existing data from the database, and $newInfo is the information entered by the user. If one particular field is different between the two, I want to enter a new row into the database, if this one field matches the one I just update.

 if($oldInfo['col'] != $newInfo['col']){ INSERT INTO ... } else { UPDATE WHERE ... } 

The problem is that the if statement always evaluates to true. I put the echo ... die() command in the if statement to print two values, and they both look the same. Old Info: 38mg/1, 20 New Info: 38mg/1, 20

Is there anything specific about string comparisons in PHP? These values โ€‹โ€‹seem to be exactly the same (in fact, $ oldInfo is used to pre-fill the form, so they should be the same), but the if statement says that they do not match.

+4
source share
2 answers

Try trimming the lines before comparing. Often, when this happens, this is due to the fact that the extra bit runs through one of them.

+3
source

Something funky can happen with PHP, which thinks it's the number 38, or with an encoding. Try to use! == instead! = For enforced strict equality.

-1
source

All Articles