Solved

variable named variable ($$)

Posted on 2004-09-14
8
178 Views
Last Modified: 2006-11-17
here is what i am trying to do, which i thought i have done before but it does not seem to work.

$Foo= 'Bar';

$$Foo = '1';

if ($Bar == '1'){
//DO SOMETHING
} elseif ($OtherName == '1'){
//DO SOMETHING ELSE
}

basically i need to be able to set the name of a variable, then later on give it a value and test for the value. i will need the variables name to be able to be set to anything i want, because i will have a bunch of different if's at the end.

thank you
0
Comment
Question by:qwertq
8 Comments
 
LVL 14

Expert Comment

by:dfu23
Comment Utility
why not use an associative array/hash
0
 

Author Comment

by:qwertq
Comment Utility
could you be more specific?
0
 
LVL 33

Expert Comment

by:snoyes_jw
Comment Utility
Your code seems to function as written.  Try
echo $Bar;
to see if it got set correctly.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Accepted Solution

by:
dfu23 earned 50 total points
Comment Utility
http://us2.php.net/types.array

$Foo = array("Bar" => "1", "OtherName" => 1);

if ($Foo["Bar"] == "1") {
    // Do Something
}
elseif ($Foo["OtherName"] == "1") {
    // Do Something Else
}
0
 
LVL 8

Expert Comment

by:hendridm
Comment Utility
Your code seems to work for me in 4.3.8:
<?
$foo = "bar";
$$foo = 1;

echo $bar; //Displays '1'
?>

Not sure which PHP you're using, but maybe this would work for you:
<?
$foo = "bar";
eval("\$$foo = 1;");

echo $bar; //Displays '1'
?>
0
 

Expert Comment

by:mary_BE
Comment Utility
You might wanna try this :
${$Foo} = '1';
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now