Jump to content

Operator not working correctly


Monkuar

Recommended Posts

if($value != "0"){
				$errors[] = 'Invalid option chosen';
			}
$value is a string.

 

IF $value equals '00', it doesn't work why? It does not equal 0?

 

I'm trying to check if a value equalis 1 or 0. LITERALLY the string has to be 1 or 0, if not I need to error out. Why is this so difficult?

 

How does 2 zero's (00) = 0? Doesn't make sense.

 

Especially when it's matching 2 STRINGs.. If it were a numeric value I could see PHP saying 00 might equal 0. But how does 00 and 0 equal the same thing? makes no sense.

Link to comment
https://forums.phpfreaks.com/topic/294189-operator-not-working-correctly/
Share on other sites

You should use strict type comparison.

 

if ($value !== '0')
This will look for string '0'.

 

BOOM

 

if($value !== "0" AND $value !== "1"){
				$errors[] = 'Invalid option chosen';
			}
Got it now.

 

So the word 'strict' is what I was looking for, thank you scootstah!

 

Now these values HAVE TO BE 1 or 0, no matter what. No injection, nothing is possible. Thank you. Going to array map intval these too just in case.

Going to array map intval these too just in case.

Well, then it won't work. Then your value is an int but you're comparing it to a string. Also, if your value was a non-int string before hand then it will be converted to int 0, which might be breaking your logic.

Well, then it won't work. Then your value is an int but you're comparing it to a string. Also, if your value was a non-int string before hand then it will be converted to int 0, which might be breaking your logic.

Yeah, I just got some false positives. I would have to switch back to != if they were integers right? Anyways, I'm not going to array map them, as this a very strict match. No need

 

I just find it sad I spent like 1 hour trying to figure this out, I feel like absolute shit and should be ashamed.

Yeah, I just got some false positives. I would have to switch back to != if they were integers right?

No, just change your comparison to an int as well.

if ($value !== 0)

I just find it sad I spent like 1 hour trying to figure this out, I feel like absolute shit and should be ashamed.

Ha, trust me we've all been there. We've all been stuck on stupid little problems like this. Sometimes you just need to walk away and do something else for a bit.

If you want to know why it does what it does when using !=, see the manual on comparisons, specifically:

 

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

Basically if both strings consist of something that is_numeric says is a number, PHP will convert it to a number then compare them. So '00' become (int)0 and '0' becomes (int)0. 0 != 0 is false.

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.