PHP Lesson 1 : Hello World


PHP Lesson 1 : Hello World : 

Hi Guys,

I am starting this series of tutorials in which i will be writing lessons about PHP and you will be learning PHP step by step. So as usual in all programming languages lets start with the Hello World” program. I am assuming that you have installed PHP on your system, and if not then for MS Windows please read this.

For writing PHP code, you can use Notepad and for more advance environment you can use some IDEs like Adobe Dreamweaver, Zend Studio and Eclipse. For more IDEs, please read this post from IBM.

Open your IDE, and write down the code below ( Please dont copy paste 🙂 )

<?php //PHP start tag

/* Print Hello World */

echo "Hello World";

//PHP end tag

Explanation : Now lets explore the above code line by line. Look at the line 1 and line 7. Every PHP file starts with PHP starting tag ( <?php ) and ends with a PHP end  tab ( ?> ) . The starting tag is always required and the ending tag can be ignored, but for best practice use end tag also at end of the file.

At line 1 after PHP start tag, you can see PHP comments. Comments are very important and it document your code. There are two types of comments, single line comments and multiple lines comments. At line 1 and line 7 you can see single line comments started with double slashes ( // ). Single line comments can be only with a single line and ends at the end of the that line. Multiple line comments are used at line 3. It starts with ( /* ) and ends with ( */ ), so these comments end where the ending */ us placed.

At line 5 the real operation is performed.

[php] echo "Hello World"; [/php]

This line prints the text Hello World at your browser screen. echo output text or other data. You can also use it as followed

[php] echo("Hello World"); [/php]

Please note that echo is not a PHP function, it is a language construct, so it is not required to use parentheses with echo.

echo is not the only way to output or print text in PHP. You can use print construct also as below

[php] print "Hello World"; [/php]

After you type the above code, then save the file with name hello.php and save it your server. Open your browser and type the link like http:/// and you will see the text Hello World printed on browser screen. Now try some more texts other than hello world.

Our PHP Lesson 1 : Hello World lesson ends , and the next lesson is PHP Data Types and Variables .