Link to home
Start Free TrialLog in
Avatar of elepil
elepil

asked on

Question about PHP curly brackets

I am getting confused with how PHP uses curly brackets.

I know one of its purposes is for syntax clarity. For example:

var $h = "Hello";
echo "$h World!"; // displays Hello World!
echo "{$h} World!"; // also displays Hello World!, but curly brackets are used for clarity

Open in new window


But I am beginning to see other applications of the curly brackets that elude me, such as:

$fields  = array();
$fields{"Name"} = "John"; // This turned out to create an associative element with the key "Name" and value of "John"
$fields["Name"] = "John"; // This turned out to be just identical to the previous line.
var_dump($fields);

Open in new window


So why are people interchanging the use of [] and {}?? This is confusing me. Does anyone have a good link that explains clearly how the curly brackets are used in PHP? The ones I googled ended up confusing me more than helping me. Thanks.
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elepil
elepil

ASKER

I gave a 'B' because I didn't really get my answer. I already read through the link Dave Baldwin and Ray Paseur provided, and it wasn't much help because it did not deal with all the nuances of the {}. I have seen it used as a getCharAt() fashion and other outlandish ways, and I'm disappointed that the official PHP manual does not cover all the nooks and cranny of the language.

But what I did find valuable was the astonished reaction from both Mark and Ray. Dave just gave a link with no opinion or comment, so I can only thank him for responding and some consolation points.

When I post to this board as someone new to PHP, I not only pay attention to the contents of your posts, but also your reactions. Mark was specific he has been doing PHP for 15 years, that adds a lot of weight to his opinion; hence I marked his answer as the best.

Thanks all for responding.
So if I had told you that Ray is the #1 all time PHP expert here and I'm #5, would that have changed your opinion?  Admittedly, I've only been doing PHP for about 12 years.  Does 12 years of C and Assembly language before that count for anything?

You asked for a link, I gave you one.  And by the way, 'syntax clarity' is not one of the reasons for using curly braces.
Avatar of elepil

ASKER

Dave, your points was not based on your experience. You responded by a mere link that I've already read and was not even comprehensive on the subject. Ray has responded to me so many times (just like you as well), and I know he's quite knowledgeable and experienced (just like you as well). But the fact that he sounded befuddled gave me insight, probably not the way he expected to help me.

Sometimes, there will be just no good answer or solution to a problem. But I do know the content I got fell below par because I could name two more different (and bizarre) ways I've seen the curly brackets used, and no one even said anything about those, hence the B grade.
Avatar of elepil

ASKER

Dave, about using the curly brackets for syntax clarity, the book I read says it can be used that way. For example:

$name  = "John";
echo "His name is $name";
echo "His name is {$name}"; // my book says this is clearer in the intent that it's the variable value you're trying to output and not '$name' as a string. I tend to agree.

Open in new window

Then I don't agree with your book though you and your book may use it that way if you want.  I never do that on any of the thousands of PHP files on this computer.  Or on any of my other dozen computers here.  The prototypical example for curly braces in a string is to accurately identify the variable when there would be confusion.  As shown here:  http://schoolsofweb.com/php-curly-braces-how-and-when-to-use-it/  

It would be much more common to use '.' to concatenate the strings and the variables which is what I often do.  That's even 'clearer' than using curly braces and it works with both single and double quoted strings.

echo "His name is ".$name." not Fred.";
echo 'His name is '.$name.' not Fred.';

Open in new window

Avatar of elepil

ASKER

Dave,

You just proved my point.

The prototypical example for curly braces in a string is to accurately identify the variable when there would be confusion.

You just reiterated what I said in a different way. I will agree with you that your latest example is the clearest of them all (but also the most tedious). But the curly brackets in my example served clarity because it does, in your words, "identify the variable when there would be confusion".

But I would even go further that the curly brackets are more significant than anyone has told me on this post. Curly brackets is also used to make a "variable variable":

$func = "capital";
$obj->{$func}="Washington D.C.";
echo $obj->capital; // displays "Washington D.C."

Open in new window


Here's another example:

$x="test";
$y=$x{2}; // acts like a charAt()
echo $y; // displays 's'

Open in new window


But I already knew all the above even before I made this post. What alarmed me was that I was seeing more examples which I felt was outlandish, but worked! That's when I realized there's more to these curly brackets in PHP than meets the eye. So I came here to ask.

But what I got were the usage of curly brackets for if () {} blocks and class Name {}. Seriously, had I been a person of a different kind of temperament, the responses I got from this post would've been harsh because I didn't really learn anything at all. BUT, and unexpectedly, I found something positive from the almost derisive responses a couple of you gave about the curly bracket; I'm sure they were more of reactions than instructional, but which I nevertheless learned something from anyway.

So my giving this post a 'B' is being very generous.
Avatar of elepil

ASKER

Ray, an 'A'? Seriously?

Read my previous post responding to Dave Baldwin. I have been generous with a 'B'. I can't believe you would take it upon yourself to just change the grade.
Well this question has certainly got more attention than it warrants and not in a good way. What you could learn from all this is PHP is a loosely typed scripting language that has evolved many times since its conception. Yes there a more than one way to do things unlike Python where it likes throwing exceptions if you aren't accurarate. What we try to do in EE is educate rather than supply answers. An Author usually gets a sullution or comments based on not only experience but on best coding practices. In my opinion it is futile to spend time working out why something works when in reality one would never see that in any professional organisation. I believe all of the answers were informative and as an ex-moderator I would have done the same as Ray by changing the grade. These questions are available for others to read and hopefully learn from and that is why you get variety in the responses. I hope you learnt something out of this and best of luck to you.
Avatar of elepil

ASKER

If you're going to allow the user to give a grade to a response, don't change it. It's like a company giving out a survey on customer satisfaction of their services, then changing the answers to make them look good. How would you feel if you left a poor rating for a product at Amazon, and Amazon changes it because they didn't like it? The grading system should be EE's way of gauging how satisfied its customers are with the responses they're getting. Because you administrators are changing it at will, that statistic source is now tainted with their subjectivity and bias. Some might argue you're mildly crossing the line of fascism where you correct people's thoughts just because it doesn't agree with you.

Sorry, but I do not agree with Ray changing the grade. That system is for ME to grade YOU, and if you're going to end up changing it, then you're grading yourselves. Why not just remove that grading system altogether and pat yourselves on the back every single time? It's just a source of aggravation the way administrators are implementing it.
Please read the grading guidelines. Unless the answers are demonstrably wrong or defective in some way, the only correct grade is "A".  Until E-E decides to remove the grading guidelines, they can be helpful indicators of good answers, especially for others who come along after us.  The answers here are all good answers and are as good as you can get with a language like PHP that has many variants of its syntax and type coercion.  If you want a more complete answer about curly braces, you will have to read all of the syntax and data type pages in the online manual, because curly braces can be used in many ways, meaning different things with different constructs.  Rather than waste your time chasing arcane edge cases down that rabbit hole, we showed the expected usages, based on our many years of PHP experience.

Here is the link to the E-E Grading Guidelines
http://support.experts-exchange.com/customer/portal/articles/481419
Avatar of elepil

ASKER

Please read the grading guidelines. Unless the answers are demonstrably wrong or defective in some way, the only correct grade is "A".

By gosh, Ray, I guess you must not have heard me. I just stated why it was wrong/defective. It was far from complete, and it was derisive towards the curly brackets concept than instructional. The links it provided led me to a source that was also inadequate because it failed to explain the nuances (of which I gave examples). Yet after all that, you still think there was nothing lacking?

There is a reason why a grading system ranges from A to D. But if all you can see is A or nothing, all the more EE should remove the grading system. According to you, Amazon should also change their rating from 5 stars or nothing.

In my humble opinion, I think your grading system is a good thing. The problem is how it's being implemented by administrators who are clearly averse to anything less than a perfect rating.