Odd String Comparison Problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have run into an odd problem. I have been able to isolate the larger
issue down to a simple string comparison problem. I am parsing a
string with the explode function of PHP. I then set variables equal to
the parsed values. Finally, I compare the variables to known values as
string literals, for example if($my_var != 'abc'){do stuff;}

The problem is that under no circumstance and with no compare method
(cmpstr, ==, !=) can I get a "correct" result. So a string with a
value of 'abc' derived from explode(',' , '"abc", "def"') compared to
the string literal "abc" indicates that the two values are NOT equal.

I have tried this on both a PHP4 and a PHP5 installation. Both are
shared hosting accounts.

It is certainly easy enough to simply create a small set of derived
variables and compare to them instead (which works fine, BTW), but
since this is actually just a snippet from a larger open source
application running successfully on millions of servers, and since I
am sure to upgrade as new releases come out, I wanted to seek an
understanding from the PHP community as to what could effect my
results this way - perhaps a setting, or a compile option? I would
appreciate hearing any ideas. For reference, here is the non-working

      $response_vars = explode(',', '"1", "1", "1", "This transaction
has been approved."');
      $x_response_code = $response_vars[0];
      $x_response_subcode = $response_vars[1];
      $x_response_reason_code = $response_vars[2];
      $x_response_reason_text = $response_vars[3];

      //check to see if the value is encapsulated; if so, remove
      $x_response_code = str_replace('|', '', $x_response_code);

      if ($x_response_code != '1') {
            echo 'true';
            echo '<br />' . $x_response_code;
        else {
            echo 'false';
            echo '<br />' . $x_response_code;

This code yeilds:


Re: Odd String Comparison Problem

Quoted text here. Click to load it

Well, that's true, and logical the string '"abc"' is not equal to the  =

string 'abc'.

Quoted text here. Click to load it

Resulting in:
array(0=3D>'"1"',0=3D>'"1"',1=3D>'"1"',2=3D>'"1"',3=3D>'""This transaction
Quoted text here. Click to load it

Notice the extra "'s in there.

Quoted text here. Click to load it

("1" !=3D 1) is offcourse true.

Quoted text here. Click to load it

No, it yields:

See the difference?
-- =

Rik Wasmus

Site Timeline