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

  • Home
  • SEARCH
  • 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 6542593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:14:47+00:00 2026-05-25T11:14:47+00:00

Perhaps this is just a stylistic question, or perhaps there is a right answer.

  • 0

Perhaps this is just a stylistic question, or perhaps there is a “right” answer.

If I have a php function that needs to return multiple values (and those are incoming args) which is preferred:

function myFuncReturnVals ($a, $b) {
 // do stuff
 return array ($a, $b)
}

function myFuncByRef (&$a, &$b) {
 // do stuff
}

list($a,$b) = myFuncReturnVals ($a,$b);
myFuncByReb($a,$b);

Either way will get the job done, but which is preferred?

ReturnVals seems to be a little cleaner in some respects as it lets the caller decide if their values are going to be modified.

However, sometimes by ref seems to fit better as the intent of the function is really to muck with that data.

What about mixing and matching? What if you have a function that mostly is responsible for giving back some data, but in process might have additional information about the incoming parameter?

  • 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-25T11:14:48+00:00Added an answer on May 25, 2026 at 11:14 am

    I tend to avoid reference parameters like the plague: it’s obvious what they do at the point where you’re defining them, but most of the time, you’ll be looking at their usage, and there it isn’t clear at all whether those parameters are by-reference or by-value.

    Sticking to the return-as-array paradigm is more readable, and makes it easier to avoid side effects; it also allows nesting / chaining function calls – the kind of style that looks like this in action:

    $output = nl2br(
              detect_links(
              htmlspecialchars(
              trim($input))))
    

    If you have more than one function returning the same kind of value collection, it may be worth wrapping them in a class – this way, your return value becomes more descriptive even without (phpdoc) comments, and you will be able to catch invalid input to other functions that consume such values by adding type hints to the function argument.

    Also, note that objects are reference types already, so passing them by reference only makes sense if you want to replace the entire object – altering individual fields or calling destructive methods will alter the original object even if you had passed it by value. Example:

    <?php
    class Bar {
        public $x;
        public function __construct($x) { $this->x = $x; }
    }
    
    function foo($bar, $x) {
        $bar->x = $x;
    }
    
    $myBar = new Bar(23);
    $foo($myBar, 42);
    printf("$foo->x is now %s\n", $foo->x);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is just out of interest, and perhaps could be useful for my
I have an ObjectDataSource (but perhaps this question is the same for all kinds
Weird question, perhaps. We have a number of simple utilities written in-house that need
Perhaps this is my lack of understanding, but I would have assumed that doing
Perhaps i am being stupid by asking this just now, but... I notice that
I probably just haven't thought this through, or perhaps I'm simply unaware of an
I'm perhaps being a bit lazy asking this here, but I'm just getting started
Perhaps this is a naive question. In my understanding, ASP.NET MVC cannot work with
Perhaps this question has been asked elsewhere, but I'm unable to find it. With
[EDIT] Hmm. Perhaps this question should be titled what is the default user-input dialog

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.