So I am writing this php function which reads a csv file and assigns every column to a variable. In the end it needs to calculate the mean of a row. I would like to pass the row (from which the mean has to calculated) in the parameters.

My question is how do I pass a variable name (including the dollar sign) of a variable of the function?

I've tried several things like:

  • function("$number") //this just says the variable is undefined, which is true

  • Same as above, but without the quotes, does the same thing of course...

  • function("\$number")
Question author J-doe | Source



Not entirely sure what you're trying to do, but it sounds something like variable variables. So, if you have a variable $foo you would pass it like this: myfunc('foo'); Then myfunc might look something like this:

myfunc($var) {  global $$var;  // do stuff with $$var}

You'd probably be better off just loading the columns into an array and looping over that in the function.

Answer author Alanlittle