paradigmapc Posted December 3, 2007 Share Posted December 3, 2007 I'm sure there is a perfectly logical explanation behind this but why does print (0 == "STRING") ? "0 is equal to STRING" : "0 is not equal to STRING"; print 0 is equal to STRING no need to say to the identity operator "===". I'm just curious to the reason of why "STRING" would be converted to zero during the comparison. Thanks Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 3, 2007 Share Posted December 3, 2007 welll you aren't converting aynthing you are using a print which is a function itself and doesn't take an else if operator Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 3, 2007 Share Posted December 3, 2007 <?php if(0 == "STRING"){ echo "0 = STRING"; } else{ echo "0 != STRING"; } ?> Quote Link to comment Share on other sites More sharing options...
paradigmapc Posted December 3, 2007 Author Share Posted December 3, 2007 This code still prints "0 = STRING": <?php if(0 == "STRING"){ echo "0 = STRING"; } else{ echo "0 != STRING"; } ?> as does: print (0 == "STRING") ? "0 is equal to STRING" : "0 is not equal to STRING"; I think you may have misunderstood by question: Within the if statement 0 and "STRING" are being compared but because the identity operator (===) was not used string is being converted to a int in order to be compared. Why does a string convert to 0 when being compared to an int? My guess is that what ever is converting the string to an int is returning zero because it cant be converted, but I am not sure, and then again my whole thought process of the event could be wrong. Sorry for the confusion. Quote Link to comment Share on other sites More sharing options...
trq Posted December 3, 2007 Share Posted December 3, 2007 This looks like a bug to me. "STRING" should actually equate to 1 (true). <?php echo ('STRING') ? 'true' : 'false'; // returns as expected true. echo ('STRING' == TRUE) ? 'true' : 'false'; // returns as expected true. echo ('STRING' == FALSE) ? 'true' : 'false'; // returns as expected false. echo ('STRING' == 0) ? 'true' : 'false'; // returns as NOT expected true. ?> Quote Link to comment Share on other sites More sharing options...
lur Posted December 3, 2007 Share Posted December 3, 2007 This looks like a bug to me. "STRING" should actually equate to 1 (true). Nope. (int) 'STRING' = 0; (bool) 'STRING' = TRUE; Quote Link to comment Share on other sites More sharing options...
trq Posted December 3, 2007 Share Posted December 3, 2007 This looks like a bug to me. "STRING" should actually equate to 1 (true). Nope. (int) 'STRING' = 0; (bool) 'STRING' = TRUE; Yeah, I realise now that a string is converted to an int (with a value of 0) when compared with an int. 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.