w3resource

Install PHP on Windows

Obtaining PHP

  • Download latest version of PHP from http://www.php.net/downloads.php.
  • You can also download the .msi package i.e. windows installer of PHP from sourceforge.net

Prerequisite and running on your Windows system, else you will not be able to run PHP Scripts.

Alternatively, you can install WAMP server or AMPPS server to install PHP, MySQL, and Apache Web Server on your Windows machine.

Options for installing PHP on Windows

  • You can install php on windows manually.
  • You can run the .msi package, i.e. windows installer for php to install php.
  • Besides full installation, you can install php extensions on an existing installation of PHP.
  • You can upgrade to a higher version of PHP from an existing version.

Install PHP on Windows manually

You have to download zipped binary version of PHP from http://www.php.net/downloads.php.

Extract the downloaded file to php folder which must be created at the root of any of your windows drives (e.g. c:\php or d:\php).

Enter php folder and rename the file php.ini-recommonded to php.ini.

Open the file php.ini with any text editor and find extension_dir within the file.

Change the extension directory, i.e. if you have installed php in php folder of D drive, setting will be extension_dir=D:\php.

Now you open Environment Variables (MyComputer -> System Properties -> Environment Variables), under system variables, select Path and click Edit,
in the Variable value, go to the end of the list and put a semicolon, then  add  D:\php;D:\php\ext assuming D:\php is the location of your php installation.

Then Click on New under system variable and add PHPRC against Variable name and D:\php against Variable value.

Restart your system.

Install PHP on Windows using Windows installer 

Simply run the .msi Windows installer file and follow the installation wizard.

Testing if PHP is installed properly

Create a php file containing code <?php echo phpinfo(); ?> and save it as test.php.
Run this file on your web server and if you get an output like this:

install PHP on Windows
then you have installed PHP properly.

Install PHP extensions on Windows

What is a PHP extension?

A PHP extension is a piece of code which is not part of the core PHP and you need it to explore certain functionalities of PHP (e.g. for using mysql you need mysql extension of PHP).

Many extensions are available out of the box in the PHP installer downloaded from php.net or in the .msi package. But many need to be obtained separately from php.net.

In Windows, extensions come in the form of a  .dll file which you can download from php.net.

How do you configure an extension on Windows?

You need to change the setting of extension dir in php.ini file. For example, If you are using PHP 5.0.3, and you have installed php in c:\php, then you have to set extension_dir = C:\php\ext in your php.ini file.

After downloading the files needed, you have to open php.ini and enable a line pointing to an extension in question.

For enabling extensions, you have to uncomment a line in your php.ini file. For example, for enabling mysql extension, you have to change; extension = php_mysql.dll to extension = php_mysql.dll in your php.ini file.

You also need to restart your web server and php in order to complete the installation of a specific extension.

Sample php.ini

Previous: PHP Home
Next: Install php on Linux



PHP: Tips of the Day

The ternary operator can be thought of as an inline if statement. It consists of three parts. The operator, and two outcomes. The syntax is as follows:

Example:

$value = <operator> ? <true value> : <false value>

If the operator is evaluated as true, the value in the first block will be returned (<true value>), else the value in the second block will be returned (<false value>). Since we are setting $value to the result of our ternary operator it will store the returned value.

Example:

$action = empty($_POST['action']) ? 'default' : $_POST['action'];

$action would contain the string 'default' if empty($_POST['action']) evaluates to true. Otherwise it would contain the value of $_POST['action'].

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1evaluates to true, and expr3 if expr1 evaluates to false.

It is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise. ?: is often referred to as Elvis operator.

This behaves like the Null Coalescing operator ??, except that ?? requires the left operand to be exactly null while ?: tries to resolve the left operand into a boolean and check if it resolves to boolean false.

Example:

function setWidth(int $width = 0){
 $_SESSION["width"] = $width ?: getDefaultWidth();
}

In this example, setWidth accepts a width parameter, or default 0, to change the width session value. If $width is 0 (if $width is not provided), which will resolve to boolean false, the value of getDefaultWidth() is used instead. The getDefaultWidth() function will not be called if $width did not resolve to boolean false.

Refer to Types for more information about conversion of variables to boolean.