tags:

views:

113

answers:

5

Does the following code below do the same thing and if so which one is better when coding? And is there a name for when PHP code is missing curly brackets?

The PHP code.

<?php if (isset($_POST['email'])) { echo $_POST['email']; }?>
<?php if (isset($_POST['email'])) echo $_POST['email'];?>
+5  A: 

Personally I always write statements that can use curly braces with curly braces. For example take the code:

if (UserIsLoggedIn())
    ShowCreditCardDetails();

Now if some amateur coder comes along and wants to also do something else when the user is logged in:

if (UserIsLoggedIn())
    WriteAuditLog();
    ShowCreditCardDetails();

Now, ShowCreditCardDetails() will be run for users who aren't logged in, because only the first statement after the if gets executed when no curly braces are present. Had that been:

if (UserIsLoggedIn())
{
    ShowCreditCardDetails();
}

this wouldn't have happened because the curly braces enclose the entire code block to be executed.

I don't believe there's a name for this, it's just a shortened way of writing an if statement. Note I also put the curly braces on their own lines outside of the code block and the original statement as well - it just aids in readability.

Andy Shellam
i agree completely, parentheses and curly braces are good to use, they alse make the code more readable
marvin
I generally try to use braces, purely because if I get into the habit of not using them for single commands, when I move over to say Perl, which doesn't support no brace commands like this, I keep using them, and create loads of errors. However I prefer having the braces on the same line as the statement. I find it just as easy to read, and wastes less space.
Psytronic
I would agree with you Andy if you just hadn't added an <enter> on the first { I really hate { } like that :(
AntonioCS
@AntonioCS, @Psytronic - personally I find that having the braces on a separate line clearly separates the `if` statement and the code block, especially if it's a complicated `if` statement (I then write one condition per line.) I don't mind wasting space if it makes code clearer to read on-sight.
Andy Shellam
+1  A: 

if you dont use the curly brackets it does exactly the same thing but it stops after the first ; and the code followed after isn't in the if statement anymore (in your example it doesnt matter since you have only one ; in there). personally, i'd hate this kinds of shortcuts.

antpaw
+2  A: 

In this case you can go with the second version. The reason why you can omit the brackets is that you don't need brackets if the control statement's body only contains one statement:

if (isset($_POST['email'])) 
    echo $_POST['email'];

But of course this can lead to problems if you extend your script and forgot about this:

if (isset($_POST['email'])) 
    echo $_POST['email'];
    echo $_POST['foo'];

Here, echo $_POST['foo']; is not contained in the if statement. Similar is this:

 <?php if (isset($_POST['email'])) echo $_POST['email'];  echo $_POST['foo'];?>

In both cases, echo $_POST['foo']; will be executed regardless of the if clause.


But if you use this in a template and just want to print a field if it is set, it is unlikely that you extend this short piece of code. So in this case, the "short" version should be fine.

Update:

Please note stereofrog's comment. Outputting unescaped user input can be very dangerous. NEVER trust user input.

Felix Kling
+1 good answer. It'd be worth noting, that "echo $_POST[whatever]" is the wrong way anyways.
stereofrog
A: 

If the block statement count is only one . You no need to put curly braces .

But, If you put the curly braces for single statement also. The code will be in readable format. others can understand easily .

<?php
if (isset($_POST['email']))
 {
 echo $_POST['email']; 
 }
?> 

Now the code will have the indentations.

pavun_cool
A: 

There is no such thing as "better coding". It's a matter of agreement. Choose your favorite coding standard and follow it's guidelines.

http://pear.php.net/manual/en/standards.php
http://framework.zend.com/manual/en/coding-standard.html

are examples and you can google for php coding standard for more

Col. Shrapnel
I disagree with "there is no such thing as better coding." I've both seen and written awful code in my time, and I've come to realise which is psychologically better.
Andy Shellam
@Andy That's your personal opinion.
Col. Shrapnel
I think it's a common opinion amongst developers - code that's easier to read and understand immediately (without having to try and re-format it or work out when an if starts/finishes) is "better code"
Andy Shellam
@Andy The problem is everyone on the Earth has it's own judgement on code readability. Try to realize the fact that your presonal opinion on code readability is not the true for anyone. Some folks on the earth write their texts from right to left. Go tell em they are all wrong, cause it's hard for you to read.
Col. Shrapnel
The fact that you've never seen good code doesn't mean there is no such thing.
stereofrog
@Col. Shrapnel - I understand a lot of it is personal opinion but saying there's no such thing is like saying there's no such thing as better driving. My opinion on code readability is mostly formulated by coding standards - which are majority agreed by the development community. The example about left-to-right isn't even an argument - why do you think code is written in English? There's a difference between being able to read code, and being able to read code **easily**. When code introduces the potential for security issues, it's not "better."
Andy Shellam