I’m creating an array outside of my PHP function and calling global inside the function but my function fails to see the array. The function is also called recursively. Any idea what I’m doing wrong?
$node_types = array( 1 => 'testproject',
2 => 'testsuite',
3 => 'testcase');
function get_subtree($node_id,&$node_list) {
global $node_types;
foreach ($node_types as $key) {
echo("node_types: " . $key . "<BR>");
}
....
get_subtree($node_id,$node_list);
}
My error:
Notice: Undefined variable: node_types in ...
Thanks
Declaring an array within a function will create a new array each time the function is used and the scope of the array is only as long as the function. The Global array must be declared outside of functions. So in this case should be placed before at he top of the example.