Install PHP & Run Your First Script

PHP · Lesson 2 of 50 9 min read Updated

PHP code needs PHP installed to run. In this lesson you will set up PHP on your own computer, check the version, run your first script in the terminal and open your first PHP page in a browser.

Pick the section for your operating system, then follow the common steps after it. The whole setup takes about 20 minutes.

What you need

You need three things on your computer:

  • PHP itself, the program that reads and runs .php files.
  • A web server to serve pages to your browser. PHP has a small one built in, which is enough for learning. Packages like XAMPP also include Apache.
  • A code editor. This course uses Visual Studio Code (VS Code), which is free.

Later, when you reach Connecting to MySQL with PDO, you will also need a database server. The Windows packages below include one.

Windows option 1: XAMPP

XAMPP is a free package that installs Apache, PHP and a database in one go. Many Indian colleges use it in labs.

  1. Download XAMPP for Windows from the official site, apachefriends.org.
  2. Run the installer and keep the default folder C:\xampp.
  3. Open the XAMPP Control Panel and click Start next to Apache. Start MySQL too when you need a database.
  4. Create the folder C:\xampp\htdocs\learn and put your .php files inside it.
  5. Open http://localhost/learn/ in your browser.

Two things to know about XAMPP:

  • XAMPP releases can lag behind PHP releases, so it may bundle an older PHP version than 8.5. Check the version as shown below. Most of this course works on PHP 8.2 and newer, and lessons point out features that need 8.4 or 8.5.
  • The "MySQL" button in XAMPP actually starts MariaDB, a database that began as a copy of MySQL. It is very close to MySQL, but not identical, so a few newer MySQL features behave differently.

If Apache will not start, another program is probably using port 80. Close it, or change the port in Apache's settings.

Windows option 2: Laragon

Laragon is another free all-in-one package for Windows. It is lighter than XAMPP and makes it easy to switch between PHP versions. Put your projects in C:\laragon\www. Laragon can give each project folder a friendly local address, such as http://learn.test.

Whichever package you choose, add its PHP folder to your Windows Path so that the php command works in any terminal. For XAMPP that folder is C:\xampp\php. Search the Start menu for "environment variables", open Edit the system environment variables, click Environment Variables, select Path, click New and paste the folder. Close and reopen your terminal afterwards.

macOS and Linux

On macOS, install Homebrew first (from brew.sh), then run:

Terminal
brew install php

On Ubuntu or Debian Linux, install PHP and some common extensions with apt:

Terminal
sudo apt update
sudo apt install php-cli php-mbstring php-xml php-curl php-mysql

Linux distributions package one PHP version for each release, and it may be older than 8.5. That is fine for learning. If you need the newest version, look for an up-to-date package source for your distribution.

Check your PHP version

Open a terminal (PowerShell or Command Prompt on Windows, Terminal on macOS and Linux) and run:

Terminal
php -v

The first line tells you the version. Your numbers will be different, but it will look something like this:

Text
PHP 8.5.x (cli) (built: ...)
Copyright (c) The PHP Group

If you see "php is not recognized" or "command not found", PHP is not on your Path. Repeat the Path step above and open a new terminal. On Windows, where.exe php shows which php.exe the terminal finds, which helps when you have more than one PHP installed.

Run your first script in the terminal

Create a folder called learn, and inside it a file called hello.php:

PHP
<?php
echo "Namaste! PHP is working.\n";
echo "5 + 7 = " . (5 + 7) . "\n";

In the terminal, go to that folder with cd and run the file:

Terminal
php hello.php
Output
Namaste! PHP is working.
5 + 7 = 12

This is called running PHP from the command line (CLI). Most examples in this course are short scripts like this, and the Output boxes show what the terminal prints.

Run a PHP page in the browser

PHP includes a small built-in web server for development. In the learn folder, create index.php:

PHP
<?php
$city = 'Pune';
$temperature = 31;
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Weather</title>
</head>
<body>
  <h1>Weather in <?= $city ?></h1>
  <p>It is <?= $temperature ?>°C right now.</p>
</body>
</html>

Then start the server from the same folder:

Terminal
php -S localhost:8000

Open http://localhost:8000 in your browser. You will see the heading "Weather in Pune" and the sentence "It is 31°C right now." Press Ctrl+C in the terminal to stop the server. If you view the page source in the browser, you will find only HTML:

Output
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Weather</title>
</head>
<body>
  <h1>Weather in Pune</h1>
  <p>It is 31°C right now.</p>
</body>
</html>

If you use XAMPP or Laragon, you can skip php -S and open the file through Apache instead, for example http://localhost/learn/.

Set up VS Code

  1. Install VS Code from code.visualstudio.com.
  2. Open the Extensions panel and install PHP Intelephense. It adds autocomplete, error highlighting and "go to definition" for PHP.
  3. Use File > Open Folder to open your learn folder.
  4. Open the built-in terminal from the Terminal > New Terminal menu and run php hello.php there.

Working with a whole folder open, rather than single files, lets the editor understand how your files connect.

See your PHP settings

PHP reads its settings from a file called php.ini. Run php --ini to see where it is. To see every setting and loaded extension in the browser, create a file with one line:

PHP
<?php phpinfo();

Open it through your server and you will see a long table of settings, including the PHP version at the top. Delete this file when you are done. Never leave a phpinfo() page on a live website, because it tells attackers a lot about your server.

Common mistakes

  • Opening the file with a file:/// address. The browser just shows the code. Always use http://localhost/....
  • Saving hello.php.txt by accident. Windows hides known extensions by default. Turn on File name extensions in File Explorer's View menu.
  • Editing php.ini and seeing no change. Apache reads php.ini only when it starts. Restart Apache after changing it.
  • Two PHP versions fighting. If php -v shows a different version from your browser page, the terminal and Apache are using different PHP installs. Check with where.exe php.

Key points

  • On Windows, XAMPP or Laragon installs PHP, a web server and a database together.
  • XAMPP may include an older PHP and uses MariaDB, a close relative of MySQL.
  • On macOS use Homebrew (brew install php); on Ubuntu or Debian use apt.
  • php -v shows your version, and php file.php runs a script in the terminal.
  • php -S localhost:8000 starts a development server in the current folder.
  • Use VS Code with the PHP Intelephense extension, and open whole folders.

Exercise

Install PHP using the method for your system and run php -v. Then create about.php that prints your name, your city and the number of days in four weeks (work it out with 4 * 7), each on its own line. Run it in the terminal, then start the built-in server and open it in the browser.

Show solution
PHP
<?php
echo "Name: Rohan\n";
echo "City: Lucknow\n";
echo "Days in four weeks: " . (4 * 7) . "\n";
Output
Name: Rohan
City: Lucknow
Days in four weeks: 28

In the browser the three lines appear side by side, because browsers ignore \n line breaks in HTML. You will learn how to output proper HTML in the next lessons.

Quick Quiz

1. Which command shows the installed PHP version?

2. What does php -S localhost:8000 do?

3. When you click "Start" next to MySQL in XAMPP, which database actually runs?

4. Why should you delete a phpinfo() page?

Learn PHP with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call