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.

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!

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.

 

 

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.

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!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.