FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Variable variables?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Variable variables? [message #182269 is a reply to message #182261] Fri, 26 July 2013 20:17 Go to previous messageGo to previous message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma:
Senior Member
Scott Johnson wrote:

> if $parent is 'Women' I need to reference the array 'Women'

The concept of variable variables is quite simple:

$women = array(/* ... */);
$parent = 'women';
$$parent == $women; // TRUE

I don't consider this concept "sucky" per se, but I would recommend to
avoid overusing it, as it easily leads to unwieldy code due to its
dynamic nature:

$a = 1;
$b = 'a';
$c = 'b';
$$$c; // 1
$c = 'a';
$$$c; // Notice: Undefined variable: 1

However, sometimes it comes in quite handy, for instance:

foreach (array('foo', 'bar', 'baz') as $var) {
$$var = $_POST[$var];
}

--
Christoph M. Becker
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Query with PHP and MySql
Next Topic: Dynamically changing links in a web page menu when a link is clicked
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 09 23:41:41 GMT 2024

Total time taken to generate the page: 0.05225 seconds