Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending a value of a variable to a variable name? [duplicate]

Tags:

variables

php

Possible Duplicate:
Can I use a generated variable name in PHP?

Am stuck here!

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

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

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

Thanks!

like image 866
RC. Avatar asked Nov 21 '09 08:11

RC.


2 Answers

You need variable variables:

echo ${'part_'.$v};
// or
$var = 'part_'.$v;
echo $$var;
like image 126
Gumbo Avatar answered Oct 05 '22 17:10

Gumbo


$_part$v;? maybe idk. I know you can do this:

$vname="variable";
$$vname="hello";
echo $variable;

//WOULD output "hello"

try this:

$name="_part";
$name=$name . $v;
$$name=$value;
like image 21
Adam F Avatar answered Oct 05 '22 17:10

Adam F