Introduction to PHP

Introduction to PHP

PHP, an acronym for Hypertext Preprocessor, is an HTML embedded scripting language. As a general purpose language, it is used for web development and HTML. Apart from being one of the potent, server-side scripting languages, PHP is also used for creating interactive and dynamic web sites. The basic syntax of PHP is similar to Perl and C. Due to these similarities, PHP is often used with the Apache web server on various operating systems. In addition to supporting the Internet Server Application Programming Interface (ISAPI), PHP is also used with IIS on Windows. Originally designed for creating dynamic web pages, it has developed as a popular medium for innovative web applications and has helped in developing some command line interface mediums. Rasmus Lerdorf is credited with creating PHP in 1995. Usually, PHP runs on a web server and is available on different operating systems and platforms. Its ability to run on most web servers and operating systems for free has added to its demand and esteem.

PHP has the following benefits:

  • Creates advanced user experience based on resources already collected
  • Quick solution for large and advanced web sites
  • It provides cross-platform functionality
  • PHP converses with several network protocols
  • It is compatible with a wide variety of databases
  • Strong text processing facilities are available
  • It supports most current web servers
  • Convenient solutions for e-commerce
  • Diverse scopes for creating online tools
  • It is not restricted to HTML output
According to recent statistical data, PHP is installed on more than 20 million web sites and around 1 million web servers. The source code for PHP is distributed under a license, thus making it easily accessible to users. PHP can be used effectively on different operating systems such as Linux, Microsoft Windows, many Unix variants (like Solaris, HP-UX and OpenBSD), Mac OS X, RISC OS, and many others. As discussed earlier, in the present scenario, PHP supports most web servers. It works as a CGI processor in servers supporting the CGI standard. Each PHP script remains enclosed between two PHP tags commanding the server to recognise the information as PHP. As PHP is a server-side language, its scripts only run on the operating web server. They never run in the user’s browser. With PHP installed in your computer, you can use both procedural programming and object oriented programming (OOP). In some recent PHP versions, not every OOP feature is mentioned. Some code libraries and large applications have been written by using just the OOP codes. One of the essential features of PHP is that it supports several databases and so you can write database-enabled web pages. PHP supports the following databases: MySQL, Adabas D, Ingres, Oracle (OCI7 and OCI8) , dBase, InterBase, Ovrimos , Empress FrontBase, PostgreSQL , FilePro (read-only), mySQL Solid ,Hyperwave Direct, MS-SQL, Sybase , IBM DB2, Velocis , Informix, ODBC, Unix dbm PHP also supports certain other services such as SNMP, LDAP, POP3, IMAP, HTTP, COM (on Windows), NNTP and many others. The language has certain useful text processing features from the POSIX Extended or Perl regular expressions to analysing XML documents. Some of the important functions performed here include CyberMUT, Cybercash payment, VeriSign Payflow Pro and CCVS functions commendable to use for online financial programs.

1.1. Installation of PHP

There are two ways for installation with Windows. This can be done either manually or by an installer. You require PHP, a web browser and a web server. Normally, there are three common ways of using PHP:
  1. Web sites and web applications (server-side scripting) – 90%
  2. Desktop and (GUI) applications – 2-3%
  3. Command line scripting – 7-8%
Installing WAMP on windows :
  • Double click on the downloaded file and just follow the instructions. Everything is automatic. The WampServer package is delivered whith the latest releases of Apache, MySQL and PHP.
  • Once WampServer is installed, you can add other releases by downloading them on this website. They will then appear in the WampServer menu and you will be able to switch releases with a simple click.
  • Each release of Apache, MySQL and PHP has its own settings and its own files (datas for MySQL).
Using wampserver
  • The “www” directory will be automatically created (usually c:\wamp\www)
  • Create a subdirectory in “www” and put your PHP files inside.
  • Click on the “localhost” link in the WampSever menu or open your internet browser and go to the URL : http://localhost
Installing LAMP with 1 Command
sudo apt-get install tasksel

1.2 Basics of PHP

Apart from a simple text editor, no other additional software is required to create PHP code.
<?

phpinfo();

?>
  This single line of PHP code phpinfo, commands the server to print a standard information table. This provides information about the server setup. In the above example, the line ends with a semicolon and should not be missed, or else you will get an error. Save this script as phpinfo.php, and upload it to the server. Access the URL of the script with your browser. If you can access the script, you will see detailed information about PHP on your server.

How PHP operates ?

The basic function of PHP can be divided into two major sections:
  1. Escaping from HTML and
  2. Instruction separation.
Escaping from HTML: Here, a file is analysed and simply not parsed until a special tag is reached. The entire text ahead is interpreted as PHP code. Instruction separation:  Each statement is terminated with a semicolon. In PHP, the closing tag suggests the end of a statement. The syntax is as follows:
<?php

echo “This is used for testing”;

?>
  Any PHP scripting block begins with <?php and ends with ?>. PHP source code is flexible and can be placed anywhere in a document. Such source code can never be viewed by selecting the View Source option in the browser. The only thing that is apparent while running PHP is the output in HTML format that is created as the PHP scripts are executed on the server before actually sending the outcome to the browser. You can also begin a scripting block with (<?) and end with (?>). This is just a shortened version. It is always advisable to use the standard form of (<?php) in place of the shortened form (<?) as the former is clearer and generally supported.

PHP in HTML

As in an HTML file, PHP files also have HTML tags in addition to some PHP script code. Some important examples are given as below using the text string “Hello World” and sending it to the browser.
<html>
<body>
<h1>
<?php
echo “Welcome”;
?>
</h1>
</body>
</html>
Here, each line of PHP code ends with a semicolon. This semicolon actually acts as a separator between two sets of instructions. Echo and Print are the two basic statements available to output text with PHP.   Since, PHP scripts are basically embedded in an HTML document, you have the freedom to shift between HTML and PHP.    

HTML in PHP

You can even generate entire HTML syntax in PHP code itself.This style is synonymous to Servlets in Java language.   PHP comments: While a single line comment in PHP is made by using //, a large block comment is indicated by /* and */. The following example will make this clear:
<html>
<body>

<?php
//Here is a single line comment or stream comments
/*
It is
inserted in the
block this is a block comment
*/
?>
</body>
</html>
 

1.3 Combining HTML and PHP

PHP and HTML are closely related to each other and often function together. PHP generates HTML and this HTML passes information to PHP. The two together are seen in the following PHP script which includes HTML:
1: <!DOCTYPE HTML>
2: <html>
3: <head>
4: <title>A PHP Script Including HTML</title>
5: </head>
6: <body>
7: <div> <b>
8: <?php
9: print “hello world”;
10: ?>
11: </b> </div>
12: </body>
13: </html>