Re: Proper code formatting

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Angus Mann wrote:
Hi all, I'm fairly new to PHP so I don't have too many bad habits yet.

I'm keen to make my code easy to read for me in the future, and for others as well.

Are there any rules or advice I can use for formatting (especially indenting) code?

for example how best to format / indent this ?

if ($variable = 'this')
{
do this;
and this;
}
else
{
if ($name = 'bill')
{
do something will bill;
and something else with bill;
}
else
{
assume its not bill;
and do the fred thing;
}


First - if ($variable = 'this') with assign the value "this" to variable and then return true. Probably not what you want ;)

This is what I do:

if ($variable == 'this') {
   do this;
   and this;
   } else {
   if ($name == 'bill') {
      do something with bill;
      and something else with bill;
      } else {
      assume its not bill;
      and do the fred thing;
      }
   }

// note that you were missing a } - easy to miss with your style

Of course - it is better like this

if ($variable == 'this') {
   do this;
   and this;
   } elseif ($name == 'bill') {
   do something with bill;
   and something else with bill;
   } else {
   assume its not bill;
   and do the fred thing;
   }

but what many like to do is something like this:

if ($variable == 'this')
{
   do this;
   and this;
} elseif ($name == 'bill') {
   do something with bill;
   and something else with bill;
} else {
   assume its not bill;
   and do the fred thing;
}

To each his own. Whatever floats your canoe.
Just whatever you pick, stick to it throughout your code.




I'm using "PHP designer 2008" which does syntax coloring but if it has something to automatically indent - I haven't found it yet.

It probably allows you to either set a specify a tab as a real tab or a specified number of spaces. Auto-indenting - this isn't python, the compiler doesn't enforce it's way, you follow the convention of the project you are working on - so I suspect many php editors tailored to php don't have an auto indent.

I've never of course tried that specific product. I use bluefish, vi, and emacs.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux