PEAR logo

PHP_CompatInfo : The Definitive Guide

Chapter 5. 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"

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

We will use this very simple detection script.

  1. <?php
  2. require_once 'PHP/CompatInfo.php';
  3.  
  4. $info = new PHP_CompatInfo();
  5. $path_to_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'math.php';
  6. $res = $info->parseFile($path_to_file);
  7.  
  8. echo '<pre>'; var_dump($res); echo '</pre>';
  9. ?>

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 "pci.bat", while other users called directly "pci.php". To simplify, we will suppose that all users called CLI on same way (pci).

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

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

pci -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.5.1 : November 19, 2007