Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #10224

Re: Variable variables?

Message-ID <1769805.qdRcx4SL1G@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-07-27 09:37 +0200
Subject Re: Variable variables?
Newsgroups comp.lang.php
References <ksso5i$u2t$1@dont-email.me> <51f2d91e$0$6553$9b4e6d93@newsspool4.arcor-online.net>

Show all headers | View raw


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).

Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Variable variables? Scott Johnson <noonehome@chalupasworld.com> - 2013-07-25 19:56 -0700
  Re: Variable variables? Jerry Stuckle <jstucklex@attglobal.net> - 2013-07-26 06:56 -0400
    Re: Variable variables? Scott Johnson <noonehome@chalupasworld.com> - 2013-07-26 12:39 -0700
      Re: Variable variables? Jerry Stuckle <jstucklex@attglobal.net> - 2013-07-27 09:48 -0400
        Re: Variable variables? Tim Streater <timstreater@greenbee.net> - 2013-07-27 15:45 +0100
          Re: Variable variables? The Natural Philosopher <tnp@invalid.invalid> - 2013-07-27 15:46 +0100
            Re: Variable variables? Tim Streater <timstreater@greenbee.net> - 2013-07-27 15:53 +0100
  Re: Variable variables? Luuk <luuk@invalid.lan> - 2013-07-26 22:04 +0200
    Re: Variable variables? Scott Johnson <noonehome@chalupasworld.com> - 2013-07-26 20:27 -0700
  Re: Variable variables? Christoph Michael Becker <cmbecker69@arcor.de> - 2013-07-26 22:17 +0200
    Re: Variable variables? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-07-27 09:37 +0200

csiph-web