Writing your first PHP script

Now that you know how to use XAMPP Lite, it’s time to write your first PHP script.

This tutorial will guide you on writing your first PHP script, show you how the PHP syntax is used, and where to put and run your PHP pages in XAMPP.

Note: this tutorial assumes you are using XAMPP Lite environment for your PHP scripts. If you want to setup your own XAMPP Lite environment, follow this tutorial about installing XAMPP Lite on a portable device.

Finding your Web Directory

Before you start writing, you need to locate your web directory. This is a special directory within your web server (in our case XAMPP Lite) where all the webpages are located.

When you type a URL into your browser, you are requesting for a file. This file has to be located within the web directory for the web server to deliver it to you. As such, if you have a PHP script, you need to put it into this web directory in order for it to be available to be browsed by your browser.

In our installation of XAMPP Lite, go to the your XAMPP Lite install directory. In that directory, you should see a folder called ‘htdocs’. This is your web directory.

XAMPP Web Directory

Open this directory, and you should see something like this, with some images files, a couple of folders, some image files, one HTML file and one PHP file. For the moment, just ignore them.

XAMPP htdocs directory

Create your PHP file

The next thing you need to do is to create a file within this web directory. So go ahead an do that, and name the file ‘hello.php’, and put the following source codes into the page.

1
2
3
4
5
6
7
8
9
10
<html>
<head>
<title>My First PHP Page</title>
</head>
</body>
<?php 
print ('Hello. How do you find PHP so far?'); 
?>
</body>
</html>

After this, execute the page by going to http://localhost/hello.php in your browser. You should get something like this.

Browser Hello ouput

Well, congratulations, you’ve successfully run your first PHP page.

A Closer Look

So, what exactly happened? If you refer back to the source code, Lines 1-5 and 9, 10 are just your usual HTML tags.

Line 6, 7, 8 is where the PHP stuff is happening. Line 6 and 8 is the start and end delimiter that tells XAMPP web server where the PHP scripts starts and where it ends. This is refered to as a PHP processing block.

1
2
3
<?php
/* put your PHP script between these delimiters */
?>

Line 7 is where the PHP code is.

1
2
3
<?php
print ('Hello. How do you find PHP so far?'); 
?>

In this case, it’s telling XAMPP to output the text ‘Hello. How do you find PHP so far?’.

So when you run the page at http://localhost/hello.php, that’s exactly what XAMPP does – print out the text.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Propeller
  • Reddit
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
Posted on January 15, 2008 at 3:52 pm by Eldee · Permalink
In: PHP Tutorials · Tagged with: , ,

10 Responses

Subscribe to comments via RSS

  1. Written by Sandip on August 26, 2008 at 6:00 pm
    Permalink

    A Good One.

  2. Written by Tamasys on September 18, 2008 at 9:46 am
    Permalink

    Thanks, this was useful.

  3. Written by bram on September 26, 2008 at 10:17 pm
    Permalink

    wew ! this is good for newbie…

    webmaster, do you want put my link in this blog ?

    bramzzz.blogspot.com

    and i will put your link in my blog.

    tq

  4. Written by manas on November 19, 2008 at 5:25 pm
    Permalink

    was very useful to start with php coding.
    thanks

  5. Written by suresh on November 20, 2008 at 4:10 pm
    Permalink

    thanks for giving this informatin it very usefull.

  6. Written by Mukul on January 31, 2009 at 1:37 am
    Permalink

    Yes a very good tutorial….thanks a ton…

  7. Written by tbb on June 23, 2009 at 5:27 am
    Permalink

    Good intro to PHP. Thanks!

  8. Written by Aemz on October 30, 2009 at 7:52 pm
    Permalink

    Nice Information..

    Thanks Alot…

  9. Written by danilo on January 21, 2010 at 4:02 am
    Permalink

    thanks! i didn’t know how to run php with xampp lite and this tutorial really worked here.

  10. Written by K shanu on February 18, 2010 at 11:00 pm
    Permalink

    thanx,,,,,,
    it help me to run php

Subscribe to comments via RSS

Leave a Reply