Jump to content

weird sh*t - working with integers and strings


ricmetal

Recommended Posts

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?

 

 

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 ===.

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?

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.

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.