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.
PHP code must be processed by a PHP interpreter. To run PHP web pages locally, you generally need the following:
A database is not required for every PHP program. You can start learning PHP with simple examples that display text.
XAMPP is a local development package that includes Apache, PHP, MariaDB, and additional tools.
Official download website:
https://www.apachefriends.org/
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.
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.
Follow these steps to execute a PHP program using XAMPP.
htdocs folder.first.php inside your project folder.<?php
echo "PHP is installed successfully!";
?>
http://localhost/Php_Tutorials/first.php
If the message appears, your local PHP environment is working.
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.
php -v
The command displays the installed PHP version and other build information.
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.
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.
php -v.Official PHP website:
https://www.php.net/downloads.php
PHP can execute scripts directly from the terminal without requiring Apache.
<?php
echo "Hello from PHP CLI!";
?>
php hello.php
The PHP CLI (Command Line Interface) is useful for learning, testing scripts, automation, and running command-line applications.
| 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. |
php -v displays the PHP CLI version.php hello.php runs a PHP script from the terminal.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?