Jump to content

What is the function of curly braces {}?


jeboy

Recommended Posts

It's so PHP knows that it's a variable, if you did "$f" then it would think it was a string, putting it in curly braces tells PHP that it's a variable...

 

Even theres no curly braces "{$f}" on it PHP can interpret it ($f) as a variable because it's inside double quotes (" "). It can only be considered as string if its inside single quotes.

Link to comment
Share on other sites

Tell me which looks cleaner:

 

echo "My {$string} is very clean because I use {$curlybraces} around variables in a doublequoted {$string}";

 

Vs.

 

echo "My $string is very ugly because I didn't use $curlybraces around variables in a doublequoted $string";

 

Or worse..

 

echo 'My '.$string.' is very ugly because I decided to '.$concatenate.' variables.';

 

Good coding practices dictate cleanliness. The first example is clean. The second can be difficult to read, the third is just a mess.

 

Curlybraces also let you get away with;

 

echo "My {$_GET['string']} is clean.";

 

Try that without curlybraces. Not saying you should do it that way, but the option exists. Much cleaner than concatenation!

Link to comment
Share on other sites

That would be a personal opinion wouldn't it. I use:

 

echo "My " . $string . " is easy to read because I decided to " . $concatenate . " variables.";

 

which I can look at in about 1/10 of a second and see that the variables are $string and $concatenate.

 

 

Link to comment
Share on other sites

Tell me which looks cleaner:

 

echo "My {$string} is very clean because I use {$curlybraces} around variables in a doublequoted {$string}";

 

Vs.

 

echo "My $string is very ugly because I didn't use $curlybraces around variables in a doublequoted $string";

 

So its just some kind of labeling or naming convention on part of the programmer, theres no big deal when it comes to PHP's interpreting. Good coding practice is not the issue here, we're just looking for a reason if there is a special function or purpose with this curly braces for PHP's processing.

Link to comment
Share on other sites

Double quotes also makes PHP have to parse the string but that doesn't really matter unless you have a big site.

 

That's actually a good point. And on that note, I think you've converted me to curly braces. I'm a stickler when it comes to little things that add efficiency. Thanks for the comments!

Link to comment
Share on other sites

There are a significant number of posts in programming forums by people using the close-quote, concatenate, open-quote... method that have syntax errors that they cannot find (usually due to missing dots or mis-matched quotes.) But I cannot recall any people with syntax errors using the {} method. {} have the added benefit of allowing the exact same syntax for an array variable inside of string as when it is used outside of a string.

 

Only use the close-quote, concatenate, open-quote... method when you must, when you are concatenating the result of function calls within strings.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.