Important alert: (current site time 7/15/2013 11:25:12 AM EDT)
 

article

Proper Indentation

Email
Submitted on: 3/5/2001 8:39:35 PM
By: Steve Oliver 
Level: Beginner
User Rating: By 6 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 19542
(About the author)
 
     Teaches the basics of Proper Indentation in your php source.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				I think it is a basic coding standard to Indent each function, loop, etc. This not only helps others that may be working with your source, But it also helps when you go back and look at your source. Below are some examples of right and wrong.

wrong way:
if($foo==""){$var="1");}
should be:
if($foo=="")
{
   $var="1";
}

wrong way:
While(List($blah)=mysql_fetch_row($results)){
if($blah==""){echo $blah;}else{echo "none";}}

should be:
While(List($blah)=mysql_fetch_row($results))
{
 if($blah=="")
 {
   echo $blah;
 }
 else
 {
   echo "none";
 }
}

wrong way:
function doit($var){
if($var==""){$var="false";}else{$var="true";}
return $var;}

should be:
function doit($var)
{
 if($var=="")
 {
   $var="false";
 }
 else
 {
   $var="true";
 }
 return $var;
}

I think you get the basic Idea. Although it may seem long and drawn out, trust me, it will help you in the long run.

-Steve


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/8/2001 10:33:35 PMShaun

Steve is right, although it may seem like more coding up front, it'll help you later when you need to read your code. And if you're publishing code that someone else will be reading or using, it becomes even more important that it's formatted in an easy-to-read manner.

Another common convention is to place the opening brace on the same line as the start of the function definition, loop call, etc:

while(1){
echo
(If this comment was disrespectful, please report it.)

 
9/7/2001 7:34:59 AMMatthias

I agree to Shaun, that there is not only one way to format source code. I use the same formatting as Shaun
function xyz()
{
  print 
(If this comment was disrespectful, please report it.)

 
9/13/2001 9:58:43 PMRob Loach

I have a habbit of doing:

if($var==""){
$var="false";
}

(having the first bracket on the first line)
I find that this is very good. I know that it is neater to have the bracket on a new line, but I really don't like to scrool up and down my code alot.
(If this comment was disrespectful, please report it.)

 
11/19/2002 1:04:09 PMDrat911

Identation this way is WAY too many useless lines. Identation should look like this:

while (blah) {
if (blah) {
do_this_func();
}
}

Simple to read, and notice I didn't use half the page to show off brackets on each line? :)
(If this comment was disrespectful, please report it.)

 
11/19/2002 1:06:07 PMDrat911

forget that post, the form didn't properly show how I indented.
(If this comment was disrespectful, please report it.)

 
7/9/2003 6:32:07 AM

if ($blah == "blah") {
// Do something
}

Thats how I do it
(If this comment was disrespectful, please report it.)

 
7/9/2003 6:33:07 AM

Gah, it doesnt parse hmm
\n if ($blah == "blah") {
\n // Do something
\n }
\n
(If this comment was disrespectful, please report it.)

 
2/24/2006 11:05:23 AMNDawg

Keep in mind that PHP is an interpreted scripting language. Every time a client requests a document, PHP has to strip out the useless white-space. So, it makes sense to code a program with proper indentation, but all “production releases” should be optimized with some sort of optimizer program. For example, the program could rename all variables to 2 or 3 character variables and delete all useless white-space.
(If this comment was disrespectful, please report it.)

 
2/24/2006 11:05:37 AMNDawg

Keep in mind that PHP is an interpreted scripting language. Every time a client requests a document, PHP has to strip out the useless white-space. So, it makes sense to code a program with proper indentation, but all "production releases" should be optimized with some sort of optimizer program. For example, the program could rename all variables to 2 or 3 character variables and delete all useless white-space.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.