Jump to content

Operator not working correctly


Go to solution Solved by scootstah,

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.

Edited by Monkuar
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.

Edited by Monkuar

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.

Edited by Monkuar

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.

  • Like 1

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.

  • Like 1
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.