Giving my function access to outside variable?

By default, when you are inside a function, you do not have access to the outer variables If you want your function to have access to an outer variable, you have to declare it as global inside the function : function someFuntion(){ global $myArr; $myVal = //some processing here to determine value of $myVal $myArr = $myVal; } For more informations, see Variable scope But note that using global variables is not a good practice : with this, your function is not independant anymore A better idea would be to make your function return the result : function someFuntion(){ $myArr = array(); // At first, you have an empty array $myVal = //some processing here to determine value of $myVal $myArr = $myVal; // Put that $myVal into the array return $myArr; } And call the function like this : $result = someFunction() Your function could also take parameters, and even work on a parameter passed by reference : function someFuntion(array & $myArr){ $myVal = //some processing here to determine value of $myVal $myArr = $myVal; // Put that $myVal into the array } Then, call the function like this : $myArr = array( ... ); someFunction($myArr); // The function will receive $myArr, and modify it With this : Your function received the external array as a parameter And can modify it, as it's passed by reference And it's better practice than using a global variable : your function is a unit, independant of any external code For more informations about that, you should read the Functions section of the PHP manual, and,, especially, the following sub-sections : Functions arguments Returning values.

By default, when you are inside a function, you do not have access to the outer variables. If you want your function to have access to an outer variable, you have to declare it as global, inside the function : function someFuntion(){ global $myArr; $myVal = //some processing here to determine value of $myVal $myArr = $myVal; } For more informations, see Variable scope. But note that using global variables is not a good practice : with this, your function is not independant anymore.

A better idea would be to make your function return the result : function someFuntion(){ $myArr = array(); // At first, you have an empty array $myVal = //some processing here to determine value of $myVal $myArr = $myVal; // Put that $myVal into the array return $myArr; } And call the function like this : $result = someFunction(); Your function could also take parameters, and even work on a parameter passed by reference : function someFuntion(array & $myArr){ $myVal = //some processing here to determine value of $myVal $myArr = $myVal; // Put that $myVal into the array } Then, call the function like this : $myArr = array( ... ); someFunction($myArr); // The function will receive $myArr, and modify it With this : Your function received the external array as a parameter And can modify it, as it's passed by reference. And it's better practice than using a global variable : your function is a unit, independant of any external code. For more informations about that, you should read the Functions section of the PHP manual, and,, especially, the following sub-sections : Functions arguments Returning values.

– PatrikAkerstrand Mar 27 '10 at 22:58 2 @Machine : quite a good question ^^ (I have, since, edited my answer a couple of time to add more informations ; maybe it was downvoted because not complete enough, at first... It probably has something to do with global, which people don't like...) – Pascal MARTIN Mar 27 '10 at 23:00 3 @Machine Mr Anti-Global @Coronatus has decided that perfectly viable answers are wrong. – Anthony Forloney Mar 27 '10 at 23:01 I read your initial version, it was still very much good enough for a +1 vote, and definitely not worth down-votes. – PatrikAkerstrand Mar 27 '10 at 23:05.

The one and probably not so good way of achieving your goal would using global variables. You could achieve that by adding global $myArr; to the beginning of your function. However note that using global variables is in most cases a bad idea and probably avoidable.

The much better way would be passing your array as an argument to your function: function someFuntion($arr){ $myVal = //some processing here to determine value of $myVal $arr = $myVal; return $arr; } $myArr = someFunction($myArr).

Global $myArr; $myArr = array(); function someFuntion(){ global $myArr; $myVal = //some processing here to determine value of $myVal $myArr = $myVal; } Be forewarned, generally people stick away from globals as it has some downsides. You could try this function someFuntion($myArr){ $myVal = //some processing here to determine value of $myVal $myArr = $myVal; return $myArr; } $myArr = someFunction($myArr); That would make it so you aren't relying on Globals.

(never using globals..) – svens Mar 27 '10 at 23:07.

$myArr = array(); function someFuntion(array $myArr) { $myVal = //some processing here to determine value of $myVal $myArr = $myVal; return $myArr; } $myArr = someFunction($myArr).

1 Stupid downvoting. Of course, this is the only correct answer in the whole thread. – user187291 Mar 27 '10 at 23:25.

By default, when you are inside a function, you do not have access to the outer variables. For more informations, see Variable scope. But note that using global variables is not a good practice : with this, your function is not independant anymore.

And can modify it, as it's passed by reference. And it's better practice than using a global variable : your function is a unit, independant of any external code.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions