Jump to content

An interesting bug !!


prathyush

Recommended Posts

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
https://forums.phpfreaks.com/topic/166596-an-interesting-bug/#findComment-878468
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
https://forums.phpfreaks.com/topic/166596-an-interesting-bug/#findComment-878469
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
https://forums.phpfreaks.com/topic/166596-an-interesting-bug/#findComment-878471
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
https://forums.phpfreaks.com/topic/166596-an-interesting-bug/#findComment-878476
Share on other sites

  • 3 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.