...

View Full Version : Which is more correct? { or :



missing-score
04-14-2003, 12:34 AM
Right, now I like to keep PHP code as strict as possible.

In most cases I have seen this done:

if(condition){
// Do whatever
}

However, I have also seen:

if(conditon):
// Do whatever
endif;


Can anyone tell me if one of these is more 'proper' than the other.

Spookster
04-14-2003, 01:07 AM
One is not any more proper than the other. Many languages throughout history have/had slightly different syntax for control structures like the if/else blocks. PHP on the other hand allows you to choose which one you like. So it's more just a personal preference which one you feel more comfortable in using. Being a C++, Java programmer I usually use:



if(condition){
//yaks
}
else{
//yaks
}


since that is how it is done in those languages as well.

firepages
04-14-2003, 03:40 AM
+ there are a few 'coding standards' around , and whilst they all have ways and means they almost all support the

if($condition){
}

or

if($condition)
{
}

syntax , the older ':' method is generally frowned upon

missing-score
04-14-2003, 10:05 AM
Thanks for your help guys.


P.S: Isnt

if(condition){
// Do whatever
}

and

if(condition)
{
// Do whatever
}

the same? I always use the second and indent everything after a { so that it is easy to count if you miss out a }, but apart from the break, they look exactly the same.

I have also seen people using one-line if statements:

if(condition) // Do whatever


Is this ok too, without the {}, or should you always use {}

Phantom
04-14-2003, 11:55 AM
If the code you're executing in the if statement contains only one line, then you don't need {}


if($condition) callFunction();

is the equivalant to


if($condition) {
callFunction();

firepages
04-14-2003, 12:19 PM
its not a case of whether it works or not (which is of course of primary importance ;)) but if you want to stick to a set of coding standards , most would suggest that even 1 liners should include the braces regardless of whether you actually need them or not, its basically about readability for other developers who may have to mess with your code at alater date.


(From PEAR `standards`)
You are strongly encouraged to always use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.


The closest thing to 'official' PHP coding standards can be found at PEAR .. http://pear.php.net/manual/en/standards.php

Note that many do not give a hoot about PEAR perhaps with some justification , but as its now part of the 'official' release we may as well put up with it?

missing-score
04-14-2003, 01:46 PM
Thanks for your help in this. It turns out that I wont be changing anything in my coding. Thanks for the info though! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum