Running your first PHP script on Windows using XAMPP, in your localhost

This article helps you to run your first PHP script successfully on Windows from your local host. It should be useful for PHP novices on Windows.  Before dive in to PHP, lets set up an environment to run PHP on Windows.
Yes, If you use PHP on Windows then you are not only on Win environment but on Win - Win Environment. You will feel this after you run your first PHP script on windows. OK,let's get into action.
Code attached with this is verified and has been run successfully on Windows XP, Windows 7, Windows VISTA on all commonly-used browsers.


Download XAMPP,  an opensource package that holds Apache, PHP, MySQL, PERL (very old but powerful) which makes our work easier. Read more here (I mean the installation and configuration of PHP, Apache and MySQL on windows).  Download XAMPP self extracting RAR archive .EXE file which in turn is an easy way rather than a zipped archive. Install it on your top level folder / directory of any drive Eg : C:\xampp or D:\xampp. The instructions on the XAMPP web page are very clear with good screenshots.

A few words about XAMPP ingredients,  
Apache - A powerful and popular Web server to run server side scripts like PHP, ASP, JSP, CFML etc. Read More

PHP - A simple, flexible, powerful and easy to learn and get help server side scripting language with huge community support. Read More

MySQL - An open source RDBMS (database management system) with great set of features and very compatible with PHP. Read More

PERL -  A 22 year-old powerful scripting language -- perhaps a pioneer of PHP or server side scripting.   A person with the basics of 'C' and 'UNIX' finds it easier to work with.Read More


Now just open your folder where you have installed XAMPP and run the xampp control panel by running
inside your xampp folder.        
If you find the running... label highlighted in green color next to Apache and MySql then it is time to script your first PHP program, and our environment is ready.  If not, just click the check boxes near to Apache and MySQL to install them as Windows services and click start button.  (Wait for a while, don't lose your temper -- don't click the start button again and again which you more likely to do as a beginner.)

At this point, some students get struck -- they find Apache / xampp not running on localhost even after this.  Most of the times it is because the IIS (Microsoft's web server) runs by default and occupied the HTTP port  : 80, which Apache demands by default. There are two ways to resolve this.

The easy way:
Type services.msc in the run window.  In other words Windows Key + R and type
and click OK.  Find the IIS admin in the list of services shown in the window below.  If you find its status 'started' and startup type 'automatic' then right click on IIS admin and select Properties option in the pop up screen and stop the IIS service and change its startup type to disabled from the drop down list.  And click OK.
ServicesNow find Apache in the same list of services right click on it start the service and change its property as default start up type as 'automatic'. Now click xampp-control.exe inside your xampp folder and start the MySql service.  That's all.

OR.... the other way
The next way is to open 'httpd.conf' in the following location YOURDRIVE:\xampp\apache\conf It is ia simple text file.   Change 'Listen 80' to
   Listen 81
or Listen 8085 or the port number you wish (but don't use the reserved port numbers).  
Httpd Configuration FileAnd restart your Apache and MySQL.

You can follow either of these two options as per your wish.  I don't recommend or insist that you choose only one of them rather than the other one.


So we have successfully installed xampp and now it's time for PHP.

i. Open notepad and copy paste the following PHP code into it or just use the attached file and copy it in htdocs folder inside xampp (eg:c:\\xampp\htdocs\).
                          echo "<center><b>Kudos !!! You have successfully run your first PHP scrip on Windows</b> :-)</center>";
                          echo "<br/><center><b>Welcome to <h2>PHP</h2> the language of web</b></center>";
                          echo "<br/><center>For any PHP queries Contact :<a href=''>haijerome</a></center> ";

Open in new window

ii Save the file as "haiphp.php" into htdocs folder of xampp server (eg: c:\\xampp\htdocs\) and select save as type "All Files".
All FilesIf you don't save your file type as 'all files' then you won't get your PHP script running. Just because of this small issue, I have got a number of emails in my inbox and believe it or not, it forced me to write this article.  And I have to thank this issue, because i have got  a quite number of clients who turned into my friends later, all because of this little issue. So save you file type as 'All Files'

And my kind suggestion is to use Notepad++ or if you want to be very professional then download Net Beans or buy Dream Weaver.        

iii. Now type the url "http://localhost/haiphp.php" and see the PHP script running like a cake on your localhost web browser.

If you have followed the step ii of step 2 then type the URL like this http://localhost:81/haiphp.php assumed that your port number is 81.  If not, give the port number that you have saved as listen port number in the httpd.conf file.
PHP scriptIf you get any page not found or object not found or 404 error, then try with this http://localhost/xampp and conform whether you're getting the xampp home page.  If it displays the XAMPP home page then verify that you have saved your PHP file inside htdocs folder and the file name as haiphp.php and file type as 'all files'.

If you don't even get the xampp home page, then make sure Apache is running.  Follow the main step 2 given in the article above to get xampp or apache running on your  localhost to resolve object not found or object not found error no : 404.
I am not a professional article writer (am just a budding article writer but am a good article reader, so kindly don't mind about anything that you found very awkward or blunder in this article. If any, kindly let me know.  I hope this article is a welcome one for all the PHP beginners on windows and fortunately becomes the very first article on this zone.  Let me have your thoughts, comments, suggestions, and queries.  And if you find it useful then kindly click on the helpful YES button to vote this one. Cheers EE :-). All Glory Goes to GOD !!!

Best Regards,
Jerome Dennis D

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.