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 #182271 is a reply to message #182269] Sat, 27 July 2013 07:37 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
Christoph Michael Becker wrote:

> 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:

ACK.

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

Thanks, I did not know that you could have variables named “1” this way :)

$ php -r '$a = 1; $b = "a"; $c = "b"; $$$c = 42; echo $1 . PHP_EOL;'
PHP Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting
variable (T_VARIABLE) or '$' in Command line code on line 1

$ php -r '$a = 1; $b = "a"; $c = "b"; $$$c = 42; echo $$$c . PHP_EOL;'
42

Another interesting fact that I have learned (or have been reminded of) in
the ZCE (PHP 5.3) preparation class recently:

$ php -r '$a = 42; $b = "a";

echo $$b . PHP_EOL;
echo "$$b" . PHP_EOL;
echo "${$b}" . PHP_EOL;
echo "{$$b}" . PHP_EOL;'
42
$a
42
42


PointedEars
--
When all you know is jQuery, every problem looks $(olvable).
[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: Wed Dec 11 23:58:47 GMT 2024

Total time taken to generate the page: 0.03969 seconds