Home > other >  Merge two variable values to get a new variable name (not value)
Merge two variable values to get a new variable name (not value)

Time:02-10

is possible in php to merge the first variable value with the second variable value to get a new variable name (not value).

$1 = test
$2 = 5
//do magic results in new variable name eg. $($1 $2)
$test5 = true

So if $2 is dynamic we get a dynamic variable name, not a dynamic value. The value "true" is just an example value. I don't know If it is possible, because I'm completely new to PHP and found nothing by searching.

CodePudding user response:

Yes. Use {} after the $ sign to interpret the inner content as the variable name.

$a = "test";
$b = 5;

${$a . $b} = "new value";
echo "test5 = " . $test5; // prints test5 = new value 

See manual page for more details.

  •  Tags:  
  • Related