johne281 Posted July 24, 2009 Share Posted July 24, 2009 Can someone please explain to me what curly brackets do and when is the best time to use them. I am following a tutorial video and he uses curly brackets in the video quite a bit. When I googled for the answer i find that they don't seem to be necessary in most of the code he uses them in. If they are not necessary then what is their use? Quote Link to comment https://forums.phpfreaks.com/topic/167290-solved-curly-brackets-theory/ Share on other sites More sharing options...
cs.punk Posted July 24, 2009 Share Posted July 24, 2009 $var = "exam"; echo "$var"; // exam //What if you want it to echo out "$var*ple" without the *? echo "{$var}ple"; //Could also be done like this echo "$var" . "ple"; Also to echo out the CORRECT way for an array (mmm nice ryhming hey) echo "{$array['first']}"; //unlike echo "$array['first']"; //which won't work Quote Link to comment https://forums.phpfreaks.com/topic/167290-solved-curly-brackets-theory/#findComment-882103 Share on other sites More sharing options...
sKunKbad Posted July 24, 2009 Share Posted July 24, 2009 They designate a block of code to run, instead of just a single line. You can use them with a single line, and I think it actually makes the code more structured, but this is personal preference. For example: if(!isset($some-var)){ //do something line one //do something line two } Quote Link to comment https://forums.phpfreaks.com/topic/167290-solved-curly-brackets-theory/#findComment-882105 Share on other sites More sharing options...
Mark Baker Posted July 24, 2009 Share Posted July 24, 2009 don't forget $testVar = 'Hello'; echo $testVar{3}; echo '<br />'; echo substr($testVar,3,1}; Quote Link to comment https://forums.phpfreaks.com/topic/167290-solved-curly-brackets-theory/#findComment-882256 Share on other sites More sharing options...
.josh Posted July 24, 2009 Share Posted July 24, 2009 In short, they are used as a delimiter for various things. Quote Link to comment https://forums.phpfreaks.com/topic/167290-solved-curly-brackets-theory/#findComment-882276 Share on other sites More sharing options...
vineld Posted July 24, 2009 Share Posted July 24, 2009 All you need to know at this point is that they are used to group together rows of code along with different sorts of loops and if statements. Look at foreach and while in the php documentation for example. There are a couple of other uses as well although they are way less common. Quote Link to comment https://forums.phpfreaks.com/topic/167290-solved-curly-brackets-theory/#findComment-882278 Share on other sites More sharing options...
johne281 Posted July 24, 2009 Author Share Posted July 24, 2009 OK. Thanks a lot guys ( and gals). This clears up a lot and will help my syntax fit standards better. Keep up all the good work . Quote Link to comment https://forums.phpfreaks.com/topic/167290-solved-curly-brackets-theory/#findComment-882315 Share on other sites More sharing options...
phorman Posted August 15, 2009 Share Posted August 15, 2009 Ok, everything stated is almost correct, except..... NEVER use curly brackets in echo.... Curly brackets, while they will work in echo, are meant for print statements only. There is an overhead to processing a string with brackets, and on a small site it is minimal, but learn to code correctly and when working on bigger sites, you will see the payoff. " echo " is a special function if you will that does not require the output to be in quotes in the first place. If you want to append something with an echo statement, use a comma. The following are all valid echo statements. echo $start; echo $start["now"]; echo "Start Time: ", $start["now"], " End Time: ", $end["now"]; echo $start->now(); Quote Link to comment https://forums.phpfreaks.com/topic/167290-solved-curly-brackets-theory/#findComment-899040 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.