R
R
Roma Zvarich2015-10-29 23:22:28
PHP
Roma Zvarich, 2015-10-29 23:22:28

Which parenthesis is better to use for string and array element access in PHP?

I am learning PHP.
In the video courses "Specialist" the lecturer says that array elements and string elements can be accessed by index using square [ ] and curly { } braces. And there is no difference .
I found a discussion on the Internet that curly braces should be declared "deprecated" in future versions.
Personally, I tend to use square ones, but everyone is wondering if there is a difference ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-10-29
Protko @Fesor

the difference is that square brackets are a way to get an element by index. Whereas curly braces provide us with a construct for dynamic index calculation. And not only the index:

$foo = (object) ['bar' => 'buz'];

${'foo'}
$foo->{'b' . 'ar'} // 'buz'
${'foo'}->{'bar'} // 'buz

that is, roughly speaking, curly brackets are a general way, and square brackets are a particular way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question