0

I'm making an API and in one of the endpoints the variable user_type that can only be a 0 or a 1

api/user?user_type=0 || api/user?user_type=1

Inside my controller i check if the variable equals to one of those two values, the problem is that the comprobation results in a boolean one, and if in the endpoint i add a text value for user_type results in true because the variable exists.

Is there any way of evaluating without triggering a boolean result? Something like this:

if($variable != 0 && $variable != 1){ return true } //It should be false if $variable == 'text' and true if variable == 0 or 1
Gabri
  • 70
  • 7
  • 2
    Have you heard the good word about our lord `===` and our savior `!==`? Edit: I really don't know what is it about avoiding booleans. – Theraot Feb 10 '22 at 11:51
  • https://stackoverflow.com/questions/80646/how-do-the-php-equality-double-equals-and-identity-triple-equals-comp?rq=1 – Ben Hillier Feb 10 '22 at 11:53
  • i guess you can use filtervar with parameter `FILTER_VALIDATE_BOOLEAN` which would returns true for "1", "true", "on" and "yes" ,returns false otherwise. On top of that `FILTER_SANITIZE_NUMBER_INT` since it would **remove all characters** except digits, plus and minus sign or just use plain old to compare === or !== – Emma Marshall Feb 10 '22 at 12:20

1 Answers1

0

PHP8 satisfies your condition without any modification. Or you can use one of the below conditions - 0 and 1 within quotes. OR Check with ! == and ===

Isha
  • 99
  • 1
  • 9