Yes…”Hello World!’
For those of you who have either programmed in other language(s) or have read a programming book before, you’re aware of this INITIATION RITE.
For the benefit of those who have never written a program before, I’ll explain what this is:
A “Hello World” program is simply a program that displays a “Hello World” message to the user (or programmer) in the simplest way possible in the Language (i.e. programming language which in this case is PHP)
Since it’s practically the simplest program possible, it lets you take in the structure and general rules for the specific language without having to go into too many details.
Introduction
I hope you’ve installed an appropriate editor (see PHP Getting Started – Setting Up the Environment).
Using a very good editor is essential especially for newbies, because it reduces the stress of you having to watch your code for different situations that arise (e.g syntax highlighting, method highlighting e.t.c).
Now on to the real thing; to actually run PHP code, it needs to live in a particular folder on the server (Recall we installed WAMP Server from the Getting Started Tutorial). So you’ll have to follow the steps below to know where and how to use your installed local server:
- Locate the install directory for your WAMP installation (for Windows usually C:\wamp — Lets call this WAMP_INSTALL_DIR); for Ubuntu users, this step is not necessary
- Then locate the folder named www inside of it, so you’re finally in the directoryWAMP_INSTALL_DIR\www; for Ubuntu users just browse nautilus to /var/www on Ubuntu.
- This is your web root, It’s called web root because this is where you’ll place all files (i.e. html, css ,javascript, php e.t.c that will be viewable in the browser).
PLEASE NOTE: I’ll assume you are a Windows user for all tutorials following this.
Let’s Start
The next step is to start your editor, just double-click the icon for your IDE (Integrated development Environment), If you remember i suggested one of either:
- Netbeans
- Eclipse ; or
- Dreamweaver check the Getting Started tutorial for more information
Every editor has it’s own way but usually one common thing among all of them is the ability to create projects (a project is simply a new website) but we’ll start with something simple.
Create a new file in your web root (hope you still remember where that is?
just kidding… ). Depending on your IDE, you’ll follow different paths although an easy way is to press Ctrl+N on your keyboard which opens a new file or a new file dialog (all you have to do is make sure you save it in your web root); feel free to give it any name e.g hello-world.php
Make sure to add a .php at the end of the file name else the server won’t recognize it as a PHP file (more on this at a later time).
Writing our Code
The next step after all the above is to write the code into the file, simply type the following:
<?php
echo "Hello World";
?>
- Press Ctrl+S to save the contents (the code) of the file.
- Open you browser
- Make sure that WAMP Server is running on your computer, you can look at your system tray to confirm or you can simply double-click the wamp icon on your desktop to start it

- Browse to http://localhost/hello-world.php or http://127.0.0.1/hello-world.php
Code Explanation
Explanations will be made on a line-by-line basis:
- We have <?php ; if you have had knowledge of html, its called a tag (more precisely an opening tag); it denotes the beginning of a block, in our case it denotes the beginning of a PHP code block.
- Next is the line echo “Hello World”; This line contains something in programming called a statement (more on this later), All echo does is to print to the page any piece of text enclosed in quotes (“) immediately after it. (Hint: try changing the Hello World to something else). Please also note that the statement ends in a semicolon (;) every statement in PHP must end in this
- Finally we have ?>; its another tag. It’s actually the closing tag (Rule 1: every opening tag must have a closing tag). So one can place any amount of code in between the PHP open and close tag
There you have it. A running albeit simplistic PHP program.
As you can see, with this very simple example, we’ve talked about the
- PHP tag,
- a function called echo, and
- the importance of semicolons
See you in the next tutorial (don’t forget to pick up a good PHP book and go through it and feel free to ask questions on whatever you read in the forum. Start new threads and generally have fun).
Popularity: 9% [?]