0

Is there a way to save a boolean true/false value in a function to a variable?

function check() {
    if(1 = 1) {
        return false;
    }
}
$status = check();
if($status === false) {
    echo "sign in please";
}

As of now $status comes out to be null. I don't like having to use if(check() === false) {} since a lot of my function have a lot of arguments. And setting $status within the function would defeat the purpose of a universal function that can be used for multiple variables.

How do you guys handle this?

EDIT: I apologize, something else in my code was causing it to be null. Tried to make a test (above code) and when it failed, I thought it was a worthy question :P

4

1 に答える 1

2

単一の等号は代入であり、比較ではありません。3 つの等号で修正できます (2 つの値がまったく同じかどうかを比較します)。

function check() {
    if(1 === 1) { // Here
        return false;
    }
}
$status = check();
if($status === false) {
    echo "sign in please";
}
于 2012-09-08T05:26:14.273 に答える