Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1007793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:40:20+00:00 2026-05-16T08:40:20+00:00

I have a function which returns a referenced value by default – however, the

  • 0

I have a function which returns a referenced value by default – however, the function should return false if something went wrong during 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 “Only variable references should be returned by reference”. Do I really need to put $result = false; in my code and return $result? That would be somehow ridiculous.

Thanks in advance for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:40:21+00:00Added an answer on May 16, 2026 at 8:40 am

    I think that that is not a good way to code a general find function because it would be impossible to determine if a FALSE value returned from the function means that the value of the searched item was in fact FALSE or if the searched item was not found.

    If you know that FALSE are not legal values than its OK, otherwise you should implement the find function in a different way.
    For example this way:

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

    This way you can always know if the requested item was found by checking the value returned from the function and if the returned value is TRUE you would have a reference to the found item in the variable referenced by the $found_value argument.

    Finally, this way you don’t get any strange notice 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which returns 3 numbers, e.g.: def numbers(): return 1,2,3 usually
I have a function func which returns true or false . Until func returns
If I have a function which returns a reference cursor for a query, how
Say, i have a function which returns a reference and i want to make
I have a function which returns a one-sided intersection of values between two input
I have this function which returns a datatype InetAddress[] public InetAddress [] lookupAllHostAddr(String host)
I have a php function which returns JSON in response to a ajax request
i have a php function which returns a random json encoded color <?php function
If you have a C function which returns an integer, you could write a
I have a native/unmanaged DLL and it has a CreateObject function which returns a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.