Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a variable, which is stored in another one 3

Status
Not open for further replies.

lukelukeluke

Technical User
Dec 23, 2003
117
CH
hi,
I have a question about php variables:
Here are my 3 variables:

$var1 = "hello World";
$var2 = "Hello Earth";
$var3 = "var1";

Now, I would like to output "Hello World" using var3,
and also "Hello Earth" when changing $var3 = "var2";

Someone knows how this is possible?

Webmaster of knowledgebase for IT knowledge.
 
hi chris. Thanks for your fast answer, but thats not what i wanted...
i have these:

$var1 = "hello World";
$var2 = "Hello Earth";
$var3 = "var1";

now, i want to do something with $var3 (but not changing its content) so it gives me the content of var1. my $var3 is set to "var1".
I tryed to see google for this and saw its maybe called pointer. But i didnt see any solution or tutorial for these.

Webmaster of knowledgebase for IT knowledge.
 
Isn't it:
Code:
$var1 = "hello World";
$var2 = "Hello Earth";
$var3 = "var1";

echo $$var3;
It's been a while since I did anything with PHP.
 
If this helps, I have done something like this in the past.
Code:
$var1 = "hello World";
$var2 = "Hello Earth";
$var3 = 1;

echo $var{$var3};

Likewise, I think that you could do (in your example) ${$var3} to echo "hello World"

You can always search for dynamic variables
 
Vragabond has given IMO the best answer.
A variable-variable. A double dollar sign accesses the value of the named variable.
Code:
# The value
$var1 = "Hello world";
# The varname
$var3 = "var1";
#output -> Hello world
# same as the code >echo $var1;
echo $$var3;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top