UnWorld UnWorld - 1 year ago 88
PHP Question

PHP 0 vs False ambiguity

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

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

Answer Source

Use ===:

if(0 === false){
    // will never be true
    echo 'true';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download