user2426701
user2426701

Reputation:

PHP return the value but false

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 :)

Upvotes: 2

Views: 155

Answers (4)

Daryl Gill
Daryl Gill

Reputation: 5524

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;

Upvotes: 0

dognose
dognose

Reputation: 20899

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";

Upvotes: 1

Zzz
Zzz

Reputation: 3025

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;
}

Upvotes: 1

joshfreemanIO
joshfreemanIO

Reputation: 11

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.

Upvotes: 0

Related Questions