ricmetal Posted March 8, 2012 Share Posted March 8, 2012 i hit a wtf moments print $_POST['idName']; // prints 0, correct, the value of the selected select element item is indeed 0 print '<br>'; $value = intval($_POST['idName']); // otherwise its a string if($value == 'empty') { print '<br>wtf does this get printed?'; } the $values value is the number zero, not the string 'empty', so why does it eval as one? Quote Link to comment https://forums.phpfreaks.com/topic/258539-weird-sht-working-with-integers-and-strings/ Share on other sites More sharing options...
requinix Posted March 8, 2012 Share Posted March 8, 2012 Comparison with various types When converting a string with a number, the string is converted to a number and the two are compared. "empty" becomes 0. If you want an exact comparison, without any type conversions, use ===. Quote Link to comment https://forums.phpfreaks.com/topic/258539-weird-sht-working-with-integers-and-strings/#findComment-1325284 Share on other sites More sharing options...
ricmetal Posted March 8, 2012 Author Share Posted March 8, 2012 in other words, when you assign a converted string to a number to a variable, the == operator breaks down, when testing against said variable?i don't get the relation between the assigned data (zero) to a random test string (empty). is there any? Quote Link to comment https://forums.phpfreaks.com/topic/258539-weird-sht-working-with-integers-and-strings/#findComment-1325297 Share on other sites More sharing options...
requinix Posted March 8, 2012 Share Posted March 8, 2012 What you did with that variable doesn't matter. That's in the past. What matters is that you're using == to compare a string and a number. When you do so, PHP converts the string to a number. The string "empty" isn't a valid number so PHP uses the default of zero. Quote Link to comment https://forums.phpfreaks.com/topic/258539-weird-sht-working-with-integers-and-strings/#findComment-1325302 Share on other sites More sharing options...
ricmetal Posted March 8, 2012 Author Share Posted March 8, 2012 thanks so much dude Quote Link to comment https://forums.phpfreaks.com/topic/258539-weird-sht-working-with-integers-and-strings/#findComment-1325306 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.