PHP: by-reference-function with boolean as return value - strange notification

I have a function that returns a reference value by default, however the function should return falseif something went wrong while processing things in the function.

The function is declared as follows.

function &find($idx, $pref_array = false) {

    if ($pref_array === false)
        $pref_array = &$this->preferences;

    foreach ($pref_array as $key => $data) {
        if ($key == $idx) {
            return $pref_array[$idx];
        }
        else if (is_array($data)) {
            $res = &$this->find($idx, &$pref_array[$key]);
            if ($res !== false)
                return $res;
        }
    }

    return false;
}

PHP gives me a notice that "links to links should be returned only by reference." Do I really need to put $result = false;in my code and return $result? That would be kind of funny.

Thanks in advance for your help.

+3
source share
1 answer

, , , FALSE , FALSE .

, FALSE , OK, -. , :

function find($idx, & $found_value, $pref_array = false) {
   // pseudocode
   if found:
     $found_value = $array[$idx]
     return true
   else:
     return false
}

, , , , , TRUE, , $found_value.

, - ;)

+4

Source: https://habr.com/ru/post/1759753/


All Articles