What is PHP Programming Language?
PHP Stand for Hypertext Preprocessor
PHP is an open source, Server Side Scripting language which is mostly used for dynamic website development. It can be embedded easily with HTML (HyperText Markup Language).
Definition of PHP
“PHP as Cross Platform, HTML embedded, Server side, Web scripting Language“
As definition have some technical term so let’s have look that terms in brief:
PHP as Cross Platform:
PHP Language can support most of Operating System like Linux, Unix variants (including HP-UX, Solaris and OpenBSD), Microsoft Windows, Mac OS X, RISC OS etc. PHP also supports all kind of web server like Apache, IIS etc.
PHP as HTML Embedded:
PHP code can be embedded with HTML code. It helps us to build dynamic websites, which means PHP embedded code execute on server and will return HTML to client.
PHP as Server Side:
PHP code only run on server it mean any web server which are available in different OS like IIS, Apache are able to run PHP block. Some pre-built package available on web world like WAMP, LAMP, MAMP will setup a whole working environment which is capable to run PHP. PHP code runs and compiles on a server and generates HTML, which returns to client. This is how PHP is different compared to JavaScript which is Client Side Scripting Language.
PHP as Web Scripting Language:
PHP is a kind of language which is capable to run via a web browser. To run PHP web script we need three things
-
Web Parser (CGI or Server Module)
-
Web Server
-
Web Browser.
PHP History:
Rasmus Lerdof is the father of PHP, who created PHP when he wrote several CGI programs in c, which he used to maintain his personal homepage. First version of PHP known as PHP/FI (Personal Home Page/ Form Interpreter) in 1994 which work as simple CGI (Common Gate Interface) script which written in C programming language. In 1995 Rasmus released PHP/FI source code to public, which allowed to use PHP as per need. Here is hierarchy of PHP generation:
- Simple CGI (Common Gate Interface) – 1994
- As PHP Tool – June 1995
- Tool as FI (Forms Interpreter) – September 1995 – look like PERL Language
- Personal Home Page Construction Kit – October 1995
- PHP/FI – April 1996
- PHP 2.0 – November 1997
- PHP/FI 2.0 – Development End – 1997
- PHP 3.0 Development Start- 1997 – Introduce PHP (Hypertext Preprocessor) by Rasmus Lerdof along with Andi Gutmans and Zeev Suraski of Isreal.
- PHP 3.0 Release – June 1998 – With using public PHP team
- PHP 4.0 – 1999 – Along with Zend Engine (With using first name of Zeev and Andi)
- PHP 4.0 – Official Release – 2000
- PHP 5.0 – 2004 – with Zend Engine 2.0
Project which famous as PHP related project:
- Jan-2000 – PEAR (PHP Extension and Application Repository) – At time of PHP Developers Meeting (PDM)
- 2000 – PQAI (PHP Quality Assurance Initiative)
- March-2001 – PHP-GTK – Andrei Zmievski
Advantages of PHP
- Free & Open Source
- HTML Embedded
- Execute Server
- Simpler and Easy to learn for new beginner
- Low Cost
- Create Dynamic Web Pages
There are three area where PHP is used most:
- Server Side Scripting
- Command Line Scripting
- Desktop Application
Other Benefits:
PHP works as either a module or as CGI processor. As PHP can be used on all major operating system and also support most of web servers, we have options to choose any OS or Server. PHP also support Procedural programming and/or Object oriented programming.
PHP use in services like LDAP, POP3, SNMP, HTTP and many more. PHP support database like MySQL, Oracle, SQL and many more. PHP has built in many other support like XML (Which include SAX, DOM, and XSLT), Regular Expression, built in function. PHP not only support output into HTML but it support output in different way like PDF File, Flash Movie, XHTML, XML etc. PHP support all kind of database programming by using database specific extensions like MySQL, PDO, ODBC etc.
PHP Syntax with “HELLO WORLD” Example
A file with .php extension will need to be in “DOCUMENT_ROOT” of server (www), where it can be executed by web server. Run any .php file which is in Document Root using http://localhost/phpinfo.php or http://127.0.0.1/phpinfo.php. If the file did not give any output or it asking to download, then there could be possibility that web server not running or it is not properly configured.
Hello World PHP Program:
Let create file helloworld.php and run it on web server using URL: http://localhost/helloworld.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World</title> </head> <body> <div> <h2>Hello world Application</h2> <h3>HTML Embedded with PHP</h3> <?php echo 'Hello World'; ?> </div> </body> </html> |
After found the .php extension, web server will only parse section inside the “<?php ‘The Code’ ?>”. The tag basically is way to start and stop interpreting PHP code, everything outside of this starting and closing tag is ignored by the PHP Parser. When PHP interpreter parse closing tab ?>, It output whatever come after that until next opening tag <?php.
PHP tag can be used like the methods mentioned below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php ..php code block.. ?> <? .. php code block .. ?> <% ..php code block.. %> <script language='php'> ..php code block.. </script> |
<?php ?> // standard tags
< ? ?> // short tags, need short_open_tag enabled in php.ini
< % %> // asp tags, need asp_tags enabled in php.ini
<script language=”php”> </script> // case insensitive
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
If you save your code as UTF-8, make sure that the BOM (EF BB BF) is not present as the first 3 bytes of the file otherwise it may interfere with the code if the PHP need to be run before any output
PHP Comments
1 2 3 4 5 6 7 |
<?php echo 'This is a test'; // This is a one-line c++ style comment /* This is a multi line comment yet another line of comment */ echo 'This is another test'; echo 'One Final Test'; # This is a one-line shell-style comment ?> |
In PHP every code statements end with semicolon ‘;’ . Code block are enclosed in curly braces { }. Comment in code begin with // for single line comment and for multi-line comment, we have to use /* code block */
echo is generally used to display output.
Note:
- HTML comments have no meaning in PHP parser.
- When the comment string contains ‘?>’, you should be careful.
- PHP comments do not take up PROCESSING time
- Comments in PHP can be used for several purposes, a very interesting one being that you can generate API documentation directly from them by using PHPDocumentor (http://www.phpdoc.org/).
- Be careful when commenting out regular expressions.
How PHP program Run:
When web server found .php extension then it refer that file e.g. ‘helloworld-php.php’ to PHP processing engine. After engine processes PHP code block by recognize PHP tag, compile that code statement and covert output to HTML. In our example echo construct can display string within DIV.
Basically Client-Server relationship is very important into any PHP application because PHP always run on server. Also one point needed to consider is that only one file can run at one time because only one file can request at one time from server. There is few way to include file with using some PHP function like include(), require() but at the end server can execute only one file at a time. All the data and variable are lost each time the single page is processed and the HTTP request is fulfilled. But there is way in PHP to preserve variable or data from one page to other page using session.
Let’s have look in Client-Server Relationship:
Any program which interact with a server we call them as client and any program that provide service for client program we call them Server.
Client side technologies enable the web client to do more interesting things than displaying static documents. Usually these technologies are extensions of HTML, and didn’t replace it entirely.
Server side technologies are those that enable the server to store logic to build web pages on the fly. More information you will get from above mentioned images which show us how HTML/PHP request work with HTTP between client & server.
Example: Programs like browsers, FTP, e-mail are client and it is connect with server like web browser, FTP Server and mail servers.
Hopefully, you can aware of PHP after this introduction article.