Lesson 2 of 50 – Installing PHP
4%

Installing PHP

Before writing PHP programs, you need to install PHP or use a local development environment that includes PHP. In this lesson, you will learn how to install PHP, configure a local server, and run your first PHP program.

Note: For beginners, XAMPP is a convenient option because it bundles Apache, PHP, and MariaDB. You can also install PHP separately and use another supported web server.
What is Required to Run PHP?

PHP code must be processed by a PHP interpreter. To run PHP web pages locally, you generally need the following:

  • PHP: Executes PHP code.
  • Web Server: Apache or another compatible server handles browser requests.
  • Code Editor: Visual Studio Code or another text editor helps you write PHP code.
  • Web Browser: Chrome, Firefox, Edge, or another browser displays the output.
  • Database (optional): MySQL or MariaDB can be used for database-driven applications.

A database is not required for every PHP program. You can start learning PHP with simple examples that display text.

Installing PHP Using XAMPP

XAMPP is a local development package that includes Apache, PHP, MariaDB, and additional tools.

Installation Steps:
  1. Visit the official Apache Friends website.
  2. Open the XAMPP download page.
  3. Download the installer for your operating system.
  4. Run the installer and follow the setup instructions.
  5. Choose the components you need, including Apache and PHP.
  6. Complete the installation.
  7. Open the XAMPP Control Panel.
  8. Start the Apache module.

Official download website:

https://www.apachefriends.org/

Important: If Apache fails to start, another program may be using its port. Check the error messages and resolve the port conflict before continuing.
Understanding the htdocs Folder

In a standard Windows XAMPP installation, the htdocs folder is the default document root for Apache.

Typical folder location:

C:\xampp\htdocs\

Create a folder named Php_Tutorials inside htdocs if you want to run your tutorial files from that folder.

Example Folder Structure:
C:\xampp\htdocs\
    |
    └── Php_Tutorials\
          |
          ├── php_intro.php
          ├── php_installation.php
          └── first.php

The exact installation path may differ depending on where you installed XAMPP.

Running Your First PHP File

Follow these steps to execute a PHP program using XAMPP.

  1. Start Apache from the XAMPP Control Panel.
  2. Open your htdocs folder.
  3. Create a file named first.php inside your project folder.
  4. Write the PHP code shown below.
  5. Save the file.
  6. Open your browser and visit the local URL.
Example Code:
<?php
echo "PHP is installed successfully!";
?>
Browser URL:
http://localhost/Php_Tutorials/first.php
Expected Output:
PHP is installed successfully!

If the message appears, your local PHP environment is working.

Checking the PHP Version

You can check which PHP version is available from the command line by running the following command in Command Prompt or a terminal where PHP is configured in the system PATH.

Command:
php -v

The command displays the installed PHP version and other build information.

Example Output:
PHP 8.x.x (cli)
Copyright (c) The PHP Group

The version number shown on your computer may be different. If the command is not recognized, PHP may not be installed separately or its directory may not be configured in your system PATH.

Installing PHP Separately

You can also install PHP without using XAMPP. This approach is useful if you want to learn PHP from the command line or configure your own development environment.

  • Download PHP from the official PHP website.
  • Choose a build compatible with your operating system.
  • Extract or install PHP according to the official instructions.
  • Configure the PHP executable in your system PATH if needed.
  • Open a terminal and run php -v.

Official PHP website:

https://www.php.net/downloads.php

Remember: Installing PHP alone does not automatically configure Apache. For web applications, you need a compatible web server or PHP's built-in development server.
Running PHP from the Command Line

PHP can execute scripts directly from the terminal without requiring Apache.

Step 1: Create a file named hello.php
<?php
echo "Hello from PHP CLI!";
?>
Step 2: Open the terminal in the file's folder.
Step 3: Run the command.
php hello.php
Output:
Hello from PHP CLI!

The PHP CLI (Command Line Interface) is useful for learning, testing scripts, automation, and running command-line applications.

Common PHP Installation Problems
Problem Possible Solution
Apache does not start Check the error log and resolve port conflicts.
PHP code appears as text Open the page through a configured PHP server, not directly as a local file.
php is not recognized Check the PHP installation and system PATH.
404 Not Found Check the file name and its location under the server's document root.
Blank page or server error Check the PHP and web server error logs.
Key Points
  • PHP requires a PHP interpreter to execute PHP code.
  • XAMPP provides a convenient local development environment.
  • Apache can serve PHP pages when configured with PHP.
  • The htdocs folder is the default document root in a standard XAMPP setup.
  • PHP files commonly use the .php extension.
  • The command php -v displays the PHP CLI version.
  • The command php hello.php runs a PHP script from the terminal.
  • PHP can be installed separately or through a development package.

🧠 Quick Quiz

1. Which package provides Apache and PHP together for local development?




2. What is the default document root folder in a standard Windows XAMPP installation?




3. Which command displays the PHP CLI version?