Variables naming in PHP

Surely you know, that variables in PHP always starts with $ and then must be a character [a-Z] or underscore.
That's the rule.
How official man says:

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

(http://www.php.net/manual/en/language.variables.php)
BUT!

There is one more tricky way to name the variables - using variable variables. To those, who don't know what is it: read about it .
Now, the trick:

$a = '123';
$$a = 'abcd';

echo $$a;

Outputs:
abcd
Sure.

But variable variables gives us one more cute opportunity: access by name, anyway what is the name

Here is how it goes:

${'1'} = 100;
echo ${'1'};

Outputs:
100

So, the original ruse is just not complete - there is one more way to name variables. And this way has no limitations of first and any characters.

Is this useful? I don't know. Just interesting.

Leave a Reply