I have some code that looks like this:
$log->error( 'addToWatch=['.$addToWatchPage.'] success=['.$success.']' );
if( ( $success == 1 ) and ( $addToWatchPage == true ) ) {
$log->error( 'adding to watch page' );
} else {
$log->error( 'NOT added to watch page' );
}
When I run it, I am always going into the if statement and never into the else statement even if $addToWatchPage
is false. See my log output below:
Sat Jan 26, 2013 @ 6:02 addToWatch=[false] success=[1]
Sat Jan 26, 2013 @ 6:02 adding to watch page.
Can anyone explain why this is broken?
Probably
$addToWatchPage
is a string containingfalse
, otherwise your resultant first log message would be:This would result in
$addToWatchPage == true
being evaluated astrue
, since'false' == true
is a truthy expression (actually, any non-empty string other than'0'
is true when casted to boolean).Make sure
$addToWatchPage
is a boolean value instead of a string.