You can declare $ arr global as a function body:
$arr = [1, 2, 6]; echo test(2); function test($a) { global $arr; return $arr[1] - $a; }
But this is not considered good practice.
If you have PHP at least version 7, you can also declare a constant with an array structure (constants have a global scope) :
const ARR = [1, 2, 6]; echo test(2); function test($a) { return ARR[1] - $a; }
If the version with constants is not suitable, you can form an array for transfer to the function by adding to the first element the value you need to work with in the function body. And in the body of the function, extract this first element from the array:
$arr = [1, 2, 6]; array_unshift($arr, 2); echo test($arr); function test($arr) { $a = array_shift($arr); return $arr[1] - $a; }
Well, another option is to create another user-defined function that will return an array, and call this function in the body of the first function:
echo test(2); function test($a) { return get_array()[1] - $a; } function get_array() { return [1, 2, 6]; }