Objective of this page:-
After going through this page, you will know what is PHP about, its brief history and how does a PHP code looks like.
PHP Overview
PHP is the most widely used server side scripting language. (A scripting language is just a method which humans write in notepad etc. Only this time, the web server (e.g. Apache) understands the code acts accordingly)
PHP is actually an abbreviation of Hypertext PreProcessor. PHP was initially developed (in 1995) to dynamically display web content. It has evolved since then and now it's used to write almost all types of web related applications.
Why PHP?
When Bill Gates founded Mircrosoft knew about PHP, they created their own version ASP. ASP is easy. However, PHP is recommended and used by developers all across the Globe because,
- PHP has a very strong community and several websites are available on internet resources on PHP. Most of these resources come for free!
- PHP supports almost all available famous databases so you do not need to worry whether it will work with a particular database or not. PHP is used along-with the free database MySQL. This combination has been deadly and powerful enough to kill any competition. MySQL is also absolutely free.
- PHP is easy to learn. One can start learning PHP in the morning and finish it before he/she goes to bed.
- For other benefits of PHP and to know more about what PHP can do, visit http://in2.php.net/manual/en/intro-whatcando.php
PHP Syntax
Syntax is the way of coding so that the web server can understand. PHP can be coded (typed) in any text editor. One very common editor available on windows group operating system is notepad. So, following list explains what does a PHP syntax look like.
- The PHP code begins with <?php and ends with ?>.
- You can also use <? ?> if short tags are enabled on your php server.
- All valid statements end with ";" a semicolon.
The syntax of php should therefore is something like
<?php
PHP Codes;
?>
First PHP Program
Before we proceed further and delve into the exciting world of PHP, let us output the famous
"Hello World!" with our first PHP program.
Please note that here it is assumed that you have installed the apache server for parsing PHP scripts on your machine. If you do not have any PHP parser installed on your machine. Then it is advised to do so before proceeding further.
Once your web server is up and running, enter the following lines of codes in any text editor and save it as firstpage.php. Notice the extension .php. This extension is a must. On windows, it is advised that while using notepad as editor, to save the file keeping the file type as "all types". Alternatively you can give the file name in quotes also.
<html>
<head>
<title>First PHP page</title>
</head>
<body>
<?php
echo “Hello World!”;
?>
</body></html>
Now copy the file in the root directory of your server. To view this page you should type the address of the directory and the file name. If you have saved the document on your local machine then
http://localhost/firstpage.php should work.
Here note that, If you have multiple servers installed on your machine then it might not work. You will have to explicitly specify the port also, in this case. The page can be found by entering the address as below http://localhost:8080/firstpage.php - Where 8080 is the port number where the server listens to incoming connections.
Now notice the structure of the PHP script. Most of the time while writing a web application the PHP script is embedded in the HTML code. The “echo” is the PHP function which writes the output to response of the server.
Here the Apache server (Or any other PHP parsing web server) will write the code up to <body> segment to the response. When it encounters the <?php code it parses the coming statements as PHP. It echoes or writes the text “Hello World!” to the response. Finally it sends the remaining html code.
So do remember this PHP function echo which will be the most widely used function in your PHP code.
Now that, we've seen a sample PHP code, let us start with the basics of PHP.