Answer the question
In order to leave comments, you need to log in
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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question