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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:08:59+00:00 2026-05-25T12:08:59+00:00

I can change $var in my function in one of two ways: either pass

  • 0

I can change $var in my function in one of two ways: either pass it by reference or using the global keyword.

$var1 = 10;

function test1() {
    global $var1;
    $var1++;
}

function test2(&$var) {
    $var++;
}

Both approaches have the same result, but is there any difference between them? Which one is preferred and which one is faster?

  • 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-25T12:09:00+00:00Added an answer on May 25, 2026 at 12:09 pm

    1. None of them is preferred.

    Unless you have a special reason to do otherwise, the preferred would be

    $var1 = 10;
    $var1 = test3($var1);
    function test3($var)
    {
        return $var + 1;
    }
    

    Introducing coupling between different parts of your program (if using a global) is something you should always reasonably try to avoid.

    In addition, if there is no concrete reason to make your function accept its argument by reference you should also avoid doing that. Only a very miniscule fraction of all functions behave this way, so if nothing else you are risking confusion among the developers who use this code for no real benefit.

    2. You do not need to think about which one is faster.

    Unless you have profiled your application under real world scenarios and have found that this function is a bottleneck (which of course it will never be in this simple form), then optimizing for performance at the expense of writing clear and maintainable code is not only pointless, but also detrimental.

    As a bonus, I should mention that using a reference might actually make the function slower.

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

Sidebar

Related Questions

I can't understend why variable s_return dont work $('.codeinput').change(function() { var s_return=; var to_check=this.value
Example: var arr_1:Array = new Array(); arr_1.push({sdate:2010-02-02,status:New}); arr_1.push({sdate:2010-02-03,status:New}); arr_1.push({sdate:2010-02-04,status:New}); arr_1.push({sdate:2010-02-05,status:New}); How can i change
I understand that I can change a sql table using the follow sp: EXEC
this one a code for read two character behind text KD-R411ED var code =
i have two jquery(can increase), one of them masterpage and the other is in
You can change the connection string at run-time like this. You make the connection
I can change SQL at runtime. Can I do the same with LINQ ?
I can change the irb prompt mode with irb --prompt prompt-mode I can see
Can someone tell me how i can change the .xml file that a flash
I understand how I can change the dns settings for my domains by editing

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.