Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

Indent

  1. Indent
  2. Indent of statements and function calls
  3. If statements
  4. Loops
  5. Funktion definitions
  6. Indent of classes
  7. Indent a switch
  8. Indent of the example above

1. Indent

To increase the readability of your php code you should indent your code in a proper way. As an example we got the following php code.

<?php
error_reporting
(E_ALL);
ini_set('display_errors'1);
function 
todo($str) {
for(
$i=0;$i<strlen($str);$i++) {
if(
"z" == $str[$i]) {
$str[$i]="1";
}else{
if(
"a" == $str[$i]) {
$str[$i]="2";
}else{
switch(
$str[$i])
case 
"b":case "c":
$str[$i]=1;
break;
case 
"c":
$str[$i]=0;
}
}
}
}
class 
Foobar {
private 
$bar;
public function 
__construct($a) {
$this->bar $a;
}
public function 
replace_something() {
foreach(
$a as $value) {
if(
is_string($value)) {
$value=todo($value);
}
}
}
}
$obj = new Foobar(array("x","z"));
$obj->replace_something();
?>

It doesn't matter what this code is doing. As you see this code isn't indented in any way. You can't easily follow which block begins and which blocks end. But this is necessary to understand any php script and fixing errors. So this chapter deals about how to indent your code.

It doesn't matter how you indent your code but it should be done. Here we use the PEAR Coding Standard. All rules there use the indent of 4 spaces and dont use any tabs. This must be changed maybe in your editor. Of course there are a lot of more indent systems, check http://en.wikipedia.org/wiki/Indent_style.

2. Indent of statements and function calls

At function calls the parameters are seperated with a single space (and the comma of course). This way you can easily check how many parameters a function have. If the parameters are very long you can even write them vertically.

<?php
$str 
str_replace('Foobar''FOOBAR'$str); // spaces between parameters
$str name_xyz('A really long parameter',
                
'another parameter',
                
false,
                array(
124),
                
"foobar"); // function call with 5 parameters, the fourth one is an array
?>

You can add spaces if they increase the readability.

<?php
$var     
"Text"// <-- some spaces before the =
$longvar "Text";
?>

3. If statements

At an if statement the body is indented by one level. The opening curly bracket is written after the if condition, seperated with a space. The closing curly brackets is written in a single line at the same level as the if statement. After the if keyword a space is added.

<?php
if (condition) {
    
// statements
    
foobar();
    
barbli();
    
xyz();
    
$var "x";
}
?>

If the if statement got an else or even an else if it is written after the closing curly bracket, again seperated with a space.

<?php
if (condition) {
    
do_this();
} else if (
condition2) {
    
do_that();
} else {
    
or_this();
}
?>

4. Loops

Loop bodys are indent by one level as for if statements. The opening curly bracket is written after the loop header, seperated with a space. The closing curly bracket is written in a single line on the same indent level as the loop keyword.

<?php
while (condition) {
    
work();
}
?>

At for loops the different parts are seperated with spaces.

<?php
for ($i=0$i<10$i++) {
    
work();
}
?>

Same style is used for do-while and foreach loops.

<?php
do {
    
work();
} while (
condition);

foreach (
$array as $value) {
    
work($value);
}
?>

5. Funktion definitions

Each function definition should always have a PHPDoc comment. This way every developer can read what the function is doing. The parameters are seperated with a space. The opening curly bracket is written after the parameter list (again seperated with a space). The closing curly bracket is written on a single line at the same indent level as the function keyword. The function body is indented by one level.

<?php
/**
 * PHPDoc for the funciton foobar
 */
function foobar($bar$bla) {
    
work($bar);
    
work($bla);
}
?>

6. Indent of classes

Classes are indented in the same way. Each field and methods are seperated additionaly with an empty line. As for functions the classes should also have PHPDoc comments for each part of the class.

<?php
/**
 * PHPDoc about Foobar
 */
class Foobar {
    
/**
     * PHPDoc about $xyz
     */
    
private $xyz;

    
/**
     * PHPDoc about XYZ()
     */
    
public function XYZ() {
        
work();
    }
}
?>

7. Indent a switch

Switch statements using the same style, however the case statements are written in the same indent level as the switch keyword.

<?php
switch ($var) {
case 
'z':
    
work();
    break;
case 
'y':
    
work();
default:
    
work();
}
?>

8. Indent of the example above

We indent the example now. Here is the same example from above.

<?php
error_reporting
(E_ALL);
ini_set('display_errors'1);
function 
todo($str) {
for(
$i=0;$i<strlen($str);$i++) {
if(
"z" == $str[$i]) {
$str[$i]="1";
}else{
if(
"a" == $str[$i]) {
$str[$i]="2";
}else{
switch(
$str[$i])
case 
"b":case "c":
$str[$i]=1;
break;
case 
"c":
$str[$i]=0;
}
}
}
}
class 
Foobar {
private 
$bar;
public function 
__construct($a) {
$this->bar $a;
}
public function 
replace_something() {
foreach(
$a as $value) {
if(
is_string($value)) {
$value=todo($value);
}
}
}
}
$obj = new Foobar(array("x","z"));
$obj->replace_something();
?>

And now with proper indention.

<?php
error_reporting
(E_ALL);
ini_set('display_errors'1);

/**
 * PHPDoc about Todo
 */
function todo($str) {
    for(
$i=0$i<strlen($str); $i++) {
        if (
"z" == $str[$i]) {
            
$str[$i]="1";
        } else {                      
// maybe replace with an else-if to save
            
if ("a" == $str[$i]) {    // one indent level
                
$str[$i]="2";
            } else {
                switch (
$str[$i]) {
                case 
"b":
                case 
"c":
                    
$str[$i]=1;
                    break;
                case 
"c":
                    
$str[$i]=0;
                }
            }
        }
    }
}

/**
 * PHPDoc about Foobar
 */
class Foobar {
    
/**
     * PHPDoc about $bar
     */
    
private $bar;

    
/**
     * PHPDoc about the constructor
     */
    
public function __construct($a) {
        
$this->bar $a;
    }

    
/**
     * PHPDoc about replace_something()
     */
    
public function replace_something() {
        foreach (
$a as $value) {
            if (
is_string($value)) {
                
$value todo($value);
            }
        }
    }
}

$obj = new Foobar(array("x","z"));
$obj->replace_something();
?>

Questions about the chapter

No questions

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!