Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am having a basic php problem that I haven't been able to resolve and I would also like to understand WHY!

 $upperValueCB = 10;
 $passNodeMatrixSource = 'CB';

 $topValue= '$upperValue'.$passNodeMatrixSource;

 echo $topValue;

OUTPUT $upperValueCB

but I want OUTPUT as the variable's value 10.

How can I make PHP read the $dollar-phrase as a variable and not a string?

share|improve this question
2  
$topValue = $upperValueCB . $passNodeMatrixSource;? Why the quotes? –  Quasdunk Apr 6 '13 at 14:31
    
$upperValue isn-t a variable it's a string –  Gamemorize Apr 6 '13 at 14:32
    
Ok... I guess I don't quite understand the problem then... –  Quasdunk Apr 6 '13 at 14:35
    
show the exact output you want to get –  yefrem Apr 6 '13 at 14:37
    
@yefrem, I said I wanted the variable-s value 10 –  Gamemorize Apr 6 '13 at 14:38

4 Answers 4

up vote 4 down vote accepted
$varName = 'upperValue' . $passNodeMatrixSource;
$topValue = $$varName;

http://php.net/manual/en/language.variables.variable.php

share|improve this answer
    
+1 i like your thinking! But I still don-t understand why! –  Gamemorize Apr 6 '13 at 14:36
2  
    
i had even -tried- to read that page! Thanks again! –  Gamemorize Apr 6 '13 at 14:42
    
Chosen for the link! Can you put it in the answer too!! –  Gamemorize Apr 6 '13 at 14:48

This little example might illustrate what you are about to do:

<?php

$a = 'b';
$b = 10;

echo ${$a}; // will output 10

So you will have to change your example to:

$upperValueCB = 10;
$passNodeMatrixSource = 'CB';

$topValue= 'upperValue'.$passNodeMatrixSource;

echo ${$topValue}; // will output 10
share|improve this answer
    
plus one, but your example is wrong / you don't use the $b –  Gamemorize Apr 6 '13 at 14:40
    
I don't think so. Have you tested it? it outputs 10 because the variable names $b or $upperValueCB will being generated dynamically –  hek2mgl Apr 6 '13 at 14:41
    
Sorry, my bad you are right! Thanks again! –  Gamemorize Apr 6 '13 at 14:43
1  
Cheers again, gave three plus ones but gave the correct to @alucardtheripper for the php manual link and because he only HAD 1 point AND was first correct too I think! –  Gamemorize Apr 6 '13 at 14:51
1  
No problem. Also note that the syntax of his answer is shorter than my. Programmers are lazy. isn't it ? ;) –  hek2mgl Apr 6 '13 at 14:57

You can do it this way :

$upperValueCB = 10;
$passNodeMatrixSource = 'CB';

$topValue= ${'upperValue'.$passNodeMatrixSource};

echo $topValue;

Because upperValueCB is the var name, if you want PHP to understand which var to use, you have to give the var name.
For that, you can use static way like $upperValueCB
or using a string like this : ${'upperValueCB'}
or using a third var containing the var name $var = 'upperValueCB'; $$var;

share|improve this answer
    
Thanks +1, thought the problem was parsing '' not variable naming –  Gamemorize Apr 6 '13 at 14:46
    
Thank's for that, but I think the problem was well in the method used to call the var correctly. ;) –  JoDev Apr 6 '13 at 14:52
 $upperValueCB = 10;
 $passNodeMatrixSource = 'CB';

 $topValue= 'upperValue'.$passNodeMatrixSource;

 echo ${$topValue};
share|improve this answer
    
$upperValue IS NOT A VARIABLE!!! –  Gamemorize Apr 6 '13 at 14:36
    
This will print "10CB" instead of "10" –  JoDev Apr 6 '13 at 14:43
    
sorry my mistake wrote in wrong. corrected. Thanks –  Sabari Apr 6 '13 at 18:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.