Monday, November 14, 2011

How to Write Hello World Program in PHP

hello world program in php

Summary: In this tutorial I will explain you how to write Hello World program in PHP, how to use echo command, where to put quotes, where to put semi colons, what happens if you don't put a echo command and lastly there is a video tutorial on How to write Hello World program in PHP in both English and Hindi.

Hello World Program in PHP

To write a Hello World program, create a new file called HelloWorld.php in notepad++ and save it in the web servers root folder in a new directory called onlinetechtutorials, that is C:/wamp/www/onlinetechtutorials

Now begin the coding by first writing the opening and closing php tags, these tags tells the PHP interpreter that there is a PHP code to process.



Now write the echo command followed by a space, then single or double quotes, inside the double quotes put the string you want to display on your browser window, and in the last put semi colon to denote the end of line.

Semi Colon is important because PHP is a interpreted language and PHP interpreter process your code line by line and semi colon tells PHP interpreter that this line of code is now complete so interpret / process it.


 echo "Hello World, this is Ankit";

Once the code is completed, start the wamp server and go to localhost and type in path of your file, i.e: http://localhost/onlinetechtutorials/HelloWorld.php

You will see the output in your browser window, you can embed PHP code inside any HTML tags too. For example to display a line of text inside H1 tag using PHP, you will write code like this:

<title> Hello World </title>

<h1> <?php echo "Hello World, this is Ankit"; ?> </h1>


You are free to use PHP code inside any of the HTML tags, infact most of PHP codes are written like this, you will understand this much better in the coming chapters.

Hello World Program in PHP [Video tutorial in English]

Hello World Program in PHP [Video tutorial in Hindi]

This is the fourth chapter in which I explained you How to write a Hello World Program in PHP, in the next chapter I will explain you How to create Variables in PHP .