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 8095281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:04:25+00:00 2026-06-05T21:04:25+00:00

Well, I know what references are and when it’s use is obvious. One thing

  • 0

Well, I know what references are and when it’s use is obvious.

One thing I really can’t get is that when it’s better to pass function by reference.

<?php

//right here, I wonder why and when
function &test(){

}

To avoid confusion, there’re some examples as how I understand the references,

<?php

$numbers = array(2,3,4);

foreach ($numbers as &$number){
   $number = $number * 2;
}

// now numbers is $numbers = array(4,6,8);


$var = 'test';
$foo = &var; //now all changes to $foo will be affected to $var, because we've assigned simple pointer 



//Similar to array_push()
function add_key(&$array, $key){
  return $array[$key];
}

//so we don't need to assign returned value from this function
//we just call this one

$array = array('a', 'b');

add_key($array,'c');
//now $array is ('a', 'b', 'c');

All benefits of using the references are obvious to me, except the use of “passing function by reference”,

Question: When to pass function by reference (I’ve searched answer here, but still can’t grasp this one)
Thanks

  • 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-06-05T21:04:27+00:00Added an answer on June 5, 2026 at 9:04 pm

    This is a function that returns by reference — the term “passing a function by reference” is a bit misleading:

    function &test(){
        return /* something */;
    }
    

    The use cases are pretty much the same as for normal references, which is to say not common. For a (contrived) example, consider a function that finds an element in an array:

    $arr = array(
        array('name' => 'John', 'age' => 20),
        array('name' => 'Mary', 'age' => 30),
    );
    
    function &findPerson(&$list, $name) {
        foreach ($list as &$person) {
            if ($person['name'] == $name) {
                return $person;
            }
        }
        return null;
    }
    
    $john = &findPerson($arr, 'John');
    $john['age'] = 21;
    
    print_r($arr); // changing $john produces a visible change here
    

    See it in action.

    In the above example, you have encapsulated the code that searches for an item inside a data structure (which in practice could be a lot more complicated than this array) in a function that can be reused. If you intend to use the return value to modify the original structure itself there’s no other option than returning a reference from the function (in this specific case you could also have returned an index into the array, but think about structures that do not have indexes, e.g. graphs).

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

Sidebar

Related Questions

I know that Java doesn't really use exact pass by reference, but rather pass
I know SQL well but I must be missing something really dumb here. This
I'm using Java 6. I want to parse XHTML that I know is well-formed.
well i do not know how to use sql union properly . i have
I know very well that null and undefined are distinct in JavaScript. However, I
Does any one know of any Java libraries I could use to generate canonical
Well, I don't know if it is possible, but the thing would be: struct
Well I know some kinds of UML's diagrams, but I haven't heard about this
I have a serious issue with Liferay 6.1 themes , I know well about
I know C# well, but it is something strange for me. In some old

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.