String value equals true

2019-08-05 05:00发布

问题:

I have a function that returns TRUE or FALSE or "Test_operation", and I am looping it to do some things. As you can see the value of $comment_reply is Test_operation.

$comment_reply = $this->Profunction->insert_comments();
echo $comment_reply; // returns Test_operation
if ($comment_reply==TRUE)
{
    echo json_encode('Success');
}
elseif($comment_reply==FALSE)
{
    echo json_encode('Failed');
}
elseif($comment_reply =="test_operation")
{
    echo json_encode('This Action Cannot be done!');
}

But still

if ($comment_reply==TRUE)
{
    echo json_encode('Success');
}

This portion getting executed. Why does it happen?

In that function I am returning like this:

return TRUE;     // if all success
return FALSE;      // if there is some problems
return "Test_operation";     //No insertion need to be done,may be for preview purpose.

SOLVED : I changed bool values to string. So it will be

return 'TRUE';     // if all success
    return 'FALSE';      // if there is some problems
    return "Test_operation";     //No insertion need to be done,may be for preview purpose.

回答1:

Not sure it's your issue, but if you want to enforce equality by type and value, use the === operator.

You can check this out in more detail on the comparison operator page.

Happy coding



回答2:

Try using === instead of ==