Install PHP And Write Your First PHP Program

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated December 3, 2024
Edited by Kamila

Edited by Kamila

Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.

A simple step-by-step guide on how to Install PHP using the XAMPP package. Understand all about the PHP installation process and quickly learn how to write your First PHP Program easily:

In this tutorial, you will learn how to install PHP, the AMP package, the advantages of XAMPP, and the XAMPP installation process. You will also see how to write your first PHP program, PHP editors, PHP online compilers, and frequently asked questions.

Please note that we have used PHP version 7 in all examples in this tutorial.

Let’s begin!

=> Complete PHP Guide for ALL

PHP Installation and Configuration Guide

PHP Installation

Components to Run PHP Program

You need three major components. They are:

#1) Web server

The following list shows the web servers that support PHP:

  • Apache HTTP Server (popularly known as Apache)
  • Microsoft’s Internet Information Server (popularly known as Microsoft IIS)
  • Nginx

The below table shows a comparison between the above three web servers:

ParameterApacheMicrosoft IISNginx
DeveloperApache Software FoundationMicrosoftNginx, Inc.
Released in199519952004
Written inC and XMLC++C
Supported operating systems•Unix-like
•Microsoft Windows
•OpenVMS

Windows NT•BSD variants
•HP-UX
•IBM AIX
•Linux
•macOS
•Solaris
•Microsoft Windows
•Other *nix flavors

#2) PHP

#3) Database

The following list shows the databases that can connect with PHP:

  • MySQL – MySQL is the most popular database used with PHP
  • PostgreSQL
  • IBM DB2 Database
  • Oracle Database
  • MongoDB
  • SQLite3
  • Ingres
  • Paradox
  • DB++
  • dBase
  • filePro
  • CUBRID
  • Firebird and InterBase
  • FrontBase
  • SQLSRV (Microsoft SQL Server Driver for PHP)
  • Tokyo Tyrant

The below table shows a comparison between five popular databases:

ParameterMySQLPostgreSQLIBM Db2OracleMongoDB
TypeRelational database management system (RDBMS)Relational database management system (RDBMS)Relational database management system (RDBMS)Multi-model databaseDocument-oriented database
DeveloperOracle CorporationPostgreSQL Global Development GroupInternational Business Machines Corporation (IBM)Oracle CorporationMongoDB Inc.
Released in19951996199319792009
Written inC and C++CC, C++, Assembly and JavaAssembly, C and C++C++, JavaScript and Python
Supported operating systems•Linux
•Solaris
•macOS
•Windows
•FreeBSD
•macOS
•Windows
•Linux
•FreeBSD
•OpenBSD
•Linux
•Unix-like
•Windows
•macOS
•Windows
•Linux
•Unix
•Solaris
•Windows Vista and later
•Linux
•OS X 10.7 and later
•Solaris
•FreeBSD

How to Install PHP

Installation Options

There are several options to download PHP and install it on your machine. One method is to install the above components manually. It is commonly referred to as manual installation. However, it will take more time and effort.

Another method is to install using an all-in-one package, such as an AMP package. It will take less time and effort as you don’t need to install the required components individually. Instead, it will install all the required components at once.

AMP Package

We recommended you to install PHP using an AMP package.

It is an acronym for:

  • A: Apache server
  • M: MySQL
  • P: PHP

An AMP package consists of three major components. They are:

  1. Apache as the Web Server
  2. MySQL as the database
  3. PHP

Some AMP packages such as XAMPP contain a Perl folder that helps to run server-side code written in Perl.

There are many AMP packages available. The below list shows some of the common AMP packages. They are also referred to as PHP server hosts.

  • WAMP: For Windows operating system.
  • MAMP: For Mac OS (operating system).
  • LAMP: For Linux operating system.
  • XAMPP: For cross-platform (Windows, Mac OS, and Linux operating systems).

XAMPP Package

In this tutorial, we are using the XAMPP package. It is an acronym for:

  • X: Cross-platform
  • A: Apache
  • M: MySQL
  • P: PHP
  • P: Perl

Advantages of XAMPP

The below list shows some of the advantages of XAMPP:

  • Free and open-source.
  • Available for many operating systems (OS) such as Windows, Mac OS, and Linux.
  • Need less time and effort to install.
  • Can easily manage configuration changes.
  • Easy to use.

Installation of XAMPP

Here is the step-by-step guide to install XAMPP on your computer:

Note: Sometimes, you may get errors while installing the XAMPP. Therefore, you need to carefully follow these steps and you may also need to fix errors. One of the easiest ways to fix errors for a beginner is to “Google” the error and find out solutions. Then, you will realize that you are not the only individual who gets those errors.

Step 1: Visit the Apache Friends website to download the XAMPP installer.

Step 2: Carefully choose the version you want to download based on your operating system. Then, download and install XAMPP. After installing XAMPP, you will get a screen similar to the screenshot shown in Step 3 (please see below). It is called the XAMPP control panel.

Step 3: If the control panel is ready (please see the below screenshot), you can start the services. Otherwise, you need to fix errors, before starting the services.

xampp control panel

Step 4: Click on the Start buttons relevant to Apache and MySQL (we only need to start Apache and MySQL) as shown in the below screenshot.

Click on the Start buttons relevant to Apache and MySQL

If the services (Apache and MySQL) are successfully running, you will see a green background as shown in the below screenshot. Also, observe the status shown at the bottom part of the control panel.

Otherwise you will see a red background. In that case, you need to fix the error.

Apache and MySQL are successfully running

That’s it! You are ready to write your first PHP program.

Step 5: To access the database (we are using phpmyadmin). Open your browser and go to the relevant URL.

If you are using your local server (localhost), the access URL will be similar to follows.

localhost/phpmyadmin (NOT http://localhost/phpmyadmin/)

Note: The above URL may vary depending on the server and configuration.

If phpmyadmin is successfully working, you will get a window similar to the below screenshot.

phpmyadmin is successfully working

Write Your First PHP Program

You need a PHP editor to write a program. It can be a text editor like Notepad or an Integrated Development Environment (IDE) like PhpStorm, Sublime Text, or Visual Studio Code.

Note: A text editor is just to write and edit code (or text). An IDE is a software application that integrates several tools like a code editor, builds automation tools, and a debugger.

Suggested Reading => Top PHP Online Editors and IDEs

Example: Follow the below steps to run your first PHP program.

Step 1: Open any text editor or IDE and write the following code.

<?php 
echo "Hello World!";
?>

Step 2: Then, Save the file by giving the .php extension. You can give any name to your file.

Example: test.php

Step 3: Next, open the file in your web browser.

Example: By typing the URL: localhost/test.php.

Note: The above URL may vary depending on the server, configuration, and file name.

The below screenshot shows the browser output of the above programming code.

browser output

PHP Online Compilers and Editors

You can also use online compilers and editors to run PHP programs. There are several free online compilers and editors available.

Examples:

The following list shows some of the websites that provide online compilers and editors to run PHP programs:

Frequently Asked Questions

In this section, we will discuss some of the FAQs. These questions will help you to prepare for your examinations and interviews confidently.

1. Does PHP need to be installed?

Yes, it needs to be installed.

2. Where can I download PHP?

You can download it on the official website or using a server stack like XAMPP.

3. Do I need to install PHP after XAMPP?

No, you don’t. XAMPP has PHP.

4. Can PHP work without a server?

Yes, it can work without a server. In such situations, you need the PHP parser.

5. Can I use Notepad for PHP?

Yes, you can use Notepad as the code editor.

6. How do I run a PHP site locally?

By running it on localhost.

Conclusion

There are several methods to install PHP on your computer but using an AMP package is an easy way to do it.

A PHP file is a plain text file. So, it can open using any text editor like Notepad or using a PHP IDE like Visual Studio Code. You can also use online compilers to run PHP programs.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment