Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


variable named variable ($$)

Posted on 2004-09-14
Medium Priority
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'){
} elseif ($OtherName == '1'){

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
Question by:qwertq
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 14

Expert Comment

ID: 12055917
why not use an associative array/hash

Author Comment

ID: 12055951
could you be more specific?
LVL 33

Expert Comment

ID: 12056420
Your code seems to function as written.  Try
echo $Bar;
to see if it got set correctly.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 14

Accepted Solution

dfu23 earned 200 total points
ID: 12057068

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

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

Expert Comment

ID: 12057559
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'

Expert Comment

ID: 12082169
You might wanna try this :
${$Foo} = '1';

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …
Suggested Courses

636 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