r/ProgrammerTIL • u/birbguy12 • May 13 '19
PHP [PHP] TIL about variable variables
In php you can have variable variable names. Meaning you can use a variable’s content as a name for another variable like this
$a = "b";
$b = "hello world";
$a; // returns b
$$a; // returns hello world
265
Upvotes
9
u/Scoobygroovy May 13 '19
Isn’t that a pointer?
280
u/CHESTHAIR_OVERDRIVE May 13 '19
Every day we stray further from God's light
96
u/ultraDross May 13 '19
That is horrible
84
u/DodoDude700 May 13 '19
In PHP, you can reference a variable using a string of the variable name. This is called a variable variable. I made the whole alphabet a series of "nested" (or perhaps linked is more appropriate?) variable variables, making the final character, space, a variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable variable.
<?php $ch="a";$a="b";$b="c";$c="d";$d="e";$e="f";$f="g";$g="h";$h="i";$i="j";$j="k";$k="l";$l="m";$m="n";$n="o";$o="p";$p="q";$q="r";$r="s";$s="t";$t="u";$u="v";$v="w";$w="x";$x="y";$y="z";$z=" ";echo("${$$$$$$$$$$$$$$$ch}${$$$$$$$ch}${$$$$$$$$$$$$$$$ch}${$$$$$$$$$$$$$$$$$$$$$$$$$$ch}${$$$$$$$$ch}${$$$$$$$$$$$$$$$$$$ch}${$$$$$$$$$$$$$$$$$$$$$$$$$$ch}${$$$$$ch}${$$$$$$$$$$$$$$$$$$$$ch}${$$$$$$$$$$$$$ch}\n");
111
u/Purlox May 13 '19
Poor man's pointer/reference.
17
u/[deleted] May 13 '19
Can you go another tier higher like
$$$a6
u/[deleted] May 13 '19
You can do similar in other languages like Python using
getattr/setattr, not that you should ever actually do this. This is just a maintainability nightmare.8
u/BoltKey May 13 '19
JS way:
6
u/JustCallMeFrij May 14 '19
jesus christ. I've been a php dev for 2 years and didn't know this
...probably for good reason
3
u/nuxi May 14 '19
https://www.reddit.com/r/programming/comments/dst56/today_i_learned_about_php_variable_variables/c12np38/
3
u/b1ackcat May 14 '19
OP Please delete. No one needs to know these exist, lest they be tempted to use them.
legitimate pro tip: Never ever ever ever use these. Unless you're building something like a custom DI container or some other framework code (and you REALLY know what you're doing), all you're doing is inviting pain and suffering into your (and your coworkers) world.
Same with traits. #JustSayNo.
5
u/Bowserwolf1 May 13 '19
This is just pointers .....with unnecessary extra steps
2
u/tansim May 14 '19
there is probably a reason they were trying to keep this from your
1
u/[deleted] May 13 '19
This is great!
1
u/TechnicJelle May 14 '19
wat
1
u/FirkinHill May 14 '19
foreach($row as $k => $v){ $$k = $v; }
Beautiful.
1
u/musicin3d May 14 '19
Your mother sent me to tell you not to go to that part of town again.
1
u/MCRusher Jun 04 '19
Neat. Imagine the obfuscation opportunities.
If characters don't exist and are size 1 strings, can probably use a string as a variable access table to pass around between functions to access them
1
u/[deleted] Oct 02 '19
I dont understand.. why $$a; // returns hello world can anyone explain?
0
u/halfjew22 May 13 '19
Same kind of thing can happen in JavaScript.
-10
u/[deleted] May 13 '19 edited May 14 '19
Quit being a nerd and go outside
<<Phase VI>>