Latest

Archive

Community news

C++

Communities and Content

Databases

Editorials

Emacs

General

HTML

Java

Notices

PHP

XML

Apache

C++

Database

General

HTML

Java

Javascript

Linux

Object oriented programming

Open source

Perl

PHP

Python

Ruby

SOAP

XML

Suggest a link

Advertise on zez

Contribute

Contact us

About zez


PHP References explained



Functions

Using references in functions is done by adding an & before the parameter.

function inc( &$var )
{
  ++$var;
}
$a = 1;
inc( $a ); // $a is now 2


Unfortunately you cannot do this

function assign( &$a )
{
  $GLOBALS["a"] =& $a;
}
function retval()
{
  return "abc";
}
$c = "a";
$d = "b";
assign( 4 ); // Constant cannot be passed
assign( retval() ); // No reference in return value
assign( $c == $d ); // Expression cannot be passed


Also returning a reference is done by adding an & before the function name and by referring to the function call.

function &createRef()
{
  $a = "text";
  return $a;
}
$b =& createRef(); // $b refers to the data of $a in the function


What happens if you want a function to change the reference of a variable but not the data itself. As you might recall it's not possible to create a reference to a reference.

The clue is to use arrays, create an array and let a specific key refer to your data then pass the array to function which takes the parameter as a reference.

function refChange( &$b )
{
  $c = "text";
  $b["ref"] =& $c; // Changes the reference for "ref" key 
}
$a = "test";
$arr = array();
$arr["ref"] =& $a;
refChange( $arr ); // Now $arr["ref"] refers to "text".


<< Previous page | 1 | 2 | < 3 > | 4 | 5 | Next page >> | Printer-friendly page |

Comment List


Topic: Author:
Time:
Definition of large? Coco Loco 12.04.2003 13:17

On page 4, under "When Should References be Used", you say "When you're passing on large text, array or object structures to functions".

What do you consider "large"? It seems to be pretty subjective. Is an array with 100 elements considered "large"? 500? 1000? How about an array with 1 big element?

Thanks for the articles BTW, not many high-level PHP articles around. :)


Misleading Lance Lovette 04.03.2002 19:53

Some of the statements in this article are misleading if you are using PHP 4. You should read the article "PHP 4: Reference Counting and Aliasing" for more perspective - http://www.zend.com/zend/art/ref-count.php. According to that article the term "reference" is more appropriately described as "alias", there is no implicit deep copying on variable assignment and there isn't always a performance gain when passing references as function parameters.


   RE: Misleading Jan Borsodi 11.03.2002 16:08

> Some of the statements in this article are misleading if you
> are using PHP 4. You should read the article "PHP 4:
> Reference Counting and Aliasing" for more perspective -
> http://www.zend.com/zend/art/ref-count.php. According to
> that article the term "reference" is more
> appropriately described as "alias",

Using "alias" can be misleading as well, consider the last example on page 1.
By using "alias" one might believe that $b and $c is a different name for the variable $a and that when $a is unset so are $b and $c, which is not the case.

> there is no
> implicit deep copying on variable assignment and there isn't

Maybe my choice of words were bad but there's still a "copy" going on, whether this is smart and does internal referencing or just copies all the bits is not the point. The point is understanding what references do.

> always a performance gain when passing references as
> function parameters.

I didn't say there always is a performance gain, but that it might.
Allthough PHP 4 has reference counting I have successfully optimized my code by using references several places.
I've also seen the opposite where references would make the code slower.




Forgot your password?

Register a new user

Results

Polls