Re: Proper code formatting

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

 



At 4:19 PM +1000 3/23/09, 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;
}

Angus:

The way I would (a personal choice) indent it would be:

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;
   }

However, that's not the way I would code it:

First, I would use tabs, not spaces.

Second, your "if statements" should be == and not = .

Third, I don't like run-on if's -- I would use a switch statement instead:

switch(1)
   {
   case $variable == 'this':
   do this;
   and this;
   break;

   case $name == 'bill':
   do something with bill;
   and something else with bill;
   break;

   default:
   assume its not bill;
   and do the fred thing;
   break;
   }


The switch is easier for me to understand. YMMV.

Cheers,

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
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