7
In PHP, variables usually have a pattern to follow in their declaration.
According to the manual:
Variables in PHP are represented by a dollar sign ($) followed by variable name. Variable names in PHP distinguish between upper and lower case.
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underline, followed by any number of letters, digits or underscores.
However, I noticed that it is possible to "escape" from this rule when defining the name of a variable.
Examples:
${1} = 1; // valor com número ${'1 variavel'} = 'número iniciando'; ${'Nome com espaço'} = 'Wallace'; ${'***Testando***'}[0][] = 'Array Louco';
Upshot:
Array ( [1] => 1 [1 variavel] => número iniciando [Nome com espaço] => Wallace [***Testando***] => Array ( [0] => Array ( [0] => Array Louco ) ) )
In addition to variables being declared like this, it is also possible (think from PHP 5.4), to make methods "run away" a little of your pattern.
class Teste { public function __call($method, $arguments) { echo "Método numérico [$method]"; } public static function __callStatic($method, $arguments) { echo "Método númérico estático [$method]"; } } Teste::{'10'}(); // Método numérico estático [10] $teste = new Teste; $teste->{'10'}(); // Método numérico
After all, what is the purpose of variables being declared between braces ?
Is there any case that would be helpful?
I think there was a similar question about declaring variables, including some close examples. I’ll see if I think.
– Papa Charlie
@Papacharlie, Special characters in identifiers ?
– rray
@rray, this one. I looked for a long time and did not find... :) I remember it was very enlightening when I read.
– Papa Charlie
@Papacharlie, there’s this other one too Is there a problem using Unicode characters for code identifiers?.
– rray
The issue at hand is "why php accepts this and whether I can use it"
– Wallace Maxters
When questions are weird and at the level of curiosities are usually asked by @Wallacemaxters rsrsrsr, deserves the +1 for the dedication in seeking "curiosities"
– SneepS NinjA