To begin Programming in PHP or to check an existing PHP site or script on your own computer, the very first question you'll face is:
How to run PHP CODE or any File ending with .php
extension on My Own Computer? What's the easiest way?
A PHP Program is the result of many lines of PHP CODE & often includes many PHP files. Also, since PHP is a Scripting Language, CODE written in PHP files are often called PHP Scripts. These are all interchangeable words. So don't get confused by these terms.
A Short Introduction:
The easiest way to install & run PHP on your own computer is using a software like XAMPP.
There are other similar software bundles like MAMP, WAMP etc. that can install & run PHP, but:
XAMPP works well in all the major Operating Systems like Windows, Mac & Linux the same way. So XAMPP is easier for beginners.
PHP can be used for different purposes, however, the most popular use of PHP is as the server side programming language for web development. Naturally, to run PHP program for web development, you also need a web server like Apache Web Server, 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 programs. XAMPP does all these for you with just a few clicks.
OK, enough with the introduction, 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.
How to run PHP Program:
- 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 (works on Windows, Mac & Linux).
Examples of other popular CODE Editors are: Sublime Text (works on Windows, Mac & Linux), Notepad++ (works on Windows only) etc.
- Write the following PHP 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 isC:\xampp
Note-2: Default Web Root Directory in XAMPP ishtdocs
. All your php files will have to be in thishtdocs
folder.That means, for a typical installation of XAMPP on Windows, you will have to save the PHP files in
C:\xampp\htdocs
folder. - When you save the file, name it
test.php
(test.php
is just an example, any valid file name with.php
in the end will work).Note: when saving this file, make sure it has no
.txt
extension at the end. Some Editors place.txt
at the end of the file name, so it becomestest.php.txt
instead oftest.php
. To avoid this, when you save the file using any Editor, place double quote around the file name: e.g."test.php"
- Then, go to XAMPP installation folder (typically,
C:\xampp
) and runxampp-control.exe
Note: if your Operating System hides common file extensions, then you'll see
xampp-control
, instead ofxampp-control.exe
- In the
xampp-control
window, click the start button beside Apache.
Later, if you need database, you'll also have to start MySQL by clicking the start button beside MySQL in
xampp-control
. - Now open your favorite web browser like Firefox, Google Chrome etc. (on the same computer where you've just installed PHP) & enter this LINK:
http://localhost/test.php
in the browser's address bar (or simply click http://localhost/test.php if you are reading this post on the same computer where you've just installed XAMPP & running PHP).
Two things to note here:
- On the web, localhost means your own computer.
- The test 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 asexample.php
, then your Link should be:
http://localhost/example.php
- 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.Congratulation! ✌️
Now you know how to run PHP CODE or file ending with
*.php
extension on your own computer using XAMPP. Perhaps it is time to learn some proper web development with PHP!
PHP Learning Resources:
The best way to learn programming is by doing a lot of practice. I recommend the following resources for beginners:
- PHP for Beginners: This is a well organized & beginner friendly online PHP learning course. While you can learn PHP from articles & tutorials here & there, they often lack completeness. That's why it's far easier & more effective to begin the learning process from a course like this.
- PHP Manual: Bookmark this official PHP manual. Reading the manual is not the easiest way to begin your PHP learning process, but you'll always need this as a 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, then only the following one line result will appear:
This is test.php File.
If you are getting an error instead, then check out the comments here or Google for that particular error text & with any luck, you'll receive tons of results.
Note: If Apache, MySQL or PHP was 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.
thanks! it was everything I was looking for to understand what the heck with PHP. (Installation, small example and how to execute it).
I've been trying since yesterday and I still get the 404 error. Apache is running on port 81. The test file is in htdocs and I put "test.php" when I saved the file, but still. 😔 Help guys.
The code inside the file is the same as the one you said we should try.
If Apache is running on any other port than 80, you must provide that port (after colon) in URL.
In your case, since the port is 81, your URL should be: http://localhost:81/test.php
Awesome post!
It worked well. Thanks
A/S fayaz miraz bhai.I was trying for the first time and thanx it really worked
Guy u 're a bro mhen!
a miracle is what thou(you) are.
thanks a lot.
thank you bro........
yaah!! Thanks a bunch it worked!
Thank you very much.
Thanks! It is a very useful to me.
Hi, Thanks for this article, but i am having issue with Apache. Skype / Outook will be always running in my system. Please see what wrong is happening here.
6:34:56 PM [Apache] Attempting to start Apache app...
6:34:57 PM [Apache] Status change detected: running
6:34:57 PM [Apache] Status change detected: stopped
6:34:57 PM [Apache] Error: Apache shutdown unexpectedly.
6:34:57 PM [Apache] This may be due to a blocked port, missing dependencies,
6:34:57 PM [Apache] improper privileges, a crash, or a shutdown by another method.
6:34:57 PM [Apache] Press the Logs button to view error logs and check
6:34:57 PM [Apache] the Windows Event Viewer for more clues
6:34:57 PM [Apache] If you need more help, copy and post this
6:34:57 PM [Apache] entire log window on the forums
6:35:01 PM [Apache] Problem detected!
6:35:01 PM [Apache] Port 80 in use by "Unable to open process" with PID 4!
6:35:01 PM [Apache] Apache WILL NOT start without the configured ports free!
6:35:01 PM [Apache] You need to uninstall/disable/reconfigure the blocking application
6:35:01 PM [Apache] or reconfigure Apache and the Control Panel to listen on a different port
Change the port of Skype (from settings) from port 80 to some other port, say 443.
I got the answer . Thank you
Thanks a lot.
It has helped me.
Thank you!!!Its worked for me.Happy to see my first output in php:):):)
thanks it really worked!!!!
Error 404
localhost
Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.13
plz help
I Need some details before I can answer:
1. What is the name of your PHP file/script?
2. Where did you place this file (full path of the file)?
3. Copy the exact PHP CODE
4. What did you enter in your browser address bar?