Developing PHP applications locally allows developers to work on projects without affecting live websites. It provides a controlled environment where changes can be tested and debugged before deployment. in this step-by-step tutorial guide you will learn how to setup your PHP development environment on your Windows system.
Let’s begin
Setting up a PHP development environment on Windows involves installing a web server, PHP itself, and optionally a database server if your application requires it.
There are basically two ways to install these three basic things – one is Install MySQL, PHP, and Apache manually, and configure them and the other one is utilize all-in-one packages like XAMPP or WAMP. (recommended).
What is XAMPP?
XAMPP is an acronym that stands for Cross-platform, Apache, MySQL, PHP, and Perl. It is a free and open-source web server solution stack package developed by Apache Friends. XAMPP facilitates the setup of a local web server environment on your Windows machine, making it ideal for PHP development.
Setting up a PHP development environment on Windows using XAMPP is a popular and straightforward method. XAMPP is an easy-to-install Apache distribution that contains MySQL, PHP, and Perl. Here’s how you can set it up:
Setup your local PHP development environment Step-by-step
1. Download XAMPP:
Go to the https://www.apachefriends.org/download.html and download the latest version of XAMPP.
2. Install the XAMPP:
- Once the download is complete, run the installer.
- Follow the installation wizard. You can choose the components you want to install, but for PHP development, you typically need Apache, MySQL, PHP, and phpMyAdmin.
- Choose the installation directory. The default is
C:\xampp
, but you can choose a different directory if you prefer.
3. Open the XAMPP control panle & start Apache and MySQL:
- After the installation is complete, launch the XAMPP Control Panel.
- Start the Apache and MySQL services by clicking on the “Start” buttons next to their respective names. You might need to run it as an administrator.
4. Test the Installation:
- Open your web browser and go to
http://localhost
. You should see the XAMPP dashboard, indicating that Apache and PHP are working correctly. - To test MySQL, go to
http://localhost/phpmyadmin/
. If you can access phpMyAdmin, it means MySQL is also working correctly.
5. Set Up Your Development Environment:
- By default, the root directory for your web files in XAMPP is
htdocs
, located within your XAMPP installation directory (e.g.,C:\xampp\htdocs
). You can place your PHP files here and access them through your browser athttp://localhost/yourfile.php
. - You can also create subdirectories within
htdocs
to organize your projects.
6. Configure PHP Settings:
- XAMPP comes with default PHP settings, but you can modify them if needed.
- Locate the
php.ini
file in thephp
directory within your XAMPP installation (e.g.,C:\xampp\php\php.ini
). - Make changes according to your requirements, such as adjusting memory limits, error reporting, etc.
7. Restart Services:
If you make changes to the configuration files, you might need to restart Apache and/or MySQL services for the changes to take effect.
Now How to Create and Execute Your First PHP Website
1. Go inside the XAMPP htdocs folder:
Open your XAMPP control panel and click on Explorer (make sure Apache and MySQL are running).
Now you can see the htdocs
folder, This is the folder where you place your web files.
3. Create your first PHP file:
Inside the htdocs folder, create a new folder called php
.
After that, go inside the php
folder and create your first php file called index.php
.
Put the following PHP code into the index.php
file. You can use Notepad to write the code. The following PHP code will show the text between the quotes.
<?php
echo "<h1>Hello, From PHP</h1>";
4. Open index.php on your browser:
Now open http://localhost/php/index.php
on your browser to see the result.
Execute PHP Code or File via Command-Line on Windows
Basically you need to set your php
folder path into your Environment Variables PATH to achieve this. Follow the steps given below to execute PHP code using command line –
1. Open the XAMPP installation directory (C:\xampp
)
2. Copy the path of the php folder (C:\xampp\php
)
3. Set the php folder path to the environment variables path:
Open Command Prompt by searching for it in the Start menu and clicking on it. To set or modify the PATH variable permanently for your user account, use the following command:
setx PATH "%PATH%;c:\xampp\php"
setx PATH "%PATH%;C:\Your\Path\Here"
Replace C:\Your\Path\Here
with the desired directory path. Please note that changes made with setx won’t take effect until you open a new Command Prompt window.
4. Test it by checking the PHP version via command line:
The command php -v
will give you the version of PHP installed on your computer.
5. Execute PHP file using command line:
To execute a PHP file via command prompt open command prompt or Powershell » navigate to the location of the PHP file » run php file_name.php
.