Ham
Ham

Reputation: 814

constructing name of php variable

$service3_step1_person = ‘999’;    
echo $service3_step1_person;    //999

and then...

$service = "service3";    
echo "$".$service."_step1_person;  //$service3_step1_person

it doesn't give me 999, just give me a string of $service3_step1_person.

How can I make the string become a new variable name? Thanks!

Upvotes: 1

Views: 166

Answers (3)

lisachenko
lisachenko

Reputation: 6092

Variable variable:

$service3_step1_person = ‘999’;
$service = "service3";
$variableName = "{$service}_step1_person";  
echo $$variableName; // 999

Upvotes: 2

Ashwini Agarwal
Ashwini Agarwal

Reputation: 4858

echo ${$service."_step1_person"};

Upvotes: 5

Vinay
Vinay

Reputation: 2594

Try this:

 $service = "service3";
 $temp = "_step1_person";
 echo ${$service.$temp};

Upvotes: 0

Related Questions