...

View Full Version : Simple Function Variable Question



benjam1nrk
02-17-2010, 06:01 PM
When a variable is passed to a function in the format:

function myfunction(&$myvariable1)
What is the purpose of the ampersand before the variable?

Fou-Lu
02-17-2010, 06:06 PM
By reference. It means any changes you perform on the variable within the function will be reflected outside of the function. The default handling of function variables is by copy with the exception of objects, which are always by ref (5.x+).
The reverse also applies, if you declare a function as a reference:


function &myfunction()

The result stored will be updated if the data changes within the function at a later time. This is handy when doing things like last minute dumps from a template system, but are not seen often otherwise.


btw for further reading: http://php.net/manual/en/language.references.php

benjam1nrk
02-17-2010, 06:09 PM
By reference. It means any changes you perform on the variable within the function will be reflected outside of the function. The default handling of function variables is by copy with the exception of objects, which are always by ref (5.x+).
The reverse also applies, if you declare a function as a reference:


function &myfunction()

The result stored will be updated if the data changes within the function at a later time. This is handy when doing things like last minute dumps from a template system, but are not seen often otherwise.

Thank you for the quick response!

This is exactly what I was hoping, and what I figured. Rather difficult to google without knowing the proper terminology. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum