0

Here is the simple code:

$result = 0;
$result = $obj->delete($this_id);
echo "Result:" . $result;
var_dump($result);
if ( (int) $result < 0 || $result == null ) {
   echo "Here" . $result;
       var_dump($result);exit;  
}

Here is the result:

Result:0int(0)
Here0int(0)

Its not supposed to enter into if block. Because $result is = 0. Not < 0.

Am I missing something or PHP handles this differently?

2
  • use === and see if that works Commented Sep 18, 2012 at 23:10
  • I want to check if the number is less than 0. How to use ===? Commented Sep 18, 2012 at 23:11

3 Answers 3

2

The comparison to null should be === instead of ==. Since null can evalulate to 0, the comparision evaluates (0 == null) = true

if ( (int) $result < 0 || $result === null ) {

See http://php.net/manual/en/language.operators.comparison.php for more information

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like the cast to an int type is making your variable test incorrectly against 0.

Replace if( (int) $result ... with if( $result ...

1 Comment

It was like that before. I added casting to see if it makes any difference. No change with or without casting.
0

Its not the first validation which matches:

var_dump(0 == null); //true
var_dump(false == null); //true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.