PEAR logo

PHP_CompatInfo : The Definitive Guide

Chapter 4. Basic detection

Table of Contents

Detection of a single file
Usage with SAPI
Usage with CLI
Detection of files into a directory
Usage with SAPI
Usage with CLI

Detection of a single file

In most case, the basic detection is enough. But sometimes, we will need to adjust accuracy of parser to give the best result. This ability is possible with $option, the second parameter of each PHP_CompatInfo API. See parser options list for details.

Usage with SAPI

Suppose we have to detect which PHP version we need to run this script named "math.php"

      
<?php
$nb = bcsub(1.234, 5, 4);
if (preg_match('/^-/', $nb)) {
    echo 'minus';
}
?>
      
     

We will use this very simple detection script.

      
<?php
require_once 'PHP/CompatInfo.php';

$info = new PHP_CompatInfo();
$path_to_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'math.php';
$res = $info->parseFile($path_to_file);

echo '<pre>'; var_dump($res); echo '</pre>';
?>
      
     

Here are the raw results we got :

array(4) {
  ["max_version"]=>
  string(0) ""
  ["version"]=>
  string(5) "3.0.9"
  ["extensions"]=>
  array(2) {
    [0]=>
    string(6) "bcmath"
    [1]=>
    string(4) "pcre"
  }
  ["constants"]=>
  array(0) {
  }
}
     

It means that we need at least PHP 3.0.9 to run the "math.php" script with PHP extensions bcmath, pcre loaded.

Usage with CLI

Windows users have a command line script named "compatinfo.bat", while other users called directly "pcicmd.php". To simplify, we will suppose that all users called CLI on same way (pcicmd).

pcicmd [options] [-d DIR] | [-f FILE]
     

If we try again to detect the same "math.php" script, the command to run will be:

pcicmd -f path_to_file\math.php
     

And result give:

+----------------+---------+------------+------------------+
| File           | Version | Extensions | Constants/Tokens |
+----------------+---------+------------+------------------+
| [...]\math.php | 3.0.9   | bcmath     |                  |
|                |         | pcre       |                  |
+----------------+---------+------------+------------------+
     

[Note] Note
[...] replace the full path to file given by -f option.

PHP_CompatInfo : The Definitive Guide v 1.4.0 : September 27, 2006