PHP Tutorial 2 – Creating Your First Web Page Using PHP (Hello World)
Ladies and gentlemen, welcome to PHP tutorial 2! In our last tutorial, we covered setting up your server environment. In this tutorial, we are going to go over creating your first web page using PHP.
With all standard server environments, there is a folder called “htdocs” where you put the actual files that make up your website. This is where you put your PHP files. For MAMP, this folder will be in the MAMP folder that you put in your Applications folder earlier. For Mac users of XAMPP, this folder will be in the XAMPP folder that is in your Applications folder. For Windows users of MAMP, this will be in the XAMPP folder on your hard drive. For Linux users of XAMPP, this folder will be in your XAMPP folder. Found the htdocs folder? Good.
If you open the htdocs folder, you should see an “index.php” file (among others). This is the default file that is run when a user goes to your URL (think of it as the home page). The server runs the PHP code in it, and the browser renders the HTML that the server sends it (as output of the PHP code).
Before we start editing it, we will need a PHP editor. We’re not going to be working on a ny major projects any time soon, so a simple, free one will suite the purpose just fine. For If you are on a Mac, I suggest you download a neat application called Komodo Edit (http://www.activestate.com/komodo-edit). This program is also available for Windows, but Notepad++ (http://notepad-plus-plus.org/)is also a great option.
Now that you have an editor, let’s start coding. First, open the index.php file that is in the htdocs folder (on Macintosh, you can right click index.php, choose “open with”, and navigate to your editor; on Windows, you can go to File -> Open in your editor and choose index.php). Next, replace all the code that you see with the following:
<title>My Cool PHP Page</title>
echo "Hello World!";
To view the result, save the PHP document, start your Apache server, and go to http://localhost .
You should already understand the HTML, and we won’t be going over that. You should, however, take note that a PHP document is really a HTML document with special code. What you probably aren’t familiar with is the chunk of code that starts with “”. This is the PHP code. When working with PHP, you have to enclose the code with the tags “” (for closing the section). As for the ” echo ‘Hello World!’; “, this will output “Hello World!” to the HTML document. “Echo” is a language construct that behaves like a function. It is used to output strings of text to the HTML document. The text that you want outputted must be in parenthesis, although it will work with both single and double parenthesis. You may want to use one or the other based on what you are outputting. For example, if I wanted to output double quotations, I would want to use single quotations (otherwise the computer would get confused and think I were ending my string of text in the middle of it). And that is your standard “Hello World!” in PHP!
Leave a Comment