I don't have much PHP experience and I want to know how to best create a recursive function in PHP. Take this example
$config = array(
"MYSQL" => array(
"SERVER" => "localhost",
"USERNAME" => "root",
"PASSWORD" => ""
),
"FOO" => "bar"
);
// Make the config global
config_parse(false, $config);
function config_parse($index, $value)
{
if(is_array($value))
{
foreach($value as $i => $e)
{
config_parse(($index ? $index . "_" : "") . $i, $e);
}
}
else
{
define($index, $value);
}
}
It does what I want it to do, but I feel that I'm "hacking"/writing bad code when I initialize the recursive function in this way (passing false as the initial index and checking for it in the function)
A way to work around this could be to reverse the order of the input parameters, which means that the index value wouldn't be accessed when an array was passed as value.
Another way could be to split the recursive function into initiation function and callback function.
What I want to know is what's the "best practice", preferably based on my example.
Thanks for replies