general function question

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
mad_phpq
Forum Commoner
Posts: 85
Joined: Fri Apr 27, 2007 5:53 am

general function question

Post by mad_phpq »

why does this function have an ampersand at the beginning of its name?

Code: Select all

function &varSession($name)
Last edited by mad_phpq on Wed Oct 10, 2007 10:54 am, edited 1 time in total.
mad_phpq
Forum Commoner
Posts: 85
Joined: Fri Apr 27, 2007 5:53 am

Post by mad_phpq »

i've found an answer, but i have another question.

The ampersand return the reference to the value in the function rather than the value itself. Why would it be used in this code below.

Code: Select all

function &varSession($name)
{
  global $sessionPrefix;
  return unmagic_quote_gpc($_SESSION[$sessionPrefix.$name]);
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Probably because the function being called internally returns a reference.
mad_phpq
Forum Commoner
Posts: 85
Joined: Fri Apr 27, 2007 5:53 am

Post by mad_phpq »

feyd wrote:Probably because the function being called internally returns a reference.
whats the benifit?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

References have been the subject of many a coder's nightmares. My suggestion would be to set up a PHP4 and PHP5 environment locally then do some of your own reference tests. This is pretty much the best way to get a hold of references and their uses/benefits/drawbacks to you.
Post Reply