Learning PHP Part 9 : The Basic Rules of Writing PHP Code


In this tutorial learn PHP this time, we’ll learn how writing and basic rules of writing PHP code.

Like other programming languages, PHP also has the rules of the writing of such case sensitivity (the difference between upper and lower case), how to terminate a command line, and the influence of the use of space in creating program code PHP. Here are the basic rules of writing PHP code :

1. Case Sensitivity (difference of upper and lower case) in PHP

PHP does not distinguish between uppercase and lowercase letters (case insensitive) for naming a function (function), the name of the class, nor the default PHP keywords like echo, while, and class. The third line will be considered the same in PHP:

But PHP distinguishes between uppercase and lowercase letters (case sensitive) for naming variables, so that $nama, $Nama and $NAMA will be considered as 3 different variables. Often times the error occurred because the wrong variable names, that should use lowercase, written in capital letters.

To overcome this difference, it is advisable to use small letters for the rest of the PHP code, including variables, functions or class. If need a variable name consists of two words, character spacing can be replaced with an underscore (_)

2. Writing a command line in PHP

Statement (command line) in PHP is a collection of commands that instruct PHP to do something. This command line can consist of a brief one line (like echo command to display text on the screen) or it could be something more complex and consist of a few lines, such as the condition of an if, or looping code (loop).

The following is an example of some command line in PHP :

Seen from a few examples of the above command line, PHP uses the semicolon sign (the semicolon) “;” as a sign of the end of the command line.

A collection of command line uses brackets such as the condition of an IF or looping (loop) does not require a semicolon after the closing parenthesis.

3. The Character Spacing and tabs in PHP

In General, spaces and tab characters are ignored in the PHP program execution. You can break up a statement into multiple lines, or unite some statement in a rows long. Like the following example :

The command line is the same meaning with

Although the first examples more save the place, but I would suggest a second example, where we strive so that every statement be on one line only, and add some extra space at the beginning to make it easier to read the code of the program.

The advantage of saving a few lines and some bytes from a PHP file will not be comparable with the effect of the headache you get when trying to understand the code that created the next 3 months (because often combining several statements in the one line). Add a comment on the more complex code as explanations are also highly recommended.

How to make a comment in PHP, we discuss on the next tutorial : how to comment in Writing PHP code.


Please enter your comment!
Please enter your name here