Forum Moderators: coopster

Message Too Old, No Replies

symbol & & & &

         

wallpaperzone

3:03 am on Aug 5, 2004 (gmt 0)

10+ Year Member



For PHP.
what is the meaning on adding the symbol & to the code?

function &db_fetch( )
{
}

$obj =& new $className();

Birdman

5:12 am on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not very adept in this area but it starts here:

PHP References Explained [us3.php.net]

I'll be reaading that as well ;)

Birdman

TheBlueEyz

10:46 am on Aug 5, 2004 (gmt 0)

10+ Year Member



The & symbol, when used in front of a function call or a variable, indicates what is called "Pass by reference."

It's SIMILAR to (NOT THE SAME AS) pointers in the C programming language.

Instead of passing the actual value of a function or a variable, you pass a reference to its space in memory. That way, the reference remains updated with the actual value while it changes, instead of having to update it later on.

These are the acceptable forms of pass by reference:

function &db_fetch( )
{
}

$obj =& new $className();

This is an unacceptable one and is being discouraged:

$myval = myfunc(&$arg);

The first two are ok because they're very useful and necessary; the third one is confusing and makes it difficult to read and maintain code.

ergophobe

2:24 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



And note that for class objects, we will be able to quit doing this in PHP5 as the default will be pass by reference for objects.