• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Services
    • WordPress Websites
    • WordPress Maintenance Package
    • WordPress Security Package
    • WordPress Turbo Tune‑Up
    • For Designers & Agencies
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / PHP / Writing Your First PHP Script

Writing Your First PHP Script

11 May 2009 / 2 Comments

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.

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: 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.

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:

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:

Running your first PHP script

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.

Filed Under: PHP Tagged With: beginner, document root, echo, first script, getting started, hello world, php, PHP tags, semicolon, text editor, troubleshooting, XAMPP

Reader Interactions

Comments

  1. gopiwisen says

    16 May 2013 at 8:17 am

    Very understandable article

    Reply
  2. chrishirst says

    18 May 2013 at 3:57 pm

    You’re a few days late for the thread’s fourth birthday link drop.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2022 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits