Write a comment on this article

Hello world

< back to tutorials main page

This first and introducing tutorial will demonstrate how to use PHP. The ouput of a PHP file needs to be HTML, so that the browser can handle it. That is to say, the file will contain PHP and HTML code (possibly CSS and JavaScript as well).

To use PHP code you need to surround it with the following tags:

Beginning of the code: <?php
Ending of the code: ?>

Everything inside these tags will be parsed as PHP code. Of course you can use these tags more than once in a file, but be sure to maintain the correct order.

To create your first PHP script, start with the HTML structure and then place the PHP code at the correct positions. Here is an example of a very basic HTML structure:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Hello world</title>
  </head>
  <body>
    <div>
      <!-- HERE COMES THE PHP CODE -->
    </div>
  </body>
</html>
 

This isn't very worldshaking yet, so let's pimp it! There are several functions in PHP to print text, the easiest one is echo. Here is the PHP code to print "Hello world":

echo "Hello world";
 

Now we can paste this short PHP code into our HTML structure, but don't forget the PHP tags, otherwise the output will look kind of weird.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Hello world</title>
  </head>
  <body>
    <div>
      <?php
        echo "Hello world";
      ?>
    </div>
  </body>
</html>
 

Congratulations! This is your first "Hello world" PHP script. If you have the possibility to test the script on a webserver, do so. If you don't have a webserver, read Your first page with PHP to get started.

< back to tutorials main page

Tags

PHP, tutorial, hello, world, first, script

Comments (0)

Leave a comment

If you have a question or a problem and you're looking for a solution, please use the forum. Comments might not be answered.

Your name (required, maximum 255 characters) ( remember)
Your message (required, maximum 5000 characters)
 
Insert these letters into the textfield below: XQGT (required, case-sensitive)
Drag the slider to the right (required)