awestover89
awestover89

Reputation: 1763

Unexpected result comparing strings

I have two arrays $oldInfo and $newInfo where $oldInfo is existing data from the database and $newInfo is info entered by the user. If one specific field is different between the two I want to enter a new row in the database, if that one field is the same than I just update.

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

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

Is there something specific about string comparison in PHP I am missing? These values seem to be exactly the same (in fact, $oldInfo is used to prepopulate the form so they should be the same) but the if statement is saying they are not the same.

Upvotes: 4

Views: 113

Answers (2)

mwhite
mwhite

Reputation: 2111

Something funky might be going on with either PHP thinking it's the number 38, or with encoding. Try using !== instead of != to force strict equality.

Upvotes: -1

Daniel Bingham
Daniel Bingham

Reputation: 12914

Try trimming the strings before comparison. Often when this happens it is because an extra bit of whitespace snuck into one of them.

Upvotes: 3

Related Questions