MasterPHP.in
PHP Tutorial

Install PHP and Run Your First Program


Why You Need a Local Setup Before Writing PHP

PHP is a server-side language, which means it needs a server environment to run — you can't just open a .php file in your browser like you would an HTML file and expect it to work. Before writing any real PHP code, you need three things working together on your computer: PHP itself, a web server (like Apache), and usually a database (like MySQL), since most real PHP applications eventually need to store data.

Setting these up individually can be tedious, which is why most beginners use a bundled package that installs all three at once.


Choosing a Local Server Environment

There are a few popular options for setting up PHP locally:

  • XAMPP — the most widely used option, available for Windows, macOS, and Linux, bundling Apache, MySQL, and PHP together.
  • WAMP — a Windows-only alternative similar to XAMPP.
  • MAMP — a macOS-focused equivalent.
  • Laravel Herd / Valet — lightweight options popular with developers already working in PHP frameworks.

For beginners, XAMPP is usually the easiest starting point because it works the same way across operating systems and includes a simple control panel to start and stop your server.


Installing XAMPP: The Core Steps

At a high level, getting XAMPP running involves:

  1. Downloading the installer for your operating system from the official Apache Friends website.
  2. Running the installer and selecting the components you need (Apache and PHP are essential; MySQL if you plan to work with databases).
  3. Launching the XAMPP Control Panel and starting the Apache module.
  4. Placing your project files inside the htdocs folder, which is the default location XAMPP serves files from.
  5. Accessing your project through http://localhost/your-folder-name in your browser.

We've published a complete, screenshot-by-screenshot walkthrough of this exact process if you want to follow along visually while installing:

👉 Install XAMPP and create your first PHP program


Writing Your First PHP Program

Once your server is running, create a new file inside the htdocs folder and name it index.php. Open it in any code editor (VS Code is a popular free choice) and add the following:


php

<!DOCTYPE html>
<html>
<body>

<?php
    echo "Hello, PHP! Your first program is working.";
?>

</body>
</html>

Save the file, make sure Apache is running in your XAMPP control panel, and open http://localhost/index.php (or whichever folder name you used) in your browser. You should see the message printed on the page.


Understanding What Just Happened

A few things are worth pointing out about this simple example:

  • The <?php and ?> tags tell the server where PHP code begins and ends.
  • The echo statement is how PHP outputs text to the browser.
  • The file extension .php is what tells the server to process the file as PHP rather than serving it as plain HTML.
  • If Apache isn't running, or the file isn't inside htdocs, the browser won't be able to find or execute it — this is the most common issue beginners run into.

Verifying PHP is Installed Correctly

A useful trick to confirm your setup is working is to create a file with the following single line:


php

<?php phpinfo(); ?>

Opening this file in your browser displays a detailed page showing your installed PHP version, configuration settings, and enabled extensions. This is a quick way to verify everything is set up correctly before moving on to writing actual application logic.


What's Next

  • With PHP installed and your first script running successfully, you're ready to start learning the language itself — beginning with variables, data types, and how PHP stores and manages information.

Frequently Asked Questions

No. XAMPP bundles PHP along with Apache and MySQL, so installing XAMPP automatically installs a working version of PHP — there's no separate PHP installation step needed for local development.
This usually means Apache isn't running, or the file isn't placed inside the htdocs folder. Browsers can't execute PHP directly — the file must be processed by a running server first.
VS Code works perfectly well for PHP development, especially with extensions like PHP Intelephense for autocomplete and error checking. You don't need specialized software to get started.
XAMPP (or a similar local environment) is mainly for local development and testing. In production, PHP applications run on a properly configured live server, but the coding concepts you learn locally apply directly to real-world projects.

Share this tutorial