Comments on: PHP & Ampersand: Passing by Reference https://phpreferencebook.com/samples/php-pass-by-reference/ PHP Reference: Beginner to Intermediate PHP5 Wed, 07 Sep 2011 23:05:47 +0000 hourly 1 https://wordpress.org/?v=4.9.13 By: Miha https://phpreferencebook.com/samples/php-pass-by-reference/comment-page-1/#comment-1285 Sat, 09 Jan 2010 16:40:35 +0000 https://phpreferencebook.com/?p=165#comment-1285 Brandon is right! As of PHP 5.3.0, you will get a warning saying that “call-time pass-by-reference” is deprecated when you use & in $foo->setBar(“xxx”).

Besides objects are passed by reference by default.

Read here:http://www.webeks.net/programming/php/ampersand-operator-used-for-assigning-reference.html

]]>
By: Brandon https://phpreferencebook.com/samples/php-pass-by-reference/comment-page-1/#comment-1101 Thu, 03 Dec 2009 14:48:31 +0000 https://phpreferencebook.com/?p=165#comment-1101 As Scott said, passing a reference in from the call will work, but it has been deprecated, so the following form is preferred:

function add(&$var){ // The & is before the argument $var
$var++;
}
]]>
By: Scott https://phpreferencebook.com/samples/php-pass-by-reference/comment-page-1/#comment-826 Sat, 05 Sep 2009 21:13:00 +0000 https://phpreferencebook.com/?p=165#comment-826 In addition to prepending a function parameter with an ampersand, the ampersand may also be prepended to the variable in the calling statement, and the same effect will be achieved.

Not sure if this is a language quirk, or it was intended that way, but it works!

ex:

   var1 = 0;
   var2 = 100;
 
   // changes var 1 to 5!   
   callMe(&var1, var2);
 
   function callMe(param1, param2) {
      param1 = 5;
   }
]]>