Home > Enterprise >  Concatenate variable with $$ in PHP
Concatenate variable with $$ in PHP

Time:10-15

I have this declaration:

$arr = 'Banana';

How can I use Banana in my new variable ?

$fruitBanana = 'Content';

What I've tried:

$fruit$$arr = 'Content';
echo $fruit$$arr; // Should echo 'Content'

But it was not working.

CodePudding user response:

These are variable variables, not concatenation.

The correct code would be:

$arr = 'Banana';
$fruitBanana = 'Content'; // OR $fruit{$arr} = 'Content';

echo ${"fruit{$arr}"};
// 'Content'

Reminder, concatenation is done via the . operator.

  •  Tags:  
  • php
  • Related