Friday 2 September 2011

PHP Tutorial Ch-01 First PHP Program


CHAPTER-1 First PHP program
Although PHP scripts can be written on any text editor (not on MS WORD or similar software) I would recommend that you get a good IDE for it like Netbeans.

clip_image002
Netbeans IDE

So here’s a simple HELLO WORLD program below.
<html>
<head>
<title>
HELLO WORLD program
</title>
</head>
<body>
<?php
echo "HELLO WORLD!"; //To display text on screen
?>
</body>
</html>

Copy and paste the above code into your text editor/IDE and save it as helloWorld.php in the directory /wamp/www.


To run this program you need to open your browser and type localhost/helloWorld.php. The following output should appear.

clip_image004
Output-Hello World

NOTE: The program won’t run if you just write helloWorld.php in the browser. It is necessary to type localhost/ before it. This is because PHP is a server side scripting language and hence the code is interpreted by the web server and then an HTML document is returned to the client browser. In this case localhost is by default the name of the web server and so all PHP scripts have to be run through localhost. Localhost interprets the PHP script and returns an HTML document to the browser. To see the HTML document returned by the web server right click on the web page and click on View Source. You would see that the script that you wrote is not displayed, but rather an HTML document which straight away writes “Hello World”. This is an advantage of PHP because the script that you write cannot be viewed by the person who views your webpage.

Now, that you have run your first PHP program it is time to understand what you just wrote (or rather copied). I won’t be explaining the HTML part of the script, only the PHP part.

Any PHP script is enclosed within <?... …?>. ‘<?’ is used to start a script and ‘?>’ is used to end the script. Between these the main PHP script is written. In the above HELLO WORLD program only one statement is written i.e. echo “Hello World”; echo is used to display text or numbers on the screen.

echo can be written in the following different ways:-
  • echo “HELLO WORLD”;
  • echo(“HELLO WORLD”);
  • echo 5;
  • echo(5);
  • echo $var;
  • echo ($var);
All the above echo statements perform the same task of writing something onto the browser display. However, there is a difference in what the first and second echo statements write and what the third and fourth statements write and what the last two statements write onto the browser window.

The first and second echo statements do the same thing of writing a string on to the browser window. And the third and fourth statements do the same task of writing an integer on the browser window. And lastly the fifth and sixth statements do the same task of writing the value of a variable onto the browser window.

So we infer two points from the above discussion-
  • echo statement maybe used in any of the forms echo <value> or echo(<value>) where <value> maybe anything like integer, string or variable of any datatype.
  • echo statement can take any of the (basic) datatypes as argument/parameter.
Notice that all the echo statements end with ; This symbol is used to end all statements in PHP just like in C, C++, Java etc. Also in the HELLO WORLD program there is a line //To display text on screen after the echo statement. This is a comment. Comments in PHP can be in the following ways-
  • //comment (for single line comments)
  • /*comment….. (for multiple line comments)
……………..*/
Now that you know what the basic structure of a PHP script is like you may move on to the next chapter.

No comments:

Post a Comment