5

I have a function that populates an array that was created before the function is launched. To make the population work, I used 'global' in my function. Everything is working fine with the here below situation:

$parameter = 'something'; 
$listOne = array();
my_function($parameter);
function my_function($para) {
 global $listeOne;
 ...some code 
 $listeOne[0] = 'john';
 $listeOne[1] = 'lugano';
}

What I would like is to pass the array that is supposed to be used in the function when calling the function. The idea would be to do something like this:

$parameter = 'something'; 
$listOne = array();
$listTwo = array();
my_function($listOne, $parameter);
...some code
my_function($listTwo, $parameter);
function my_function($list, $para) {
 ...some code
 $list[0] = 'john';
 $list[1] = 'lugano';
}

In addition according to what I read, using global is maybe not the best thing to do... I saw some people using the & sign somewhere and saying it is better. But I don't get it and not find information about that 'method'... Hope I am clear. Thank you in advance for your replies. Cheers. Marc

asked Apr 18, 2012 at 10:39

4 Answers 4

10

It's called referencing:

$listOne = array();
function my_function(&$list, $para) {
 ...some code
 $list[0] = 'john';
 $list[1] = 'lugano';
}
my_function($listOne, $parameter);
print_r($listOne); // array (0 => 'john', 1 => 'lugano');

Now the omitted array will be changed.

answered Apr 18, 2012 at 10:41
Sign up to request clarification or add additional context in comments.

4 Comments

Helo Dan. Thanks for trying to help. How is this & sign called in that situation and how does it work? And when calling the function do I have to put the & sign as well?
@Marc: why not read the manual, he left a link to direct there
In my message referencing is linked, anyway the exact term for this occasion here is Passing by reference
Thak you Dan. I am on my way to read what is behind that link. Thank you for your help...
2

Maybe you need some thing like "parameters by reference" http://www.php.net/manual/en/functions.arguments.php

answered Apr 18, 2012 at 10:44

Comments

1

Using & means to pass by reference.

For example:

$x = '123';
function changeX(&$data){
 $data = '1';
}
echo $x; //x should print out as 1.

In your case, you could use:

$parameter = 'something'; 
$listOne = array(); 
$listTwo = array(); 
my_function($listOne, $parameter); 
my_function($listTwo, $parameter); 
function my_function(&$list, $para) { 
 $list[0] = 'john'; 
 $list[1] = 'lugano'; 
} 
answered Apr 18, 2012 at 10:43

Comments

1

you can write your function like this:

$listOne = array();
my_function($list = array())
{
array_push($list, 'john');
array_push($list, 'lugano');
return $list;
} 
$listOne = my_function($listOne);
print_r($listOne);
answered Apr 18, 2012 at 10:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.