Hello World with Echo

Posted on 23. Feb, 2010 by admin in Basics


Create a file called index.php and save it to the main folder of your PHP hosting. This file will always display when you go to that folder in your browser. We will be editing the code in this file during the basic tutorials as we won’t really need more than one or two files.

For the PHP code to run you have to put it in between what is called a PHP scripting block. Basically, a PHP scripting block starts with <?php and ends with a ?>.

<?php
//Your code goes here.
?>

So that it can be properly viewed in a web browser, we must enclose our PHP code in some basic HTML tags. Your code should look something like this:


<html>
<body>

<?php
\* Your code goes here. *\
?>

</body>
</html>

Now that we have a base file to work from let’s write some PHP code. The first statement we will learn is the echo statement. With this statement you can print text to the screen by putting the text in between quotes. Something like this:

<html>
<body>

<?php
echo "Hello World";
?>

</body>
</html>

This statement will print “Hello World” on the screen. Please note that after each PHP statement you need a semicolon(;). Well, there you have it! You have run your first PHP script. You can replace the text inside the quotes for PHP to present any statement you want.