For starters, I know that variable variables don't work in functions (which is really a shame), but I thought I read somewhere that you could use the same concept but replace it with an array. So, I wrote up some code that should work just fine, but it seems like it's hitting the same problem as variable variables - once they're in a function it just doesn't work at all, which is a shame because I can save myself hundreds of lines of code if I can get this to work.
So here's the deal: I have a function return a string. Then, I pass that into another function and it's supposed to plant that returned string into an array to get a result. This snippet of code is just a replica of the real thing, so it's not meant to be very efficient on it's own.
<?php
$name = array();
$name['j'] = "John";
$name['d'] = "Dave";
$name['w'] = "Will";
function select_person()
{
$select = rand(1,3);
if ($select == 1)
{
return j;
}
if ($select == 2)
{
return d;
}
if ($select == 3)
{
return w;
}
} // End Function
// SAVE RESULT AS A VARIABLE AND PASS IT INTO ANOTHER FUNCTION LATER
$sp = select_player();
function display_name($var)
{
echo $name["$var"];
}
// PASS $SP INTO THIS FUNCTION, SO IT'S THE SAME AS PASSING J, D or W RATHER THAN PASSING THE VARIABLE. IF YOU ECHO WHAT'S PASSED IN, IT ECHOES PROPERLY.
display_name("$sp");
?>
Please let me know if there's something I can do, because if this cannot be done at all then I will be VERY pissed off.
(By the way, this code works just fine if you don't run it within a function.)