Writing Your First PHP Script
Learn how to get started with PHP programming. This beginner tutorial walks you through the process of writing a simple PHP script and shows how PHP scripts are put together.
In What is PHP? you took an overall look at the PHP programming language, and what you can use it for. In this tutorial, you get to play with PHP and create your very first PHP script. Along the way you'll learn some important basic concepts of PHP.
What you'll need
In order to start writing PHP scripts, you need access to a Web server running PHP. Your main options are:
- Run PHP on your own computer: The easiest way to do this is to install a complete package like XAMPP. This contains the Apache Web server, along with PHP and the MySQL database engine, in one easy-to-install package. XAMPP is available for Windows, Mac OS X, and Linux. (A popular alternative on Windows is WampServer.)
- Run your PHP scripts on your Web host: If you already have a Web hosting account that supports PHP then you can upload your PHP scripts via FTP and run them on the Web server. The advantage of this approach is that you don't have to install anything; the disadvantage is that it's slower to write and test your scripts.
Your first script
Here's the PHP script that you're going to create:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>My first PHP script</title> </head> <body> <p><?php echo "Hello, world!"; ?></p> </body> </html>
As you can see, most of this script is plain XHTML. The PHP code is inside the
<p><?php echo "Hello, world!"; ?></p>
?> tags tell the Web server to treat everything inside the tags as PHP code to run. (Everything outside these tags is sent straight to the browser as-is.)
This line of code is very simple. It uses a built-in function,
echo, to display some text ("Hello, world!") in the Web page. PHP contains hundreds of functions that you can use to write feature-rich applications.
Notice the semicolon (
;) after the PHP code — you need to put a semicolon after each line of code in your PHP scripts.
The semicolon is optional if you're only writing one line of code, as in this example.
Creating the script
To create your script, you'll need to use a text editor program. Most computers come with one or more text editors built in — for example:
- Notepad on Windows
- vi or Emacs on Linux
- TextEdit on Mac OS X
Copy and paste the script code listed above into a new document in your text editor, then save the file as
hello.php in the document root folder — that is, the top level of your website — on your hard drive. If you're not sure where your document root folder is then consult your Web server manual. Common locations include:
- XAMPP on Windows:
- XAMPP on Linux:
- XAMPP on Mac OS X:
If you want to run the script on your Web hosting account rather than your own computer then you'll need to upload the script using FTP instead.
Find out more about the document root, as well as uploading files, in Getting started with your Web hosting service.
Testing the script
Now you're ready to run your script. To do this, open a Web browser, type the script's URL into the browser's address bar, and press Enter. If you're running the Web server on your own computer then the URL will probably be:
If, on the other hand, you're running the script on a Web hosting account then you need to use something like:
If all goes well then you should see a page similar to this:
If you get an error message or nothing happens, check that your Web server is set up properly (see the Web server's documentation for help) and that you entered the script code correctly.
If you see the script code displayed in the browser, rather than the expected Web page, or your browser offers to download the script file, then your Web server has not been configured to run PHP scripts.
If you're still stuck then check out the many PHP support options available.
If you saw the correct Web page then congratulations — you've successfully written, saved, and run your first PHP script! You'll build on these skills in future tutorials as you learn how to write more complex PHP scripts and applications.
You might also like to browse the PHP language reference to get some ideas for other PHP scripts to write.
Learn PHP With Ease!
Written by Matt Doyle — ELATED's resident Web programming expert — Beginning PHP 5.3 is a complete introduction to PHP, covering everything in these tutorials and lots more besides. Find out how to:
...and lots more!
“What a pleasure it's been spending hours and hours studying PHP with this magical book.” — Lulio, Florida
“The book is not only great for learning, but I find myself using it constantly as a reference as well!” — David A. Stoltz
Responses to this article
2 responses (oldest first):
Post a response
Want to add a comment, or ask a question about this article? Post a response.
To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.