piątek, 17 lutego 2012

Never use boolean values in php define function

Last time I hade trouble with my defined global variable in php. I used simple code:

define("MY_VARIABLE", true);

and then tried to check if it was defined to true, so:

if(MY_VARIABLE == true)
{
    echo 'true';
}

It was ok... until i didn't defined it. Condition still was correct:

// When we remove this line: define("MY_VARIABLE", true);

if(MY_VARIABLE == true)
{
    echo 'true';
}

result is still 'true'. We just get notice, but most of php compilations have notices disabled.

Better way is to use string and compare it:

define("MY_VARIABLE", 'true');
if(MY_VARIABLE == 'true')
{
    echo 'true';
}

Or use strict condition with tripple =

if(MY_VARIABLE === true)

Now everything will be ok.

Brak komentarzy:

Prześlij komentarz