Let's dive in. You'll need:
- A web host that supports PHP (most do)
- S/FTP access to your host (see our FTP Made Simple tutorial)
- A plain text or programmer's editor. See the What You Need section of our HTML 101 tutorial for recommendations
- Basic knowledge of HTML
From this point on, we'll assume that you know how to upload documents to a directory (folder) on your web host, how to use your text editor, and have basic competency in HTML.
The basic process works like this:
- Start with a simple HTML document
- Name the document with a .php extension instead of .html
- Upload it to a location on the web and visit that URL
- Add some PHP code to the document, re-upload, and refresh the browser
Remember: Because PHP is a server-side programming language, you can't simply save a .php document to your desktop, open it up in a browser, and expect to see anything meaningful. You must place all of the exercises in this tutorial on a real web server and view them over the web.
Tip: If you really want to do PHP development on your desktop, it's possible to install a real web server on your personal computer. If you're a Mac user, note that Mac OS comes with Apache and PHP already installed - you just need to configure it. If you're a Windows user, you can install WAMP to get Apache, PHP, and MySQL database working under Windows.
For the rest of this tutorial, we'll assume you're placing your files on a real web host via S/FTP. Let's start with this simple XHTML document:
Even though there's no PHP code in it, save that code to a file called
hello.php and upload it to your web server, in a folder called
phptest. Now find the URL in a browser. It should be something like
http://example.com/phptest/hello.php (adjust to match your domain and path).
Now let's add some PHP to the document. PHP is simply interleaved with existing HTML code wherever necessary, using PHP opening and closing markers, like this:
So let's introduce the classic "Hello world!" code into our document.
Save and upload the document, overwriting the hello.php file on the server, then click Refresh in your browser. Kind of underwhelming, wasn't it? That's OK - we're just getting our sea legs. The important thing right now is to understand how you were able to switch out of HTML mode, into PHP mode, and back into HTML mode, all in the middle of the same document. You'll be doing lots of that.
The line between the opening and closing PHP tags is called a "statement," which is the smallest standalone element of a programming language. Simply put, a statement does a unit of work (in this case, "echoing" something back to the screen.) In PHP, statements always end with a semi-colon -- without it, your code will not work, and you'll get a nasty error message back from the server.
We'll come back to the
date() line later in the tutorial. For now, the objective is to understand how PHP code can be interleaved with HTML.
Note: This tutorial shows and explains example code, but does not show the output of that code. You'll need to put each sample in a PHP file on a web server to see its output. There's no better way to learn than to work with actual code yourself.
Commenting your code
All programming languages let you add "comments" to code - sections of text that are meant to be read by the programmer but ignored by the interpreter. Adding comments to code may seem silly when you're working with "Hello world!," but it becomes essential when your codebase becomes large and complex - especially if multiple people are working on it. Plan to comment your code any time its purpose isn't 100% obvious. In PHP, comments can take two forms:
So far so good - you've got a working PHP document! Let's liven things up a bit with dynamic data.