prathyush Posted July 20, 2009 Share Posted July 20, 2009 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>'; } Quote Link to comment Share on other sites More sharing options...
mattal999 Posted July 20, 2009 Share Posted July 20, 2009 Because in the second you are testing for an integer, when the variable you should be testing for is a string. This will result in unexpected behaviour. And btw, it returns Success because the string 'E' does not equal 0. Quote Link to comment Share on other sites More sharing options...
ignace Posted July 20, 2009 Share Posted July 20, 2009 This isn't a bug. For more information: http://us2.php.net/manual/en/types.comparisons.php Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 20, 2009 Share Posted July 20, 2009 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. Quote Link to comment Share on other sites More sharing options...
ignace Posted July 20, 2009 Share Posted July 20, 2009 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 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 20, 2009 Share Posted July 20, 2009 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 Quote Link to comment Share on other sites More sharing options...
ignace Posted July 20, 2009 Share Posted July 20, 2009 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. Quote Link to comment Share on other sites More sharing options...
d.shankar Posted July 20, 2009 Share Posted July 20, 2009 lol , the topic starter hasn't even replied... but the discussion goes on phpfreaks rocks ! Quote Link to comment Share on other sites More sharing options...
ignace Posted July 20, 2009 Share Posted July 20, 2009 lol , the topic starter hasn't even replied... but the discussion goes on phpfreaks rocks ! We get to the bottom of it Quote Link to comment Share on other sites More sharing options...
d.shankar Posted November 4, 2009 Share Posted November 4, 2009 thats true.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.