Introduction to PHP
PHP: An Introduction
PHP is the most widely used server side scripting language. The most popular PHP server 'Apache' is an open source server, and it comes bundled with Linux. Due to this, one does not need to spend anything to be able to use PHP.
Why PHP?
- PHP has a very strong community and several websites available on internet offering resources on PHP. Most of these resources come for free! Apart from this the PHP is a very powerful scripting language.
- PHP supports almost all available famous databases so you do not need to worry whether it will work with a particular database or not.
- For other benefits of PHP and to know what PHP can do you can visit http://in2.php.net/manual/en/intro-whatcando.php
PHP Syntax
You can code PHP in any text editor including Notepad on Windows.
- 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-->
First PHP Program
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.
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, 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 number 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 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.
- 1365 reads
