PHP Installation on Windows systems

PHP is a popular scripting language that can be used to create dynamic and interactive web pages. PHP can run on various platforms, including Windows, Linux, Mac OS, and Unix. PHP can also work with many web servers, such as Apache, IIS, and Nginx. In this tutorial, we will show you how to install PHP on Windows systems using the Windows Installer and the Windows Zip file methods. We will also show you how to configure PHP to work with Apache web server.

Installing PHP using the Windows Installer

The Windows Installer is an easy way to install PHP on Windows systems. It provides a graphical user interface (GUI) that guides you through the installation process. However, the Windows Installer may not have the latest version of PHP or all the extensions that you need. Therefore, you should check the PHP website for updates and additional extensions after installing PHP using the Windows Installer.

To install PHP using the Windows Installer, follow these steps:

  1. Go to the PHP for Windows download page and download the latest version of PHP that matches your system architecture (x86 or x64) and thread safety (TS or NTS). For example, if you have a 64-bit system and want to use PHP with Apache web server, you should download the x64 Non Thread Safe (NTS) version of PHP.
  2. Run the downloaded file (php-8.2.0-nts-Win32-vs16-x64.msi in this example) and follow the instructions on the screen. You will need to accept the license agreement, choose an installation directory (C:\php in this example), and select the components that you want to install. You can choose either a standard installation or a custom installation. A standard installation will install all the common components and extensions that most users need. A custom installation will allow you to choose which components and extensions you want to install.
  3. After choosing the components, you will need to choose a web server configuration. You can choose either IIS FastCGI or Other CGI if you want to use PHP with a web server other than IIS. If you choose Other CGI, you will need to specify the path to your web server’s configuration file (C:\Apache24\conf\httpd.conf in this example). You will also need to specify whether you want to register PHP as an environment variable and whether you want to configure error reporting.
  4. After choosing the web server configuration, you will need to choose an INI file location. You can choose either Development or Production depending on your environment. A Development INI file will enable more error reporting and debugging features, while a Production INI file will disable them for better performance and security.
  5. After choosing the INI file location, you will need to review your installation options and click Install to start the installation process. The installer will copy the files and make the necessary changes to your web server’s configuration file.
  6. After the installation is complete, click Finish to exit the installer.

Installing PHP using the Windows Zip file

The Windows Zip file is another way to install PHP on Windows systems. It provides more flexibility and control over the installation process than the Windows Installer. However, it also requires more manual steps and configuration settings than the Windows Installer. Therefore, you should use this method only if you are comfortable with editing files and changing system settings.

To install PHP using the Windows Zip file, follow these steps:

  1. Go to the PHP for Windows download page and download

the latest version of PHP that matches your system architecture (x86 or x64) and thread safety (TS or NTS). For example, if you have a 64-bit system and want to use PHP with Apache web server, you should download the x64 Non Thread Safe (NTS) version of PHP. 2. Extract the downloaded file (php-8.2.0-nts-Win32-vs16-x64.zip in this example) to a folder of your choice, for example C:\php. You can use any file extraction tool, such as WinZip or 7-Zip. 3. Copy the php.ini-development file in the extracted folder and rename it to php.ini. This file contains the configuration settings for PHP. You can edit it with any text editor, such as Notepad or Notepad++. 4. Add C:\php to the PATH environment variable. This will allow you to run PHP from any directory in the command prompt. To do this, follow these steps:

  • Right-click on My Computer and select Properties.
  • Click on Advanced system settings in the left panel.
  • Click on Environment Variables at the bottom of the System Properties window.
  • In the System variables section, find the Path variable and click on Edit.
  • Append ;C:\php to the end of the variable value and click OK. Make sure there is a semicolon before C:\php and no spaces around it.
  • Click OK to close all the windows.

Configuring PHP to work with Apache web server

To configure PHP to work with Apache web server, follow these steps:

  1. Go to the Apache Lounge download page and download the latest version of Apache that matches your system architecture (x86 or x64) and PHP version (VC15 or VC16). For example, if you have a 64-bit system and PHP 8.2.0 VC16, you should download Apache 2.4.52 Win64 VC16.
  2. Extract the downloaded file (httpd-2.4.52-win64-VC16.zip in this example) to a folder of your choice, for example C:\Apache24. You can use any file extraction tool, such as WinZip or 7-Zip.
  3. Open the C:\Apache24\conf\httpd.conf file with any text editor, such as Notepad or Notepad++. This file contains the configuration settings for Apache web server.
  4. Find the line that says #LoadModule rewrite_module modules/mod_rewrite.so and remove the # at the beginning to uncomment it. This will enable the rewrite module that allows you to use URL rewriting rules in your .htaccess files.
  5. Add the following lines at the end of the file to load and configure PHP as an Apache module:
    # Load PHP module
    LoadModule php_module "C:/php/php8apache2_4.dll"
    # Set PHP handler
    AddHandler application/x-httpd-php .php
    # Set PHP configuration file
    PHPIniDir "C:/php"
    
  6. Save and close the file.
  7. Open a command prompt as administrator and navigate to C:\Apache24\bin by typing cd C:\Apache24\bin and pressing Enter.
  8. Install Apache as a Windows service by typing httpd.exe -k install and pressing Enter. You should see a message saying “The ‘Apache2.4’ service is successfully installed.”
  9. Start Apache by typing httpd.exe -k start and pressing Enter. You should see a message saying “The ‘Apache2.4’ service is running.”
  10. Test your Apache installation by opening your browser and going to http://localhost/. You should see a page saying “It works!”

Testing PHP on Windows systems

To test PHP on Windows systems, you can use various tools and methods, such as:

  • Using a web browser. You can use any web browser, such as Chrome, Firefox, Edge or Safari, to test your PHP scripts on your local web server. You just need to enter the URL of your PHP file in the address bar, for example http://localhost/test.php. You should see the output of your PHP script in the browser window. You can also use the browser’s developer tools to inspect the HTML source code, view the network requests and responses, debug errors and warnings, and more.
  • Using a command prompt. You can use a command prompt to test your PHP scripts without a web server. You just need to navigate to the directory where your PHP file is located and run it with the php.exe executable. For example, if your PHP file is C:\php\test.php, you can type C:\php\php.exe test.php and press Enter. You should see the output of your PHP script in the command prompt window. You can also use various command-line options to customize the behavior of PHP, such as -f to parse and execute a file, -l to check the syntax of a file, -r to run PHP code without using script tags, -i to display information about PHP configuration, and more. For a full list of command-line options, see PHP: Command line usage – Manual.
  • Using an IDE or code editor. You can use an integrated development environment (IDE) or a code editor that supports PHP development to test your PHP scripts. An IDE or a code editor can provide you with various features and tools that can help you write, run, debug and optimize your PHP code. Some of the popular IDEs and code editors for PHP development are:
    • Visual Studio Code: A free and open-source code editor that supports multiple languages and platforms, including PHP. It has a built-in terminal, debugger, code formatter, syntax highlighting, code completion, extensions and more.
    • PHPStorm: A commercial IDE that is designed specifically for PHP development. It has advanced features such as code analysis, refactoring, testing, debugging, profiling, version control integration, database tools and more.
    • Eclipse: A free and open-source IDE that supports multiple languages and platforms, including PHP. It has a plugin system that allows you to customize and extend its functionality. It has features such as code editing, debugging, testing, version control integration, database tools and more.
    • NetBeans: A free and open-source IDE that supports multiple languages and platforms, including PHP. It has a plugin system that allows you to customize and extend its functionality. It has features such as code editing, debugging, testing, version control integration, database tools and more.These are some of the tools and methods that you can use to test PHP on Windows systems. You can also find more resources and tutorials on the PHP website and other online sources.

PHP examples on Windows systems

To help you learn and practice PHP on Windows systems, here are some examples of PHP scripts that you can try on your local web server. You can copy and paste the code into a text editor, save it as a .php file in your web server’s document root (for example C:\Apache24\htdocs), and run it in your browser by entering the URL of the file (for example http://localhost/test.php).

Example 1: Hello World

This is a simple PHP script that prints “Hello World” on the web page.

<?php
// This is a comment
echo "Hello World"; // This is another comment
?>

Example 2: Variables

This is a PHP script that demonstrates how to use variables to store and manipulate data.

<?php
// Declare a variable and assign a value
$name = "John";
// Print the value of the variable
echo "Hello $name<br>";
// Change the value of the variable
$name = "Jane";
// Print the new value of the variable
echo "Hello $name<br>";
// Concatenate two strings and assign to a variable
$greeting = "Hello " . $name;
// Print the value of the variable
echo "$greeting<br>";
?>

Example 3: Arrays

This is a PHP script that demonstrates how to use arrays to store and access multiple values.

<?php
// Declare an array and assign values
$colors = array("red", "green", "blue");
// Print the values of the array using a loop
foreach ($colors as $color) {
  echo "$color<br>";
}
// Print the value of a specific element by index
echo $colors[1] . "<br>"; // green
// Change the value of a specific element by index
$colors[1] = "yellow";
// Print the new value of the element by index
echo $colors[1] . "<br>"; // yellow
// Add a new element to the end of the array
$colors[] = "purple";
// Print the length of the array
echo count($colors) . "<br>"; // 4
?>

Example 4: Functions

This is a PHP script that demonstrates how to use functions to perform a specific task and reuse code.

<?php
// Define a function that calculates the area of a rectangle
function area($length, $width) {
  // Return the product of length and width
  return $length * $width;
}
// Call the function with different arguments and print the results
echo "The area of a 10 x 5 rectangle is " . area(10, 5) . "<br>";
echo "The area of a 15 x 7 rectangle is " . area(15, 7) . "<br>";
echo "The area of a 20 x 10 rectangle is " . area(20, 10) . "<br>";
?>

Example 5: Forms

This is a PHP script that demonstrates how to use forms to collect and process user input.

<?php
// Check if the form has been submitted
if (isset($_POST["submit"])) {
  // Get the user input from the $_POST superglobal array
  $name = $_POST["name"];
  $email = $_POST["email"];
  $message = $_POST["message"];
  // Validate the user input
  if (empty($name) || empty($email) || empty($message)) {
    // Display an error message if any field is empty
    echo "Please fill in all the fields.";
  } else {
    // Display a success message if all fields are filled
    echo "Thank you for contacting us, $name. We will reply to your email at $email soon.";
    // You can also send an email or store the data in a database here
  }
}
?>
<!-- Create an HTML form that sends data to the same PHP file using the POST method -->
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
  Name: <input type="text" name="name"><br>
  Email: <input type="email" name="email"><br>
  Message: <textarea name="message" rows="5" cols="40"></textarea><br>
  <input type="submit" name="submit" value="Send">
</form>

These are some of the examples of PHP scripts that you can use to learn and practice PHP on Windows systems. You can also find more examples and tutorials on the PHP website and other online sources.