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?

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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