For you: formatting your php code

When we write in English we put spaces between our words, break up our thoughts into paragraphs, use headings, and so on. This makes our writing easier to read. (This is one big reason why people feel English is "easy" and Tibetan is "hard"!)

When we write code, we make spaces between our php "words", separate our programming "thoughts" (blocks) with empty lines, add commments, and so on. This to make our code writing easier to read. Also, when your code is formatted clearly, it helps you think clearly. And we need all the help we can get when writing code!

white space is your friend – breathing room!

In books and sample code you will often see everything all squished together, like this:

This is sometimes for saving space, and sometimes for ego ("look how cool i am, i can write code that nobody can read!").
It is not professional programming!

If you put spaces between your code pieces:
$name = '<p>' . $firstname . $lastname . '</p>';
then you and others will have a much easier time reading your code, and also editing it. (Yes, that very small . is a piece of code, with a very big name: the "concatenation operator"!

white space is your friend – tab over!

If you indent your blocks of code, it will be much easier to read and to fix problems (debug).

What does "indent" mean?

Here's some code with no indenting:

if ($name == 'tashi') {
echo 'hello student!';
echo 'welcome to class!';

Here's some code with thoughtless indenting:

if ($name == 'tashi') {
      echo 'hello student!';
 echo 'welcome to class!';}

Here the "code block" inside the if is indented 4 spaces:

if ($name == 'tashi') {
    echo 'hello student!';
    echo 'welcome to class!';

"Well", we can say, "big deal! So what? The code still works fine, right?".

Yes, the code works fine, but how well do you work? With this:

if ($name=='tashi') {
if ($name2=='dolma') {
}elseif($name2=='tenzin') {
} else {
echo "<p>Hello $name $name2!";
echo '<br />You are in class <font color="'.$color.'">'.$class.'</font>';
echo '</p>';

or this?

// Define the names:
$name = 'tashi';
$name2 = 'tenzin';

// if the name is tashi, then look for second name:
if ($name == 'tashi') {

    // Now define class and color, 
    // according to second name:
    if ($name2 == 'dolma') {
        $class = 'X';
        $color = 'blue';
    } elseif ($name2 == 'tenzin') {
        $class = 'XII';
        $color = 'red';
    } else {
        $class = 'unknown';
        $color = 'grey';

    // display the result:
    echo "<p>Hello $name $name2!";
    echo '<br /> You are in class <font color="' . $color . '">' . $class . '</font>';
    echo '</p>';

// end if name is tashi.
} else {

    // else if the name is not tashi, give a message:
    echo "<p>Sorry, your name is $name, it is not tashi!";

} // end else name is not tashi.

“Why go to the trouble of formatting my codes? Why have standards? As long as my program works ...”

Coding is about logic, organisation, and clarity.
When your code is a mess, your programming is probably a mess also!
When you write beautiful code, that helps you write beautiful programs.

If you are only going to write a program once, never look at it again, and nobody else is going to use it, then maybe standards don't matter. But more likely you will want to maintain and improve your programs, use your code in other programs, and have other people use them! In that case, it is very helpful to think about code formatting, consistency, systematic coding, and universal standards.

It would be great to have Tibetan and Chinese translations of this page. Can you help? Contact us!