Well, after a couple month hiatus, I guess it is time to throw up another post. I was up working on a project, and came across a completely valid usage of variable variables, and felt it might be worth making a post, because it is kind of fun.
At first glance, “variable variables” seemed pretty pointless to me. Surely they had a place somewhere, but I guess that’s just not how I write code. The best use I could think of was a pointless, but stupid-fun script, like this:
$a = "hello world"; $b = "a"; $c = "b"; $d = "c"; $e = "d"; $f = $$$$$e;
Perhaps, for this example I’m about to use, there is a better way of accomplishing the task (I can think of other methods, but can argue which is “better”). My task was to feed a function the numerical representation of a month, and spit back the textual representation, adhering to preferred formatting. So I wrote a function that has a couple hard-coded lookup arrays, named after the $format parameter. The function is pretty well self-explanatory:
function getMonth($i, $format = 'F') {
$F = array(
1 => "January",
2 => "February",
3 => "March",
4 => "April",
5 => "May",
6 => "June",
7 => "July",
8 => "August",
9 => "September",
10 => "October",
11 => "November",
12 => "December",
);
$M = array(
1 => "Jan",
2 => "Feb",
3 => "Mar",
4 => "Apr",
5 => "May",
6 => "Jun",
7 => "Jul",
8 => "Aug",
9 => "Sep",
10 => "Oct",
11 => "Nov",
12 => "Dec",
);
$format = strtoupper($format);
if($format != 'F' && $format != 'M') {
$format = 'F';
}
if($i > 12 || $i < 1) {
return false;
}
$source = $$format;
return $source[$i];
}

