String Comparison Issues

The scenario is this:

  1. I keep a default value for a string in a config file.
  2. This value is used to populate a textarea in a form.
  3. User can change the text in the textarea if he/she chooses and submits
  4. I compare the $_POST value with the config value to see if there are any changes.

The problem I’m having is that strcmp, ==, and === all evaluate to false even if the text hasn’t changed.

I’ve checked and it doesn’t appear that the form is inserting any extra chars…

Any ideas?

Do you trim() the input value?

That function returns 0 when the strings match.


if(strcmp('yes','yes')) {
echo 'not equal';
} else {
echo 'equal';
}

Therefore, you may be getting the return values mixed up. In this case the function is going to return false (0) when there is a match and true (!0) when not a match. Although you may expect it to work the opposite way but that is not the case due the return value complexity for non-matches.

http://php.net/manual/en/function.strcmp.php