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

22 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

  11. Written by Luckman Raharja on April 27, 2010 at 3:37 pm
    Permalink

    Hello there
    Can I ask ..
    How to use only <?
    instead of <?php.

    Best Regard,

    Luckman R thank you

  12. Written by webmaster on April 28, 2010 at 9:24 pm
    Permalink

    @Luckman

    By default, your PHP install should support the short tag. If it does not, you can enable it in by changing the short_open_tag value in php.ini.

    For more information, see the description for ’short_open_tag’ at this page http://php.net/manual/en/ini.core.php

  13. Written by yogesh sharma on May 21, 2010 at 10:07 am
    Permalink

    thanx man,for past 2 days i m searching for how i run my php pogram and you just solve my problem.

  14. Written by yogesh sharma on May 23, 2010 at 7:59 pm
    Permalink

    How can I make the documentation of an WEBSITE running in PHP?

  15. Written by Jake on May 30, 2010 at 9:15 am
    Permalink

    Thanks man, very helpful. By the way, line five of the code has a typo in it!

  16. Written by johnhelloworld on June 6, 2010 at 9:22 am
    Permalink

    thanks…… a lot…

  17. Written by harsh on May 2, 2011 at 2:30 am
    Permalink

    thnx… keep it up…

  18. Written by Steve2688 on June 26, 2011 at 11:48 pm
    Permalink

    i would like to change my script placing path such like…
    lets say i create a scripting name it as start.php and this script is inside a folder name “project” can i put my directory in “C:\xampp\project”, if can, then did i need to setup a httpd.conf?

  19. Written by Rahul Rana on June 30, 2011 at 2:20 am
    Permalink

    thax a lot to askaboutphp…..very useful

  20. Written by Sonal on July 6, 2011 at 1:39 pm
    Permalink

    Thanx for dis…it really helped me out!!!!!

  21. Written by AJ on September 26, 2011 at 4:53 pm
    Permalink

    hello

    the date function doesn’t works in my php. it doesn’t return any value please help me. i have tried all the syntax.

  22. Written by jj on September 27, 2011 at 2:36 pm
    Permalink

    please tell me how to load the list of items from mysql table to php combobox?

Subscribe to comments via RSS

Leave a Reply