2

I have this simple function:

function isMember($uID, $pdo) {
$status = getUserStatus($uID, $pdo);
    if(isAllowed($status['status']))
            return $status['status'];
    return false;
}

Now I am looking for a way to return false yes, but to return also the value of the variable.

I tried the following, but it makes it empty anyway:

return $status['status'] == false;

So the logi is return false anyway but give me back also the value of the variable, even if it's false, because false should not mean empty :)

1
  • You want return $status['status'] == false; (notice the double equal sign). Commented Jun 20, 2013 at 16:31

4 Answers 4

1

Return an array, and use the list() method to get your results:

function isMember($uID, $pdo) {
    $status = getUserStatus($uID, $pdo);

    $statusString = $status['status'];
    $statusFlag = false;
    if(isAllowed($status['status']))
       $statusFlag = true;

    return array($statusFlag,statusString);
}

//usage
list($flag,$msg) = isMember(5,"whatever");
echo "Access: $flag, with message $msg";
Sign up to request clarification or add additional context in comments.

Comments

1

A function can not return multiple values, but similar results can be obtained by (1) returning an array or by (2) passing a variable by reference and storing the value you want returned in that variable.

  1. You will need to write your function in a way that it returns an array containing the following:

    • The value you wan't returned
    • A flag that signifies true/false
  2. Pass a variable by reference into your function and store the value of the status in that variable.

    function isMember($uID, $pdo, &statByRef) {
$status = getUserStatus($uID, $pdo);
if(isAllowed($status['status'])) {
return $status['status'];
}
$statByRef = $status['status']; return false;
}

2 Comments

Sorry all, couldn't get the last function to format correctly.
Fixed it with <pre><code></pre></code> the text editor wasn't formatting correctly :)
0

False returns empty in PHP, see http://php.net/manual/en/function.empty.php

From documentation: Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

Comments

0

Try using something like this:

function isMember($uID, $pdo) {
$status = getUserStatus($uID, $pdo);
    if(isAllowed($status['status'])){
            return $status['status'];
    }
    return false;
} // If isAllowed returns true, then PHP will return $Status['Status'];, if not then PHP will by default return false.

I have noticed you haven't used braces which makes the code a little awkward to debug. Then validate like:

if (isMember($Var,$AnotherVar) !== false){
 //isMember has not returned false, so PHP is operating within these braces
}

Such a simple thing, which should be most effective.


If your wanting to assign true/false to $status['status']; then you are performing the right method, but wrong operator.

== is a comparision operator. Not assignment

= is an assignment operator, so your assignment should be:

$status['status'] = false;

Comments

Your Answer

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