All Tech Guides

Tech Guide

Install PHP and Build Your First Page

An introduction to PHP, how to install it on Windows, macOS, and Linux, an overview of the language, and a beginner web page example using PHP's built-in server.

Programming Beginner Updated October 2026

Disclaimer & Important Cautions

These Tech Guides and instructions are provided in good faith as a general educational guide. Technology changes over time, and settings, software versions, installation steps, and security practices may vary by system.

I cannot guarantee that these steps will work in every environment, and I am not responsible for data loss, configuration problems, system errors, security issues, downtime, or other problems that may result from following this guide.

  • Download PHP only from php.net, windows.php.net, or your operating system's package manager.
  • Use administrator privileges carefully, and back up settings before editing PATH or php.ini.
  • PHP's built-in web server is for development only. Never use it to host a public website.
  • Always escape user input before displaying it, as shown in the example, to prevent cross-site scripting (XSS).
  • When in doubt, consult official documentation or a qualified technician.

Overview

PHP (a recursive acronym for PHP: Hypertext Preprocessor) is a free, open-source scripting language designed for building dynamic websites. PHP code runs on the web server: the server executes the PHP and sends plain HTML to the visitor's browser. PHP was created by Rasmus Lerdorf in 1994 and powers a large share of the web, including WordPress, Wikipedia, and many custom sites. This website is built with PHP.

The current version is PHP 8.5, with PHP 8.6 scheduled for late 2026. In this guide you will install PHP, learn the basics of the language, and build a small interactive page.

Main idea: PHP runs on the server and produces HTML. For learning, you can install PHP by itself and use its built-in development web server; no separate web server is required.

Requirements

  • A Windows, macOS, or Linux computer.
  • Administrator privileges to install software.
  • Internet access.
  • A code editor, such as Visual Studio Code.
  • A web browser.

Important Notes

  • If you want PHP to run under the IIS web server on Windows, follow the IIS + PHP guide instead of the Windows tab below.
  • PHP files must be opened through a web server (for example http://localhost:8000). Opening a .php file directly from your folder shows the code instead of running it.

PHP Installation Instructions

Step 1: Check whether PHP is already installed

Open a command line window (Command Prompt or PowerShell on Windows, Terminal on Mac and Linux) and enter:

php -v

If you see a version number of 8.3 or higher, you can skip to the Language Overview. Otherwise, install PHP below.

Choose the tab for your operating system:

Installing PHP

Click the tab above for your operating system.

The guide covers these major tasks:

  1. Download or install PHP.
  2. Add PHP to your PATH (Windows).
  3. Create a php.ini file (Windows).
  4. Confirm the installation with php -v.

Step 2: Confirm the installation

In a new command line window, enter:

php -v

You should see output beginning with something like:

PHP 8.5.x (cli) ...
Check yourself: php -v prints a PHP 8 version number.

PHP Language Overview

PHP code goes inside <?php ... ?> tags. Anything outside the tags is sent to the browser as-is, so you can mix PHP with HTML.

FeatureExampleNotes
Outputecho "Hello";Sends text or HTML to the browser.
Variables$name = "Ana"; $age = 20;Start with $. Types (string, int, float, bool) are set by the value.
Strings"Hi, $name"   'Hi, ' . $nameDouble quotes insert variables; . joins strings.
Arrays$colors = ["red", "blue"];
$user = ["name" => "Ana", "age" => 20];
Indexed lists and key–value (associative) arrays.
Conditionsif ($age >= 18) { ... } else { ... }Also elseif, match, and switch.
Loopsforeach ($colors as $c) { echo $c; }Also for and while.
Functionsfunction add(int $a, int $b): int { return $a + $b; }Type declarations are optional but recommended.
Form input$_GET["name"], $_POST["name"]Built-in arrays holding data sent by forms and URLs.
Comments// one line   /* several lines */Ignored when the code runs.

PHP also includes built-in support for databases (through PDO, for MySQL, SQLite, SQL Server, and others), sessions, cookies, file handling, and JSON.

Beginner Example: A Greeting Page

You will build a page with a form. When a visitor enters a name, PHP greets them and shows a short list.

Step 1: Create a project folder and file

Create a folder named php-practice (for example, in Documents). Inside it, create a file named index.php with this code:

<?php
    // Read the name from the form, if one was submitted
    $name = trim($_GET["name"] ?? "");

    // Escape the name so any HTML a visitor types is shown as text
    $safeName = htmlspecialchars($name, ENT_QUOTES, "UTF-8");

    $languages = ["PHP", "SQL", "HTML", "CSS"];
    $hour = (int) date("G");
    $greeting = match (true) {
        $hour < 12 => "Good morning",
        $hour < 18 => "Good afternoon",
        default    => "Good evening",
    };
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>My First PHP Page</title>
</head>
<body>
  <h1>My First PHP Page</h1>

  <form method="get">
    <label>Your name: <input type="text" name="name"></label>
    <button type="submit">Say hello</button>
  </form>

  <?php if ($safeName !== ""): ?>
    <p><?php echo "$greeting, $safeName!"; ?></p>
    <p>Your name has <?php echo strlen($name); ?> characters.</p>
  <?php endif; ?>

  <h2>Languages used on the web</h2>
  <ul>
    <?php foreach ($languages as $language): ?>
      <li><?php echo $language; ?></li>
    <?php endforeach; ?>
  </ul>

  <p>This page was generated at <?php echo date("g:i a"); ?>.</p>
</body>
</html>

Step 2: Start PHP's built-in web server

In a command line window, move into the folder and start the server:

cd Documents/php-practice
php -S localhost:8000

Leave this window open. The server keeps running and logs each request.

Step 3: View the page

  1. Open a browser and go to http://localhost:8000.
  2. Type your name and click Say hello. The page reloads with a greeting, and your name appears in the address bar as ?name=....
  3. In the browser, choose View Page Source. You see only HTML, because PHP ran on the server before the page was sent.
  4. When you are finished, return to the command line and press Ctrl + C to stop the server.
Check yourself: Enter <b>Test</b> as your name. The page should display the tags as text instead of bold text, which shows that htmlspecialchars is protecting the page.

Common Errors

  • The browser shows your PHP code: you opened the file directly. Use http://localhost:8000 instead.
  • Address already in use: another program is using port 8000. Try php -S localhost:8080 and browse to that port.
  • Parse error: syntax error: usually a missing semicolon, quote, or brace. The message includes the line number.

Next Steps

Final Notes:
  • PHP runs on the server and sends HTML to the browser.
  • Confirm your installation with php -v.
  • Use php -S localhost:8000 for development, and view pages through http://localhost.
  • Always escape user input with htmlspecialchars before displaying it.
Explore more technology resources

Return to the complete collection of installation and configuration guides.

Browse All Tech Guides