1
$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!

2
  • 5
    Although it can be done (see people's answers), it's akin to an eval() and not really good practice. Find other ways to store and access the value. e.g. $service[3]["step"]["person"] Commented Aug 31, 2012 at 10:11
  • 1
    It's called Variable variables, see docs.php.net/language.variables.variable . But it's usually easier done/more flexible with arrays. In your case maybe something like $services[3]['steps'][1]['person']. Commented Aug 31, 2012 at 10:22

3 Answers 3

5
echo ${$service."_step1_person"};
Sign up to request clarification or add additional context in comments.

Comments

2

Variable variable:

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

1 Comment

replace $$variable with $$variableName . . this will give the exact output :)
0

Try this:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.