1

As the title says, PHP seems to be evaluating the integer value 0 as false.

Take for example this snippet:

http://sandbox.onlinephpfunctions.com/code/13d885fb68359a3154999c2ef85db7c913c49bc5

<?php

if($exists = checkDup()){
    echo $exits;
}
else{
    echo "error!";  
}

function checkDup ($foo = 'blah', $bar = 'blah'){
    if ($foo == $bar) return (int) 0;
    return false;
}

As you can see, despite casting the reply as an int PHP parsing the return as false which in incorrect.

1
  • Because that's what it is flagged as.. return it as "0" Commented Apr 7, 2017 at 16:27

2 Answers 2

5

PHP is evaluating a lot to false ;) For example null, '', 0 You have to include a type check as well, you can do so by using === or !==

$exists = checkDup();
if($exists !== false){
    echo $exits;
}
else{
    echo "error!";  
}

function checkDup ($foo = 'blah', $bar = 'blah'){
    if ($foo == $bar) return 0;
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

2

You should use if($exists = checkDup() !== false)

0 == false; //true

0 === false; //false

When you don't specify the a boolean expression in the if, it will execute the == operator

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.