To begin Programming in PHP for Web Development, the very first question you'll face is:
How to install PHP and run PHP Program (any CODE written in
.php file) on your own Computer? What's the easiest way?
A "Program" is the result of many lines of "CODE" & since PHP is a Scripting Language, CODE written in PHP files are sometimes called PHP "Scripts". These are all interchangable words, so don't get confused by these terms.
Few short notes before you install & run PHP:
The easiest way to install PHP & run PHP Program or any CODE written in
*.phpfile on your own computer is using a software bundle like XAMPP.
There are other similar software bundles like MAMP, WAMP etc. that can install and run PHP files, but:
XAMPP works well in all the major Operating Systems like Windows, Mac and Linux the same way. So XAMPP is easier to start with.
To run PHP program for web development, you also need a web server like Apache, and a database server like MySQL or MariaDB. You may install these separately on your own computer, however, it is often difficult for beginners to install these tools individually and combine them to run PHP files. XAMPP does all these for you with just a few clicks.
Note: If Apache, MySQL or PHP is already installed on your computer, then before installing XAMPP, you may have to uninstall the existing Apache, MySQL and PHP installations, especially if they were installed as service. It's possible to keep multiple installations, but that's for more advanced users.
OK, enough with the initial description, now follow the steps below to run your first PHP CODE:
How to Install PHP:
- Download XAMPP from here and then double click the downloaded installer file to begin XAMPP installation.
- Just follow the instructions from the installer and you'll be done installing PHP within a few clicks.
8 Simple Steps to Run PHP Program or CODE written in .php files:
- After the installation of XAMPP is successful, open any CODE Editor.
Install a new CODE Editor if you don't have one installed on your computer. My Favorite is Visual Studio CODE (for Windows, Mac & Linux).
- Write the following PHP Program / CODE in the CODE Editor of your choice:
<?PHP echo 'I know how to run a PHP Program in XAMPP! <br />'; ?> This is test.php File.
- Save the file in
XAMPP Installation Directory\
Web Root Directory
Note-1: Default XAMPP Installation Directory in Windows is
Note-2: Default Web Root Directory in XAMPP is
htdocs. All your php files will have to be in this
That means, for a typical installation of XAMPP on Windows, you will have to save the PHP file in
- When you save the file, name it
test.phpis just an example, any valid file name with
.phpin the end will work).
Note: when saving this file, make sure it has no
.txtextension at the end. Some Editors place
.txtat the end of the file name, so it becomes
test.php. To avoid this, when you save the file using any Editor, place double quote around the file name: e.g.
- Then, go to XAMPP installation folder (typically,
C:\xampp) and run
xampp-control.exeby double clicking it.
Note: if your Operating System hides common file extensions, then you'll see
xampp-control, instead of
- In the
xampp-controlwindow, click the start button beside Apache. Later, if you need other options like MySQL, you'll also have to start MySQL by clicking the start button beside MySQL in the XAMPP control Panel.
- Now open your favorite web browser (Firefox, Google Chrome etc.) and enter this LINK:
http://localhost/test.phpin the address bar of your web browser.
This Link must match the file name you've used to save the PHP CODE in step-6 above. So instead of
test.php, if you've saved the PHP file as
example.php, then your Link should be:
- If your PHP installation and the test PHP file you've just written is working properly, then you should see the following two lines on your browser:
I know how to run a PHP Program in XAMPP!
This is test.php File.
Now you know how to run PHP CODE or any
*.phpfile on your own computer using XAMPP. May be it is time to learn some proper web development with PHP!
PHP Learning Resources:
Best way to learn a programming language is by doing. While you can learn from manuals, books, online articles etc. it's often easier & cost effective to learn a new programming language from online video courses. Here's some PHP learning resources I like:
- PHP Online Course → PHP for Beginners
This is an updated & beginner friendly online video course on web development with PHP. With this course you'll learn PHP & how to create dynamic PHP websites along with a content management system project.
- Another Online PHP Course → Learn PHP Programming From Scratch
Another decent paid video course on web development with PHP.
- Reference Manual → Official PHP Manual
This is the most comprehensive guide written on PHP, read the manual and go through the comments within the manual if you can. It's a bit difficult and time consuming compared to video courses, but if you want to master PHP, you'll need to at least keep it for reference.
Happy Coding 😊
Troubleshoot PHP installation
Uh, Houston, we've had a problem!
If the above instructions worked for you & you can run your PHP Scripts just fine, then you don't need to read this section, however, if you didn't get the expected result from the above instructions, don't panic, we've all been there. To get you started with what's going on:
If PHP is NOT working but the web server Apache is working, in that case only the following one line result will appear:
This is test.php File.
If you are getting an error instead, then Google for that particular error text and with any luck, you'll receive tons of results out there.
Also, check out the comments here, other people may have already faced similar issues, and your solution may just be in the comments as well.