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



Globals

The global syntax is actually a reference too, it creates a reference to a global variable.

function a()
{
  global $glob1;
  $glob2 =& $GLOBALS["glob1"];
}


Now $glob1 and $glob2 refer to the same data. The limitations of using the global syntax is that you can't change what the global variable is referring to you can just change the data it's referring. To achieve changing the reference for the global variable you must do.

function b()
{
  $a = "test";
  $GLOBALS["glob1"] =& $a; // Now the global variable $glob1 refers to contents of $a
}


Iterating

As mentioned above PHPs array iterator structures and functions such as foreach and each does not handle references very well, in-fact they create a copy of each element they iterate over. For instance this will not work:

$ref_arr = array();
foreach ( $arr as $item )
{
  $ref_arr[] =& $item;
}


When the foreach is done the $ref_arr will contain references to the last element in the $arr array.
The reason is that when you create the reference you refer to the variable $item which will contain the last item after foreach is done.

There are two ways to iterate arrays correctly, the first only works with normal arrays while the second works with all types of arrays. Method 1 involves using a for loop.

for ( $i = 0; $i < count( $arr ); ++$i )
{
  $item =& $arr[$i];
}


While method 2 uses the key and next functions.

reset( $arr ); // Restart the internal pointer
while ( ( $key = key( $arr ) ) !== null ) // fetch the current key, or null if at end
{
  $item =& $arr[$key]; // Create reference to element
  next( $arr ); // Advance to next key
}


The second method is similar to the first but now the key is fetched from the array instead of assuming a numerical index.


<< 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