Page 1 of 1

array and function

Posted: Sun Jun 26, 2005 6:35 pm
by nincha
how would u send an array into a function??

Posted: Sun Jun 26, 2005 6:48 pm
by neophyte
The same way as any variable:

Code: Select all

$var = 1;

function example(&$var){ // the magic is the "&";
  $foo = 2;
  $foobar = $foo+$var;
   return $foobar;
}

$var = example($var);

echo $var; //Should echo 3
For more examples visit the reference manual

http://us4.php.net/references

You can also change a variable to global scope by declaring variables outside of the function global.

Code: Select all

global $var, $var2;
Include that declaration within the function.

Posted: Mon Jun 27, 2005 3:08 am
by Chris Corbyn
Well actually the "&" is only required if you want to pass the array by-reference which jus plain confuses some people :P

Passing an array to a function is just the same as passing anything else ;)

Posted: Thu Jun 30, 2005 11:46 pm
by nincha
how do i pass an entire array into a function?

Posted: Fri Jul 01, 2005 2:52 am
by timvw
RTFM, Chapter 17 Functions, Section: Function arguments.

Posted: Fri Jul 01, 2005 4:39 am
by harrisonad
The replies you got were ok. so listen.
To explain elementarily...

Code: Select all

function eatAll($aray){ // or &$aray if you want to edit the array itself
// do whatever you want to the received array
// bite, chew and swallow...
}

$fruits = array('apple','banana','grapes');
eatAll($fruits);
:roll: yeah, that's kid stuff

Posted: Fri Jul 01, 2005 5:46 am
by harsha
style/syntax wise php look like C or C++ (I am talking about fucntions)
But concept wise php is php no changes :wink: