Function and the operator &

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Function and the operator &

Post by Draco_03 »

When you crate a function, i know it takes a & in case like this one :

Code: Select all

function addFive( &$num ) {
	$num += 5;
}
$orignum = 10;
addFive( $orignum );
print( $orignum );
?>
but why..i just need someone to explain the use of & in a function befor a variable name..

thx :)
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

It means you are passing the data by refrence, and not making a copy of it. in your example, print( $orignum ); would output 15. This allows the function access to data outside of it's normal scope
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

thx guys :)
Post Reply