Saturday, April 21, 2007

An Introduction to PHP

PHP ?
PHP stands for Hypertext Preprocessor. Well what is it ? In the world wide web we are familiar with the HTML page. The HTML page is static and does not change. What if we want a page that has some parts common and other parts different according to the browser or user that is visiting the site? Say for an example, a user's profile page. It is wasteful and time consuming to make each user a separate HTML page. Here is where Server side scripting comes in. Thus we can program the page in such a way that the common template is stored in a file and all the variable information such as the user's name, address , etc can be fetched from a database or a remote location and be displayed on the site. This is just one scenario where server side scripting is used. There is many other scenario's like the need for a login, displaying dynamic data etc.

There are many server side scripting languages. One of it is PHP. Other common languages are ASP (Active Server Pages),ASP.net,CFM (Cold Fusion Template),etc...

PHP is different from other conventional languages like C,C++ used to make desktop applications.

PHP file is an ordinary text file and does not need any compiler. The language is interpreted as it is and executed by the server. In desktop applications the data is entered through the keyboard and displayed on the monitor. In web applications that make use of the PHP, data is sent by HTTP methods in the browser. 2 most common ways to input data is via the GET and POST. Similarly the output of a PHP file can be in the form of an HTML file, JPG file, XML file or any format as you wish (Yes!!)

The PHP Language
As mentioned above, PHP files are ordinary text files with the extension PHP. Suppose you want a dynamic page that shows say todays date.


<html>
<head>
<title>Today's date</title>
</head>
<body>
Todays date is : <? //PHP code begin
echo date("M d Y H:i:s");
//PHP code end
?>

</body>
</HTML>


The <? tag marks the beginning of the PHP code segment and the ?> marks the ending of the code segment. There can be any number of PHP code segments in a file. Text outside the code segment is by default interpreted as HTML and send back to the browser as it is. The code within the segement is evaluated and executed and the output of the code replaces the <? ?> The PHP code is executed and replaced in the server itself and no PHP source code is sent to the browser. Thus the HTML recieved by the browser is


<html>
<head>
<title>Today's date</title>
</head>
<body>
Todays date is : Apr 22 2007 09:15:00
</body>
</HTML>


echo is something like the printf. It prints whatever that follows it until the ;. date is a function that returns the formatted date and time according to the argument passed to it.

The code segment is broken up into statements. Each statement ends with a ; as in C / C++.

Variables in PHP
All variables in PHP start with $. eg: $variable = 10;
Unlike C/C++ no data type needs to be mentioned in PHP.

$variable = 10;
$var2 = "arun";
$var3 = array("asd","gfh");
$var4 = false;


To read more about the language, built-in functions and syntax visit www.php.net.

Inputing to PHP

Now you have seen how the PHP outputs the DATA. Now lets see how to push data into the PHP code. Data can be pushed in two common ways :

1. GET : In get method, the data to be passed to the php page can be encoded in the URL in the following format :
http://www.example.com/some/test.php?variable1=something&variable2=something else
These variables are automatically filled into the $_GET array. Therefore $_GET["variable1"] has the value "something" and $_GET["variable2"] has the value "somethingelse".

2. POST : In the POST method the data is not send with the URL but with the HTTP request. An HTML form with action="the destination php file" is used in this case. All POST variables appear in the $_POST similarly as with the GET.

That's all for Part 1
Stay tuned....