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.
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 :
<?php
/*In general(the rule can be broken) code is interpreted left to right
top to bottom.
If you want a function to be able to use the values you input,
write the function first. This means the function should be above where
it is requested in the code. Add some parameters($param). Note it does
not need to be called $param, I use $value in the example. This can be
multiple $vars going from left to right i.e($param_1,$param_2), or be an
array(), or a mix. Just remember left to right. Left values must exist
before right values.*/
//Example function here
function foo($value){
return $value[0] + 1;
}
//Optional way to create array
//$value[0] = 0;
$value = array(0);
$limit = 10;
while($value[0] < $limit){
//Request the function here as many times as you want
echo $value[0] = foo($value);
echo "<br>";
}
//Clean up afterwards
unset($value,$limit);
?>