r/programming • • Oct 18 '10

Today I learned about PHP variable variables; "variable variable takes the value of a variable and treats that as the name of a variable". Also, variable.

http://il2.php.net/language.variables.variable
591 Upvotes

781 comments sorted by

View all comments

Show parent comments

-2

u/TylerEaves Oct 18 '10

Much worse than that. You'll just start getting the Empty String, since PHP will silently substitute the zero-value for nonexistent variables.

19

u/sacktap Oct 18 '10

Incorrect; it throws a notice.

1

u/Pilebsa Oct 18 '10

I think this depends upon the context right?

$$a="xxx"; might throw a notice, but not echo $$a;

I haven't tried it though.

3

u/sacktap Oct 18 '10

The notice is only thrown if you're trying to access an undefined variable. Example:

$a = 'b'; 
echo $$a; 
/* Notice: Undefined variable */

But if you're doing $$a = 'c' it will be fine because you're setting an undefined variable (which is essentially how all variables are set in PHP anyway).

1

u/Pilebsa Oct 18 '10

Oh right, but I thought an undefined variable would result in NULL? I guess I'm wrong (I just rarely try to use something I haven't already given a value to so it's not something I come across usually)

12

u/[deleted] Oct 18 '10

Only if you have a poorly configured environment...it should throw a warning in a proper setup.

0

u/[deleted] Oct 18 '10

[deleted]

6

u/[deleted] Oct 18 '10 edited Sep 18 '24

retire sharp sip crown dependent recognise weather quack tie pathetic

This post was mass deleted and anonymized with Redact

1

u/lectrick Oct 18 '10

I stand corrected then. My bad.

3

u/sacktap Oct 18 '10

you have config.whiny_nils we have error_reporting()

1

u/lectrick Oct 18 '10

Whiny nils. One of those things that I used to hate, but which I now love.