tags:

views:

138

answers:

1

Am stuck here!

$part_one = "abc";
$v = "one";

echo $part_???; // should output "abc"

How do I modify ??? to reference $v?

Thanks!

+2  A: 

You need variable variables:

echo ${'part_'.$v};
// or
$var = 'part_'.$v;
echo $$var;
Gumbo
Bingo - thanks!
RC
Why would this ever be useful? Just curious...
Ed Swangren
Since variable can also be used to call functions (`${'part_'.$v}()` would call the `abc` function), you can use this to compact algorithms where only the name of the function may vary. Take the `imagecreatefrom…` functions for example: Just get the image type, append it to "imagecreatefrom" and call that function: `$func = "imagecreatefrom".$type; $func($filename);`.
Gumbo