Jump to content

[SOLVED] Curly Brackets Theory


johne281

Recommended Posts

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?

Link to comment
Share on other sites

$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 

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 3 weeks later...

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();

 

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.