Jump to content

Reason for not using brackets ( {} ) on conditional statements, function, etc?


devWhiz

Recommended Posts

I've noticed some people don't use { } when they right statements, when I first started learning PHP a couple years ago, I thought you HAD to put the curly brackets, but apparently not, how does this work and why don't some people use them in their scripts?

Link to comment
Share on other sites

The basic syntax of the control structures (if, while, etc.) is just a single line of code after the control structure with no braces. You only need the braces if you have multiple lines to be associated with the control structure. (see this reference: http://us3.php.net/manual/en/control-structures.while.php)

 

I don't think the same applies for functions. But, I ALWAYS use the braces with indenting of code to make my scripts easy to read/analyze

 

There is also a third option:

 

http://us3.php.net/manual/en/control-structures.alternative-syntax.php

Link to comment
Share on other sites

if there is a very short conditional that fits on one line and only has one outcome then it's ok but most agree that it's not such a good idea as when you leave out the {} from statements it can lead to problems later in the life of the code, for example you might want to add an elseif to the statement or grow the argument to multiple lines. Personally it's just easier to read code with {} added regardless.

Link to comment
Share on other sites

Some people find it more convenient to write

if ($i == 1)
    doSomething();

 

than

if ($i == 1) {
    doSomething();
}

 

Both ways do the exact same thing so it's just a preference thing.

 

You can also use the syntax Psycho posted which is usually favorable for templates when you are going in and out of PHP tags. For example,

<?php if ($i == 1): ?>
  some html here
<?php endif; ?>

is a lot cleaner and more readable than

<?php if ($i == 1) { ?>
  some html here
<?php } ?>

 

You can also use this syntax with foreach and while loops which is again more readable.

 

if there is a very short conditional that fits on one line and only has one outcome then it's ok but most agree that it's not such a good idea as when you leave out the {} from statements it can lead to problems later in the life of the code, for example you might want to add an elseif to the statement or grow the argument to multiple lines. Personally it's just easier to read code with {} added regardless.

 

You can use else and elseif while omitting brackets.

if ($i == 1)
doSomething();
else if($i == 2)
doSomethingElse();
else
dontDoAnything();

 

This works just fine.

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.