What is PHP?
PHP is a high level scripting language particularly used in developing websites. PHP is very powerful and easy to learn. PHP is platform independent that is, PHP can run on either Linux or Windows and many other OS. Unlike C or C++, PHP is an interpreted language that is, the PHP code we write doesn’t get converted into machine code. Instead the PHP reads and executes the source code when we run the script. For more information about PHP and its history, visit http://en.wikipedia.org/wiki/PHP
PHP and web server
For creating websites, PHP must be installed and configured on the server hosting the website. Although any modern web server can be configured to run PHP and serve PHP websites, the most widely used combination is Linux, apache, MySql and PHP or jointly known as LAMP. On Windows platform, we can install PHP and configure IIS for accepting requests for PHP. Or we can install WAMP, a single package containing apache, MySql and PHP for Windows and use it instead.
Web request lifecycle
When we request a page through any web browser, the browser creates a specially formatted message (HTTP Request) and sends to the URL we requested. On receiving the request from the client, that is the browser, server invokes the PHP engine and provides the information about the page requested. Now the PHP engine reads our PHP file, parses it and execute the PHP code inside the file. After the execution the output of our script is send back to the client. The whole communication between web browser and the web server is governed by the HTTP protocol, a common language by which browser and server exchange information.
PHP hello world
From now on, we will look at actual code examples. So I expect you have installed and configured PHP or you have access to a properly configured PHP environment.
Create a file under the web server’s root and save the following code in it. Name the file as “hello.php”.
Listing 1.1
<?php
Echo “Hello world”;
?>
Let us examine the code. The first line, <?php indicates the start of PHP code block. The “?>” on the third line indicates the closing of the previously opened code block. PHP only executes code found inside these code blocks, anything outside of these code block is not parsed and printed on the page as it is. For example consider the following code snippet:
Listing 1.2
<html>
<head>
<title>hello world example 2</title>
</head>
<body>
<p>
<?php
echo "hello world!";
?>
</p>
</body>
</html>
Will output following to the browser:
Listing 1.2.1
<html>
<head>
<title>hello world example 2</title>
</head>
<body>
<p>
hello world!
</p>
</body>
</html>
There can be 0 or more such PHP code blocks on a single page. Now the second line in the listing 1.1, echo “Hello world!”; does the actual work of printing the text “Hello world!” to the webpage. The echo and print statements are used for writing to the output. Another thing to notice is that PHP requires that we should terminate each PHP statement using a ’;’ symbol.
Summary
PHP is a free, platform independent, scripting language. PHP is interpreted on the runtime not compiled as some traditional languages like C/C++ are. Also PHP can be setup to work with almost any web server including IIS and apache.
The PHP code is written inside files with .php extension. We use special markers to differentiate code from normal text by enclosing the code inside it.