Jump to content

An interesting bug !!


prathyush

Recommended Posts

Why does the second return success do you think?

 

$txnResponseCode = "E";

 

if ($txnResponseCode != "0") {

 

echo 'fail<br>';

 

 

} else {

 

echo 'success<br>';

 

}

 

if ($txnResponseCode != 0) {

 

echo 'fail<br>';

 

 

} else {

 

echo 'success<br>';

 

}

Link to comment
Share on other sites

The reason for this behavior is because, as the manual page says:

 

If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers. These rules also apply to the switch statement.

 

If you cast 'E' as an integer, it is 0.

 

Edit: Holdup. I think i read this wrong.

Edit again: No, i didn't.

Link to comment
Share on other sites

If you cast 'E' as an integer, it is 0.

 

Don't he cast E to ord(E)=69 and use that to compare it to 0? $txnResponseCode != 0 thus 69 != 0 => true. So to what does he cast the entire if statement?

 

it returns Success because the string 'E' does not equal 0.

 

if ($txnResponseCode != 0) { // E does not equal 0 => true
echo 'fail<br>';
}

 

Edit: my first reply was to fast without proper thinking it through

Link to comment
Share on other sites

Don't he cast E to ord(E)=69 and use that to compare it to 0? $txnResponseCode != 0 thus 69 != 0 => true

 

No.

 

This code:

 

$var = (int) "E";
var_dump($var);
$var = (float) "E";
var_dump($var);

 

Produces:

 

int(0)

float(0)

 

The reason I cast it to both an integer and a float is because the string contains the character 'E'. Any strings containing e, E, or '.' are converted to a float prior to comparison with a numeric value. See this page: http://us2.php.net/manual/en/language.types.string.php#language.types.string.conversion

Link to comment
Share on other sites

Do not expect to get the code of one character by converting it to integer, as is done in C -- http://us2.php.net/manual/en/language.types.string.php#language.types.string.conversion

 

Didn't knew their were differences between languages thought they all used the same mechanism.. Thank you Ginger for pointing this out.

Link to comment
Share on other sites

  • 3 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.