D
D
DeusModus2011-01-14 11:54:09
PHP
DeusModus, 2011-01-14 11:54:09

Declaring variables using curly braces?

I came across a blog post by one team about how they vehemently reduce the amount of code .
Everything would be fine, but the look caught on the design

<font color="black"><font color="#0000ff">if</font>(!${0}) log(<font color="#A31515">'Writing failed'</font>);</font><br/>

The question is, why does the language have the ability to declare variables in curly braces and why can I give them numerical names using this method?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nodge, 2011-01-14
@DeusModus

The use of curly braces allows you to refer to an identifier with a variant name.
Simple example:

$name = 'value';
$value = 'test'; 
echo ${$name}; // выведет test

You can also access object properties:
$name = 'propertyName';
echo $object->{$name};

A
Alexander, 2011-01-14
@akalend

> if(!${0}) log('Writing failed');
Idiots wrote code
Gone are the days of saving bytes.
now there are byte cachers (accelerators) that cache the opcode, so this saving is not effective.
the code must be understandable first of all to the programmer, and then to the machine.

A
amduscias, 2011-01-14
@amduscias

$name = 'propertyName';
echo $object->{$name};
If I'm not mistaken, then in this case {} can be omitted.
It seems to me that this situation is better suited here:
echo $object->{$name+"_index"};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question