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}") Quote Link to comment 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... Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
JacobYaYa Posted February 8, 2008 Share Posted February 8, 2008 Variable variables. Quote Link to comment 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! Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.