How to write your first PHP program and what are the best programming practices?

PHP is one of the most simple, easy to write and easy to understand programming language. Normally it is said that when you learn one programming language, it becomes easier for you to pick up any other programming language but with experience I can say that many a times it becomes a lot difficult than anticipated. But with PHP, it is actually one of the easist to learn languages for the programmers who are proficient in a language like C.

In order to write a PHP program, we must remember two things that, First of all every PHP program starts with an opening tag <?php and ends with a closing tag ?>. Secondly every PHP statement should end with a semicolon. Moreover you can comment out every line of PHP code with a double slash // and multiple lines can be commented by the /* and */ tags.

This is it, these are the broad syntaxes or methods for writing your first PHP program, so why not write our first PHP program and find out how it works? Let’s welcome you through this first PHP program,

<?php
  echo “Hello Newbie! Welcome to the PHP”;
 ?>

Here echo() function being used is used for displaying some message or output. Syntax of echo() function is as follows,

echo(string $output);

Here $output, as evident from the name, is the parameter containing value to be displayed through echo() function.

This is how it is so simple and easy to write a typical PHP program. PHP by default imposes no restrictions on the structure and etiquettes to be observed while writing a PHP program. But there are some rules that should be observed for writing a great PHP code, which can be easily understood, thus could be maintained by the people and organization for the long term. First of all programming code should look well organized, with proper indentation, placing logical control statements and loop statements correctly, which makes programming code looks systematic and easy to comprehend. In order to further make a PHP programming code more easily readable, it is very important to use comments at many places to explain the purpose of every control statement, loop and logic behind every block of code.

Apart from making our PHP programming code more readable, it is important to make it efficient enough so that it is easier to debug, remove any errors and exceptions that might arise. The best way to debug a PHP script is to insert line numbers in the PHP programming code file. When the PHP interpreter generates an error message, the line numbers tell you where the error has occurred. In order to be able to insert line numbers, you can use quite a few text editors available which insert line numbers to a programming code, for example Macromedia Dreamweaver, Edit Plus, PHP Designer etc.

So these are some of the considerations to be firmly kept in mind as these guidelines can enable a PHP programmer to write a programming code which can be easily understood, debugged and maintained. Being a little systematic and organized is the hallmark of a robust programmer, so go ahead and become one!

Author: ucavik


»