phpundmysql.de

First steps with PHP 5.6 on Windows

It’s almost a tradition for us to publish a How-To-Install for upcoming PHP versions with some example code of new or extended features. With the forth beta being online we take a closer look at PHP 5.6. This time – on special request – we start to do so in English. The German version can be found here.

1. Install Apache

Our first steps include installing an Apache webserver. This is done the same way as with prior versions which we described here: „PHP 5.5 und Apache unter Windows installieren“ (German only). Take a look at http://www.apachelounge.com/download/ to find the latest version of Apache (Version 2.4.9 as of time of writing). There are distributions for 32 and 64 Bit, compiled with Visual Studio 2012 (VC11). Download a suitable version and extract the folder named Apache24 to a new folder on your drive. We will use  c:\wap for this example (The name wap of our folder corresponds to the well known WAMP for Windows-Apache-PHP, without MySQL of course). Up to now we created a structure like this:

C:
+- wap
  +- apache
    +- all the apache stuff like bin, conf, etc...

Note: „Visual C++ Redistributable für Visual Studio 2012“ is required as runtime for the apache to work. If you do not have this installed go check  http://www.microsoft.com/en-US/download/details.aspx?id=30679.

We postpone the configuration of Apache until we integrated PHP. Let’s do the latter first.

2. Install PHP

Even prior to its official release you can get a sneak preview of development process of PHP: either with a beta version or in form of a snapshot. These differ in frequency of releases and stability. While there are commonly 3-4 betas at intervals of some weeks there are more than one snapshots per day. Betas are stable enough to be tested by the community, snapshots may not. Last but not least: only beta versions are anounced at php.net. You can find a history of betas for Windows OS at http://windows.php.net/qa/. Snapshots on the other hand are listed at http://windows.php.net/downloads/snaps/php-5.6/.

For our first steps we will use a snapshot. To work along with the Apache web server it has to be a “Thread Safe VC11 x64″ version, named php-5.6-ts-windows-vc11-x64-rfb991bd.zip. Do not forget to have a look at its parent directory (see link above) as there is most probably a newer versions at the time you look at it. Download the snapshot and extract it to a new subdirectory of c:\wap which is named php for obvious reasons. And while you are creating new folders: add two more. One named htdocs where our test scripts will be stored and tmp for temporary files like sessions and stuff. Your resulting structure should look like that:

C:
+- wap
  +- apache
    +- alle the apache stuff like bin, conf, etc...
  +- htdocs (empty)
  +- php
    +- all the php stuff like ext, extras and logs
  +- tmp (empty)

3. Configure both Apache and PHP

With Apache and PHP on our PC we are good to go and only the configuration is left. Like always we are going to create a portable installation which will continue working even when we copy it to a different location on our hard drive or a USB stick. Therefore we change all paths to relative ones.

Let’s begin with the PHP config file named c:\wap\php\php.ini. It does not exist right now but instead we create it as a copy of php.ini-development in the same folder. Open the file in a text editor of your choice and search for „extension_dir„. Do not get confused when you see multiple results. They all are comments by default – as you can see the semicolon (;) at the beginning of the line. Find the occurence which follows the comment „; On Windows:“ in the line above. Change
; extension_dir = "ext"
to
extension_dir = "..\..\php\ext"
Please note: remove the semicolon so the line is parsed and not dismissed as a comment anymore! The base of the relative path is the Apache config file httpd.conf. Now that the server knows where to find extensions you need to activate some necessary extensions by removing the leading semicolons. Search for ;extension= and find a block of about 20 lines in the middle of the ini file. Uncomment  curl, gd2, mbstring, mysqli, sockets and xmlrpc for starters.
Our last task of the PHP configuration is to define a path for temporary files. Remember we have created the tmp folder for this. So assign this folder to upload_tmp_dir as well as  session.save_path relatively with "..\tmp". Uncomment both places as well.

Next, open C:\wap\apache\conf\httpd.conf to configure the Apache web server. See the list of necassary changes below. Use your text editor to search for the unterlined parts and change to or add the italic parts:

  • ServerRoot „c:/Apache24“ –> ServerRoot „..“
  • Listen 80 –> Listen 82
  • LoadModule php5_module „../php/php5apache2_4.dll“ (add at the end of the block consisting of LoadModule directives)
  • #ServerName www.example.com:80 –> ServerName localhost:82
  • Require all denied (within <Directory />) –> Require all granted
  • DocumentRoot „c:/Apache24/htdocs“ –> DocumentRoot „../htdocs“
  • <Directory „c:/Apache24/htdocs“> –> <Directory „../htdocs“>
  • DirectoryIndex index.html –> DirectoryIndex index.php index.html
  • AddType application/x-httpd-php .php (add at the end of the <IfModule mime_module> directive, e.g. after AddType application/x-gzip .gz .tgz)
  • PHPIniDir ../php (add this as last line of the file)

To avoid conflicts with either other web servers (like running WAMP installations) or other software (like Skype) we changed the server port from 80 to 82. The most obvious consequence is the necessary addition of the port number to any URL in the following examples: always use  http://localhost:82/ instead of http://localhost/. If you are sure that port 80 is free, you can omit this particular change.

Let us stress the fact that this current installation is for testing purposes only! Unlike the snapshot PHP version the Apache may be used in production. But the configuration – mostly granting all access – made it too unsecure.

4. Running some tests

To start the server open a command prompt (Win+R) and switch to our directory

cd c:\wap\apache\bin

Call

httpd

and hope for the best. The server should start without warning. If this is not the case ask Google for help with the particular error message. Otherwise create your first PHP script in the htdocs folder named phpinfo.php and the following one-liner:

<?php phpinfo(); ?>

Run it in your browser with http://localhost:82/phpinfo.php:

Ready-to-go-examples für new PHP 5.6 features can be found in the PHP documentation. Don’t be disappointed: when you expect some killer features you have to wait for another release. PHP 5.6 brings some extended language constructs that should ease the developers life. Not more, not less:

Variadic Functions & Argument Unpacking

Functions with variable number of arguments could be used indirectly by using func_get_args() so far. PHP 5.6 brings explicit support by adding the  ... operator. Parameters supplied with … are available within the functions body as an array, so the workaround with func_get_args() is not necessary any more:

<?php
function v( $required, $optional = 0, ...$variable){
  printf('Variadic params are of type %s with length %d',
    gettype($variable), count($variable));
}

v('a');
v('b', 1);
v('c', 2, 9);
v('c', 2, 9, 8);
v('c', 2, 9, 8, 7);
v('c', 2, 9, 8, 7, 6);
?>

In case the variable parameter is an array or traversible object the … operator is used to unpack the elements in it. The following function is used to add all values and returns the integer value of 15:

<?php
function sumOf($a, $b, $c, $d, $e) {
  return $a + $b + $c + $d + $e;
}

$input = [2, 3, 4, 5];
echo sumOf(1, ...$input);
?>

Logic injection with use function & use const

The use construct injects classes into namespaces so far. This injection construct is extended to be used with functions and constants:

<?php
namespace Name\Space {
  const FOO = 42;
  function f() { echo __FUNCTION__."\n"; }
}

namespace {
  use const Name\Space\FOO;
  use function Name\Space\f;

  echo FOO."\n";
  f();
}
?>

Kommentare sind deaktiviert.