The most notorious piece of code for any budding programmer to know is the script that produces “Hello World!” on your screen.
What is a “Hello World!” program and why should you know how to write it? “Hello World” is always the first program that is introduced to a programmer coming into any particular language. This script was chosen because it entails all of the most basic syntax to writing in that language. There are many different versions of where the firstofficial use of it came from, but no one can seem to pin-point the exact place or time. The general consensus is that it came from a book on an introduction to C, but that it was in use long before that. It has become a tradition, and why break tradition.
To execute this script, you will need to locate your home folder, or document root as it is usually called. If you followed the default installation of a package, the document root will be in the following location:
- LAMP: document root – /var/www
- MAMP: document root – /volumes/www
- WAMP: document root – /wamp/www
- XAMPP: document root – /xampp/htdocs
Once there, create a new directory called “test”. Inside of your new directory, create a new file called “hello.php” with your text editor of choice; gedit on Ubuntu, or Notepad on Windows. The extension “.php” tells the server that the file contains PHP code, and to be expecting it. Just because it has a .php extension doesn’t necessarily mean it will have PHP code in it, but if it does, and does not have a .php extension, the code will not work. So always remember that if a file contains PHP, to give it a .php extension.
Now, open “hello.php” and copy the following lines into it.
<?php echo "Hello World!"; ?>
Now that you’ve copied the code, save it. Make sure that your server is running, and open your favorite browser. In the address bar of your browser type the following:
You should see the words “Hello World!” appear on your screen. Congratulations, you’ve done it.
Okay, so lets break it down.
<?php” and “
?>” are called open and closed tags, respectively. They tell the server that PHP code is incoming, and will be placed inside of these tags. You should be aware that there are “short tags” in case you ever come into contact with them during a project:
<?= are both opening short tags and they are both followed by the same closing tag
It is strongly discouraged to use “short tags” because it will be deprecated in the next version of PHP (PHP 6), and because it is not guaranteed that your hosting service will allow them. While the option is available in most cases, others it will not. So sticking to long tags (
<?php) only makes sense. It makes the most sense for portability reasons, such as changing hosting services. One host may allow them, but another may not, in which case you will have to scour your code to change them all to long tags if short tags are off.
echo” tells the server to output the following information to the screen. When you use echo, it will print whatever is between the quotes to the screen. So you don’t necessarily have to use “Hello World!”, you could put whatever text you would like between the open and closing quotes and it will be printed on the screen. Go ahead and try it.
; after “Hello World!” closes the statement. It tells the server that you are done executing this statement and that it can move on to the next statement, or in this case, the closing tag.
So there you have it, the “Hello World!” script for PHP.
If you are interested in a library containing “Hello World!” examples in many other languages, check out this collection: The Hello World Collection. This could be useful to bookmark if you find yourself working with many different languages.