
Image: Simplilearn
By the end of this article, you will have a working local web server running on your machine, a PHP file saved in the right place, and the confidence to execute server-side code in your browser – without touching a live server or paying for hosting. That is the payoff: a private development sandbox you control entirely.
Here is what trips up almost every beginner: PHP cannot run in your browser the way HTML or JavaScript can. Open an HTML file in Chrome and it renders immediately. Try the same with a .php file and you get a blank screen or raw code – because PHP is a server-side scripting language. It needs a web server to interpret it before anything reaches the browser. XAMPP gives you that server, running locally on your own machine.
Prerequisites

Image: Simplilearn
Before you begin, make sure you have the following:
- A Windows, Linux, or macOS computer (XAMPP runs on all three)
- Administrator access to install software
- A text editor – Visual Studio Code is an excellent free choice
- No prior PHP experience needed, but basic familiarity with HTML is helpful
What Is XAMPP and Why Use It to Run PHP Files?
XAMPP is the fastest way to get a PHP environment running locally – it bundles everything you need into a single installer. The name is an acronym: X for cross-platform, A for Apache, M for MariaDB, P for PHP, and P for Perl. It is maintained by Apache Friends under the GNU General Public Licence, meaning it is completely free to download and use.
The alternative on Windows is WampServer, but XAMPP has a decisive advantage – it works identically on Windows, Linux, and macOS, so your workflow does not change if you switch machines. For serious developers, that cross-platform consistency is worth a great deal.
How to Install XAMPP
Install XAMPP before writing a single line of PHP – the server must exist before you can run anything through it.
Step 1: Download XAMPP
Go to the official download page at apachefriends.org/download.html. Choose the installer that matches your operating system. The download page lists which PHP version is bundled – note this can occasionally lag behind the very latest PHP release, which matters if you need cutting-edge language features.
Step 2: Run the installer
Double-click the downloaded installer. If you are on Windows, accept any User Account Control prompts. Work through the setup wizard and leave the default installation directory (C:\xampp on Windows) unless you have a specific reason to change it.
Common mistake: Antivirus software occasionally blocks the XAMPP installer or flags files during extraction. Temporarily disable real-time scanning during installation, then re-enable it immediately afterwards.
Step 3: Launch the XAMPP Control Panel
Open the XAMPP Control Panel from your Start menu or Applications folder. You will see a list of services: Apache, MySQL, FileZilla, Mercury, and Tomcat.
Step 4: Start Apache
Click Start next to Apache. Within a few seconds the status should turn green and display port numbers – typically 80 and 443.
If you see a port conflict error, it means another application – often Skype or IIS on Windows – is already using port 80. Stop that application, or configure Apache to use a different port. To change the port, click the Config button next to Apache in the Control Panel, open httpd.conf, and find the line Listen 80 – change it to Listen 8080, save the file, and restart Apache. You will then access your files at http://localhost:8080/ instead of http://localhost/. Every URL in the steps below that starts with http://localhost/ should have :8080 added after localhost for the rest of your session.
To verify Apache is running, open your browser and navigate to http://localhost (or http://localhost:8080 if you changed the port). You should see the XAMPP welcome dashboard – a page with the Apache Friends logo and language selection links. If you see that page, Apache is serving files correctly.
How to Create and Run a PHP File in XAMPP
Place your PHP files inside XAMPP’s htdocs folder – any file outside this directory will not be served by Apache.
The htdocs folder is XAMPP’s document root. Apache looks here for files to serve, just as a traditional web server looks in its public_html directory. Saving your file anywhere else – such as your Desktop or Documents folder – will produce a 404 error or serve raw PHP code instead of executing it.
Step 5: Locate the htdocs folder
On Windows: C:\xampp\htdocs\
On macOS: /Applications/XAMPP/htdocs/
On Linux: /opt/lampp/htdocs/
Step 6: Create a project folder
Inside htdocs, create a new folder called myproject. Keeping each project in its own folder prevents files from clashing and mirrors how real web projects are organised.
Step 7: Write your first PHP file
Open your text editor and create a new file called index.php inside myproject. Add the following code:
<?php
echo "<h1>Hello from PHP!</h1>";
echo "<p>The current date and time is: " . date('d/m/Y H:i:s') . "</p>";
?>
Save the file. The .php extension tells Apache to pass the file through the PHP interpreter before sending the response to the browser.
Step 8: Run the PHP file in your browser
Open your browser and go to:
http://localhost/myproject/index.php
Expected output: A page with the heading “Hello from PHP!” and a line below it showing today’s date and the current time in DD/MM/YYYY HH:MM:SS format. If you see both lines, your local PHP environment is fully operational.
If you see a blank white page, PHP encountered an error but error display is turned off. Add the following two lines immediately after the opening <?php tag:
ini_set('display_errors', 1);
error_reporting(E_ALL);
Reload the page. PHP will now print the error directly in the browser – typically a message like Parse error: syntax error, unexpected token followed by a file path and line number. Fix the error on that line, remove the two ini_set lines when you are done debugging, and reload again.
If you see the raw PHP source code – the literal text <?php echo ... – rather than any rendered output, Apache is not interpreting the file. This almost always means the Apache service is not running. Check the XAMPP Control Panel and confirm Apache shows a green status. If it stopped, click Start and reload.
Turning on Error Display Permanently During Development
Chasing blank pages one file at a time is slow. A faster approach is to enable error display globally in your local php.ini. Open the XAMPP Control Panel, click the Config button next to Apache, and select PHP (php.ini). Find the line:
display_errors = Off
Change it to:
display_errors = On
Also find error_reporting and set it to:
error_reporting = E_ALL
Save the file and restart Apache from the Control Panel. From this point, all PHP errors on your local machine will display in the browser automatically – which is exactly what you want during development. Never set display_errors = On on a production server, as it can expose internal code paths to the public.
Understanding the PHP File Structure
Every PHP script opens with <?php and closes with ?> – these tags tell the Apache module where PHP code begins and ends.
PHP files use the .php extension and can contain a mixture of HTML and PHP code, which makes it straightforward to build dynamic pages without a separate templating language.
Here is a slightly more advanced example that demonstrates a variable and a conditional:
<?php
$username = "Alex";
$hour = date('H');
if ($hour < 12) {
$greeting = "Good morning";
} else {
$greeting = "Good afternoon";
}
echo "<p>" . $greeting . ", " . $username . "!</p>";
?>
Save this as greeting.php in your myproject folder and visit http://localhost/myproject/greeting.php. Expected output: A line that reads either “Good morning, Alex!” or “Good afternoon, Alex!” depending on the current time. That dynamic behaviour – output that changes based on server-side logic – is exactly what makes PHP valuable for web development. PHP can also create, read, and write files on the server, interact with databases (MariaDB is already bundled in XAMPP), restrict access to pages, and encrypt data. All of these are capabilities you can begin exploring once your local environment is working.
Next Steps
Now that you can run PHP files locally, here is where to go next:
- Connect to MariaDB – Start the MySQL service in the XAMPP Control Panel and use
phpMyAdminathttp://localhost/phpmyadminto create your first database. - Learn PHP variables and functions – The official PHP documentation is authoritative and well-indexed.
- Explore PHP frameworks – Laravel and Symfony implement the Model-View-Controller pattern on top of PHP. PHP itself does not enforce any particular architecture; frameworks add that structure.
- Automate your dev workflow – Pair XAMPP with terminal CLI tools to run scripts, manage dependencies with Composer, and lint your code from the command line.
- Write tests for your code – When your PHP project grows, look into testing frameworks; for browser-level testing across a full stack, the concepts in Playwright with TypeScript translate well to understanding automated testing principles.
If you are building something more ambitious and want professional guidance, the team at drs-web.co.uk/contact can help you architect and deliver your PHP project from local prototype to live deployment.
Frequently Asked Questions
Q: Why can’t I run a PHP file by double-clicking it?
A: PHP is a server-side language, not a client-side one. Double-clicking opens the file in a browser as plain text. You need Apache (provided by XAMPP) to interpret the PHP before sending output to the browser – which is why accessing your file via http://localhost/... is essential.
Q: Where exactly do PHP files go in XAMPP?
A: All PHP files must be placed inside the htdocs folder within your XAMPP installation directory (C:\xampp\htdocs\ on Windows). Files outside this folder are not served by Apache and cannot be executed.
Q: XAMPP says port 80 is already in use – what should I do?
A: Another application is occupying port 80. Common culprits on Windows are Skype, IIS, or World Wide Web Publishing Service. Either stop the conflicting application or change Apache’s port to 8080 via the XAMPP Control Panel Config button, then access your files at http://localhost:8080/ instead.
Q: Is XAMPP safe to leave running all the time?
A: For local development it is fine, but XAMPP is not hardened for production use. Keep it stopped when you are not actively developing, and never expose your XAMPP ports to the public internet without additional security configuration.
Source: https://www.simplilearn.com/tutorials/php-tutorial/php-using-xampp
This article was researched and written with AI assistance, then reviewed for accuracy and quality. Kev Parker uses AI tools to help produce content faster while maintaining editorial standards.
Need help with your web project?
From one-day launches to full-scale builds, DRS Web Development delivers modern, fast websites.




