-2

I've been trying to make a system where certain flags in a MySQL database are checked with php. To see if a certain letter is in the string, I use the function strpos(). If the letter I'm checking for happens to be the first one in the string the function returns 0. However this result seems to be synonymous with the Boolean value for false. I tested this theory with the below code:

if(0 == false){
    echo 'true';
}

low and behold this evaluates to true and echoes the output inside the block. So, is there a logical way to distinguish between a character being at index 0 or not being part of the string at all? One option that occurred to me was to basically prepend the flags string with some character like an underscore. This way no meaningful characters would ever be at index 0 of the function's return value. That seems like a very dirty patchy way to fix things though. If you have any other ideas I would greatly appreciate your help. Thanks for reading.

UnWorld
  • 129
  • 9
  • Why don't you add just simple `1` to your `strpos` output.! – Umair Shah Apr 24 '16 at 20:00
  • The manual is a good resource, this shows tables of what you can expect from loose/strict comparisons: http://php.net/manual/en/types.comparisons.php – JimL Apr 24 '16 at 20:03
  • Umm @UmairShahYousafzai: `echo (false+1);` – AbraCadaver Apr 24 '16 at 20:03
  • If you bother reading the [PHP Docs for the strpos() function](http://www.php.net/manual/en/function.strpos.php) it actually explains this in great detail..... always check with the PHP docs before asking on StackOverflow – Mark Baker Apr 24 '16 at 20:04
  • @AbraCadaver : Ahaa...I can see that now..! seems like I didn't get the question..! – Umair Shah Apr 24 '16 at 20:06
  • That was their assumption but incorrect. – AbraCadaver Apr 24 '16 at 20:08
  • @AbraCadaver : Sorry for asking stupid question... `0 === false` can never be true as `0` is `int` and `false` is boolean.! `===` equals also compare their datatypes so ofrourse it will not be true..! – Umair Shah Apr 24 '16 at 20:17
  • @UmairShahYousafzai: My point was `false+1` is `1` so adding `1` to anything is a non-zero int and you will never get false so it doesn't matter how you compare it. – AbraCadaver Apr 24 '16 at 20:36
  • @AbraCadaver : Yes...indeed..! – Umair Shah Apr 24 '16 at 20:45

1 Answers1

1

Use ===:

if(0 === false){
    // will never be true
    echo 'true';
}
Andreas
  • 2,821
  • 25
  • 30