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 <?php
and ?>
tags:
<p><?php echo "Hello, world!"; ?></p>
The <?php
and ?>
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.
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:
C:/Program Files/xampp/htdocs/
- XAMPP on Linux:
/opt/lampp/htdocs/
- XAMPP on Mac OS X:
/Applications/XAMPP/htdocs/
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.
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:
http://localhost/hello.php
If, on the other hand, you’re running the script on a Web hosting account then you need to use something like:
http://www.example.com/hello.php
If all goes well then you should see a page similar to this:

Problems?
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.
What next?
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.
Very understandable article
You’re a few days late for the thread’s fourth birthday link drop.