?
Solved

variable named variable ($$)

Posted on 2004-09-14
8
Medium Priority
?
218 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
[X]
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
8 Comments
 
LVL 14

Expert Comment

by:dfu23
ID: 12055917
why not use an associative array/hash
0
 

Author Comment

by:qwertq
ID: 12055951
could you be more specific?
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 12056420
Your code seems to function as written.  Try
echo $Bar;
to see if it got set correctly.
0
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
LVL 14

Accepted Solution

by:
dfu23 earned 200 total points
ID: 12057068
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
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'
?>
0
 

Expert Comment

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

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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

762 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