PHP programming basics


In previous articles we have covered topics like

How to install PHP on windows
How to install PHP on Linux
How to install Apache on Windows
PHP, MySQL, Apache installers

By now, I hope you would have set up your system to start actual PHP programming.

Lets start PHP programming.

What are tags?

Tags are the starting and ending point for a specific code snippet belonging to any scripting language. For example we close HTML codes between

<html> and </html>

similarly javascript code is enclosed like

<script language=javascript>
//all statements goes here

ASP (Active Server Pages) uses

'ASP code goes here

and similar is the case for other languages.

so you are thinking that why do we need tags? For this purpose first of all you have to understand the process of a web page display in your browser window i.e Internet Explorer (IE), Opera or FireFox (FF) etc etc

Web Page Parsing:

When you write any web page URL in your browser’s address bar e.g and click enter; you in fact send a request to your web server that follows the URL and picks the contents from the target location of the requested web page.

Remember, whatever you see in your browser is HTML output of the actual code of the requested web page and it might not be the actual code specially in cases when a web page uses a client-side technology or server side scripting to process information to show you the output of a page.

This process of converting the actual code to the desired HTML output is called parsing by your web server’s parser engines.

For example, if you request the URL you see my Articles Repository home page with the latest articles on the home page and available articles categories etc etc.

In fact you are watching the HTML output of a complex and long PHP code. When you request the URL the web server follows the location and request the contents from the Server that is contains the requested web page. All the web page code is compiled (parsed) and if any TAGS (be attentive) of some client or server side languages are found in the code the web server ask the related parser engine to take on this code and compile it accordingly to generate the HTML output from the processing. If the code doesn’t have any error or warning etc that can stop the output generation, the parser engine sends back the HTML output to the web server that is generated from the server or client side code. Once all the code contained in different TAGS is parsed to HTML Output the web server sends the resultant web page to your browser window for display.

Let stick to our articles repository request now to understand the tags and parsing in depth.

step 1: is requested

step 2: Server that hosts this web page is requested to send the contents attached with this page.

step 3: web server examine the code for any additional processing, as our articles repository’s back-end is written in PHP therefore the web server will find the PHP TAGS in different places and as soon as it finds the PHP starting tag it will start sending those statements to the PHP parser Engine unless it finds the PHP ending tags for that specific code session.

step 4: web server asks the PHP parser engine to jump in and generate the HTML output from the php code in the page. As all the articles are stored in a MySQL database so the index.php code has necessary statements to fetch the latest articles info from the database. Hence all the information is fetched and the desired HTML output from all this PHP-MySQL integration and processing is returned to the web server from PHP Parser Engine and web server finally show the page that you see by visit my Articles Repository at

So this is how important are the tags for any language in fact they informs the web server that they need to be processed by some parser engine before the final output is sent to the browser.

PHP Starting and Ending Tags:

Similar to other languages PHP code starts with <?php tage and ends on ?>

Lets write our very first PHP code:

echo "PHP ! I am gonna get you.";

Code Explanation:

The above PHP code is very simple and contains only three lines,

first it start with PHP starting tag and after the crap written after it, it finally close the code session with PHP ending tag.

now lets discuss the crap :)

ECHO helps you to display any combination of string and variables on your page… wan’t it simple?


echo “display message goes here”;

everything that you want to display on the page must be enclosed between either double quotes or single quotes, php allows both.

the semi-colon is used to define the end of a statement in PHP. Your first quick verbal assignment.

how will you output your “Hello YOUR NAME” message in php?

stop thinking and give me your answer… good work I know you have something inside you therefore you are learning PHP.

yes, for my name it should be,

echo "Hello Haroon Ahmad";

to make sure things are working for you fine write different sentences and make a php page and browse it to check your php is working as well as you are understanding my lecture. Did you say how to browse a php page? well, make sure your PHP and Apache or other web server like IIS is running, then open a browser window and write http://localhost/your-php-pages-folder/yourpagename.php and this is how we execute PHP pages.

Now close your eyes for five minutes and go through all major points that we have learned till now in this lecture…

wake up guys ! … lets talk about few new friends…


Variables are your friends, whenever you have something to store somewhere temporarily give it to the variables and they will keep it for you and whenever you need it call related variables and they will give it to you and you can change, add or delete the received information and return the new values to variables and they will not complaint at all :)

Who shouted BE SERIOUS? Ok, so to define a variable, seriously, it gives you the facility to reserve some space in the memory for different values to use them later in your script’s life time.

For example, I want to save the message “Hello Haroon” in memory and then first display “PHP Tutorial” and afterward the message I stored; I will write the following simple programme,

$message = "Hello Haroon";
  echo "PHP Tutorial";
echo $message;

the output of this programme will be

PHP TutorialHello Haroon

No It is not a type but it is in fact how the output will look like, if you want the message to appear in the next line then put ‘\n’ after first message i.e

echo “PHP Tutorial \n”;

\n is used for Line Feed or next line. Remember on Linux the line feed is obtained by using \n however on windows you have to use \r\n

\r stands for carriage return and these special characters are called Escape Characters, anyway don’t confuse yourself we will study Escape Characters later in details.

In PHP we define a variable with $ (dollar) sign (dollars entered into our programming too :( )
PHP is a loose-typing scripting language and it doesn’t have any hard and fast rules for variables like C or Java languages have. You can define a variable with $ sign and parser itself determines the data-type of this variable at run time.

However PHP has seven types of variables, and all but one hold a specific class of information. The seven types are: strings, integers, floats, booleans, arrays, objects, and resources.

Strings hold characters (literally: a string of characters) such as “a”, “abc”, “Jack and Jill went up the hill to fetch a pail of water”, etc. Strings can be as short or as long as you want - there’s no limit to size.

Integers hold whole numbers, either positive or negative, such as 1, -20, 55028932, etc. There is a maximum limit to the size of integers - any numbers lower than -2147483647 and any numbers higher than 2147483647 are automatically converted to floats, which can hold a much larger range of values.

Floats hold fractional numbers as well as very high integer numbers, such as 4.2, 1.00000001, and 2147483647000.

Booleans simply hold true or false. Booleans are, in fact, just integers behind the scenes - PHP considers the number 0 to be false, and everything else to be true.

Arrays are a special variable type in that they hold multiple values. Arrays can be quite complicated, and so are covered in detail in their own chapter.

Objects are complex variables that have multiple values, but also their own functions. Objects are also very complicated, and, like arrays, are covered in their own chapter.

Resources are anything that is not PHP data - this might be picture data you have loaded from a file, the result of an SQL query, etc. Resources are used like any other variable, with the key difference that you should generally remember to free up your resources when you are finished with them.

Operators and Operands:

See the following PHP Code,

$salary = 3000;
$bonus = 1000;
$total_salary = $salary + $bonus;  // this line will be explained
echo "Total Salary is: " . $total_salary; // output: Total Salary is: 4000

One other rule you should remember is that you cannot use spaces,dot or dashes in your variables name however you can use an under score.

Operands are the entities that have some values in them or you can say the variables and operators are the symbols that have special meanings in the language for example addition operator ( + )

Also in echo statement I used something new; Dot Operator: it is used for concatenation or to join different entities to create a final string for echo to print on the page.

The following will display “Hi Haroon, How are you?”

$question = "How are you?";
$name = "Haroon";
// concatenation
echo "Hi" . $name .", " . $question;

It helps you to glue different values into a single statement.

With this we will end our today’s lecture. Please practice ! I agree with Shakespear, “Practice makes a man perfect” and programming is all about practice. To learn syntax of a programming is not even the start of the story you can call yourself a programmer only when you are able to create logic to solve some computer problem with less code and fast execution time and you can achieve this through practice. Programming practice polish your skills and gives you new ideas to solve different problems through strong logic.

Practice whatever you learn through my lectures before taking on the next lecture otherwise you are wasting your time.

In our next lecture we will learn the different data types of PHP and Complex use of Operators available in PHP in details.

Till then take good care of yourself and … practice :)

Haroon Ahmad


  1. Articles Repository » Blog Archive » Operators in PHP said,

    January 11, 2007 at 7:44 pm

    […] I hope you would have been practising the last lecture ( PHP Programming Basics ) as I am getting feedback from different students from different countries and I am very happy I am able to contribute to PHP through my series of PHP Articles for beginners and professionals. […]

  2. haroon.ahmad said,

    January 19, 2007 at 10:27 am

    I had some typos in this article where I defined INT as String and then forgot to write $ sign with ‘bonus’ variable about which I had a very good discussion on and thanks to all those fellows who pointed out the mistakes. I have fixed them now.

  3. Articles Repository » Blog Archive » Loops in PHP said,

    January 21, 2007 at 1:42 pm

    […] FAST PHP Tutorial (Part 1) How to Install PHP on Windows (Fast PHP Tutorial Part 2) How To Install PHP On Linux How To Install Apache on Windows Easy Installers to Install PHP, MySQL, Apache on Windows PHP Programming Basics Operators in PHP Decision Making using IF-ELSE in php Decision Making using PHP Switch Statement […]

  4. mushfiq said,

    January 23, 2007 at 10:54 pm

    hi haroon i am new to your site and to .php also i a want to be a learner of php my basic need in my site is to get data from my users in my e mail account . can u help me or any one help me in this matter .
    basically i am an animator in flash “really good one expert level” and also a good designer if any one need me contact me.
    but please help me making my forms if u know any appplication kindly inform me.
    thank ngreat work

  5. haroon.ahmad said,

    January 23, 2007 at 11:52 pm

    Hi Mushfiq

    I have already written a very good articles on How to make email based contact form at
    How To Make Email Based Contact Form in PHP and also you can download a professionally written ready made form from PHP Email Based Contact Form for FREE.

    I hope it will help

  6. Articles Repository » Blog Archive » Detailed Guide To Arrays In PHP said,

    January 29, 2007 at 12:55 pm

    […] In programming sometimes you have to handle with different related values which are bound together in different ways. For example Names Of Top Five Students, Models Of Porche Cars, Variables Submitted From A Form and so on. In such conditions you need to organize your code and logic to handle similar type of data efficiently and quickly. For this purpose you can use the logic of DVDs seller by […]

Post a Comment

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word