jeboy Posted February 8, 2008 Share Posted February 8, 2008 What is the function or purpose the curly braces "{ }" in PHP? I see this example file_get_contents("{$f}") Link to comment https://forums.phpfreaks.com/topic/89994-what-is-the-function-of-curly-braces/ Share on other sites More sharing options...
Aureole Posted February 8, 2008 Share Posted February 8, 2008 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... Link to comment https://forums.phpfreaks.com/topic/89994-what-is-the-function-of-curly-braces/#findComment-461419 Share on other sites More sharing options...
jeboy Posted February 8, 2008 Author Share Posted February 8, 2008 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 https://forums.phpfreaks.com/topic/89994-what-is-the-function-of-curly-braces/#findComment-461428 Share on other sites More sharing options...
haku Posted February 8, 2008 Share Posted February 8, 2008 It's considered among some people to be bad form to not use the curly braces. Personally, I like to concatenate my strings. I don't like including variables in them, its too easy to make mistakes in your syntax. Link to comment https://forums.phpfreaks.com/topic/89994-what-is-the-function-of-curly-braces/#findComment-461429 Share on other sites More sharing options...
JacobYaYa Posted February 8, 2008 Share Posted February 8, 2008 Variable variables. Link to comment https://forums.phpfreaks.com/topic/89994-what-is-the-function-of-curly-braces/#findComment-461432 Share on other sites More sharing options...
awpti Posted February 8, 2008 Share Posted February 8, 2008 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 https://forums.phpfreaks.com/topic/89994-what-is-the-function-of-curly-braces/#findComment-461433 Share on other sites More sharing options...
haku Posted February 8, 2008 Share Posted February 8, 2008 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 https://forums.phpfreaks.com/topic/89994-what-is-the-function-of-curly-braces/#findComment-461437 Share on other sites More sharing options...
JacobYaYa Posted February 8, 2008 Share Posted February 8, 2008 Double quotes also makes PHP have to parse the string but that doesn't really matter unless you have a big site. Link to comment https://forums.phpfreaks.com/topic/89994-what-is-the-function-of-curly-braces/#findComment-461438 Share on other sites More sharing options...
jeboy Posted February 8, 2008 Author Share Posted February 8, 2008 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 https://forums.phpfreaks.com/topic/89994-what-is-the-function-of-curly-braces/#findComment-461440 Share on other sites More sharing options...
haku Posted February 8, 2008 Share Posted February 8, 2008 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 https://forums.phpfreaks.com/topic/89994-what-is-the-function-of-curly-braces/#findComment-461442 Share on other sites More sharing options...
KrisNz Posted February 8, 2008 Share Posted February 8, 2008 Its called complex syntax. You could always RTFM. http://nz2.php.net/manual/en/language.types.string.php Link to comment https://forums.phpfreaks.com/topic/89994-what-is-the-function-of-curly-braces/#findComment-461489 Share on other sites More sharing options...
PFMaBiSmAd Posted February 8, 2008 Share Posted February 8, 2008 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 https://forums.phpfreaks.com/topic/89994-what-is-the-function-of-curly-braces/#findComment-461535 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.