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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:46:08+00:00 2026-06-15T18:46:08+00:00

I have a function: $query = SELECT * from lol; database_query( $query ); considering

  • 0

I have a function:

$query = "SELECT * from lol";
database_query( $query );

considering the $query will never be changed inside the database_query function, is it good practice to use a pointer to $query so the function doesn’t need to assign more memory to a new iteration of the value passed in?

function database_query( &$query ){
    //do stuff that does not affect $query
}
  • 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-15T18:46:09+00:00Added an answer on June 15, 2026 at 6:46 pm

    This turns out to be a very interesting question, I’ve spent the last hour and a half reading about PHP and how it handles references (thank you Tim Cooper for the links that got me started).

    To answer your question, yes – it’s good practice to use a reference like that when you call a function. By using a reference you will use fewer resources – there is no "copy on write" for a reference variable. Here’s some proof:

    <?php
    function noref_nowrite($var_a){
        echo '<h3>NOT Using a Reference/Not Changing Data</h3>';
        echo '<p>'. xdebug_debug_zval('var_a') .'</p>';
        echo '<p>'. debug_zval_dump($var_a) .'</p>';
        echo '<p>$var_a = '. $var_a .' and $GLOBALS[a] = '. $GLOBALS['a'] .'</p>';
    }
    function noref_write($var_a){
        $var_a++;
        echo '<h3>NOT Using a Reference/Changing Data</h3>';
        echo '<p>'. xdebug_debug_zval('var_a') .'</p>';
        echo '<p>'. debug_zval_dump($var_a) .'</p>';
        echo '<p>$var_a = '. $var_a .' and $GLOBALS[a] = '. $GLOBALS['a'] .'</p>';
    }
    
    function ref_nowrite(&$var_a){
        echo '<h3>Using a Reference/Not Changing Data</h3>';
        echo '<p>'. xdebug_debug_zval('var_a') .'</p>';
        echo '<p>'. debug_zval_dump($var_a) .'</p>';
        echo '<p>$var_a = '. $var_a .' and $GLOBALS[a] = '. $GLOBALS['a'] .'</p>';
    }
    function ref_write(&$var_a){
        $var_a++;
        echo '<h3>Using a Reference/Changing Data</h3>';
        echo '<p>'. xdebug_debug_zval('var_a') .'</p>';
        echo '<p>'. debug_zval_dump($var_a) .'</p>';
        echo '<p>$var_a = '. $var_a .' and $GLOBALS[a] = '. $GLOBALS['a'] .'</p>';
    }
    
    $a = 5;
    noref_nowrite($a);
    noref_write($a);
    ref_nowrite($a);
    ref_write($a);
    ?>
    

    If you copy/paste the above code into a PHP page and execute it you will see this:

    NOT Using a Reference/Not Changing Data
    var_a: (refcount=3, is_ref=0)=5
    long(5) refcount(4)
    
    $var_a = 5 and $GLOBALS[a] = 5
    
    
    NOT Using a Reference/Changing Data
    var_a: (refcount=1, is_ref=0)=6
    long(6) refcount(2)
    $var_a = 6 and $GLOBALS[a] = 5
    
    Using a Reference/Not Changing Data
    var_a: (refcount=3, is_ref=1)=5
    long(5) refcount(1)
    $var_a = 5 and $GLOBALS[a] = 5
    
    Using a Reference/Changing Data
    var_a: (refcount=3, is_ref=1)=6
    long(6) refcount(1)
    $var_a = 6 and $GLOBALS[a] = 6
    

    So what we have here is four basic tests. I create a global variable ($a) and assign it the value of 5.

    When I call the noref_nowrite function we see that XDebug counts 3 references while PHP’s built in function counts 4. Interestingly, PHP optimizes this so internally it’s really like calling the ref_nowrite function because PHP makes $var_a a reference to $GLOBALS[‘a’].

    When I call the noref_write function we see that the refcount drops to 1 (or 2 if you look at PHP’s built-in function). Why? Because this is where the "copy on write" issue takes place. Up until we incremented $var_a PHP was internally using $var_a as a reference to $a but when we changed the value we forced PHP to make a copy of the variable so it could be incremented. So at that point $var_a was no longer a reference to $a, instead it was changed to reference it’s own data.

    The ref_nowrite function shows ambiguous results. Looking at it alone we can’t prove anything. However the ref_write function shows us that XDebug says we’re dealing with a reference variable (is_ref=1) and most importantly we see that after we increment $var_a the value of our global variable $a has also changed – this means $var_a and $GLOBALS[‘a’] are definitely pointing at the same place in memory. Which means changing $var_a did NOT trigger a "copy on write" situation – and it shouldn’t since we’re dealing with a reference.

    Play around with this to convince yourself and here’s some more reading:

    Detecting whether a PHP variable is a reference / referenced (I thought ircmaxell had a well thought out answer)

    https://www.php.net/debug-zval-dump

    XDebug documentation: http://xdebug.org/docs/display

    PHP What References Do: https://www.php.net/manual/en/language.references.whatdo.php

    PHP Reference Counting Basics: https://www.php.net/manual/en/features.gc.refcounting-basics.php

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

Sidebar

Related Questions

I have the following code. public function get_users_theme($uid) { $r = $this->db2->query('SELECT * FROM
Does DISTINCT in a simple query with an aggregate function have any effect? select
I have this query in model: public function Games() { $q = $this->db->select('games.id, games.title,
I have a query regarding the directory returned from Path.GetTempPath() function. It returns C:\Documents
In my Iphone App I have created this query: SELECT * FROM visuel where
This is what i have function GetEventsList(){ $result = mysql_query(SELECT * FROM `events`) or
I have following query select * from table1 if table1 contains text type column/columns
I have this function: function findAllmessageSender(){ $all_from = mysql_query(SELECT DISTINCT `from_id` FROM chat); $names
I have a query: SELECT * FROM categorys LEFT JOIN category_info ON categorys.cat_id=category_info.cat_id WHERE
I have a query select * from test; I have to add UNION after

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.