Introduction to PHP: Hello World

This tutorial will introduce you to PHP with the customary Hello World script. You will be introduced to the echo language construct and the date() function. The goal of this tutorial is to show you how to output text, and display the current date.

I will not explicitly say save this as “file.php”, upload to your server, and access in your web browser, but it is strongly encouraged that you do so at each step.

To start, let’s output the text “Hello World” to the screen.

1
2
3
4
5
<?php
 
echo 'Hello World.';
 
?>

Any text placed within the quotes will be outputted to your screen when you access the script in your web browser.

Now, we are going to display the current date in addition to our greeting. We can get the current date using the date function which in this example takes one parameter, but usually takes two. We will discuss that in a future tutorial, but the other parameter is a standard Unix timestamp – the number of seconds since 00:00:00 UTC on January 1, 1970. We do not need to specify it here since the date function will assume I am after today’s date by default.

1
2
3
4
5
<?php
 
echo date('d/m/Y');
 
?>

The above will output the current date to the screen. The one parameter taken is what defines the format in which our date is returned. The page for date() in the PHP manual has all you need to know about the various ways in which you can format the date.

Now that we have learned how to output some text of our choice, and retrieve today’s date, let’s combine the two.

1
2
3
4
5
<?php
 
echo 'Hello World. Today\'s date is ',date('d/m/Y'),'.';
 
?>

You should notice the backslash preceding the single quote in “Today’s”. This is called escaping and tells PHP to ignore the character and continue on. It will be discussed in much more detail in a tutorial later in this series.

NOTE: These tutorials may seem like they are assuming too much knowledge at this point, but you will eventually see that this is by design, and that anything not covered and assumed will be covered in tutorials yet to be written or published. The goal is to have a series of related and dependant tutorials, but at this point I am not writing them in order. The tutorial page will always have the tutorials listed in the intended reading order.

Posted June 18th, 2009 in PHP, Tutorials

View Comments to “Introduction to PHP: Hello World”

  1. Introduction to PHP: Hello World + MySQL « Sorrowful Unfounded Says:

    [...] Contact « Introduction to PHP: Hello World [...]

Leave a Reply

blog comments powered by Disqus