PHP Version Number Comparison issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    PHP Version Number Comparison issue

    I have written several applications that have very specific
    functionality that is version-dependent, furthermore, requirements
    dictate my apps span multiple versions of PHP.

    I have written a function that detects which version of PHP and
    ultimately converts it into integer form, however, I am foreseeing a
    problem in doing this which I will illustrate to you now:

    [PHP]
    if (!function_exis ts('get_php_ver sion_int')) {
    /**
    * Get PHP version into integer form
    *
    * @access public
    * @return int $phpVersionInt
    */
    function get_php_version _int($seedVersi on = '') {
    $phpVersion = (strlen($seedVe rsion) > 0) ? str_replace('.' , '',
    $seedVersion) : str_replace('.' , '', phpversion());
    return (int)(preg_repl ace('/^([0-9]+).*$/','$1', $phpVersion));
    }
    }
    echo '5.0.4 > 4.3.11? '. ('5.0.4' > '4.3.11') . ' BUT.. using
    get_php_version _int? ' . (get_php_versio n_int('5.0.4') >
    get_php_version _int('4.3.11')) .
    '<P> 4.3.11 > 4.3.2? ' . ('4.3.11' > '4.3.2') . ' BUT.. using
    get_php_version _int? ' . (get_php_versio n_int('4.3.11') >
    get_php_version _int('4.3.2')) .
    '<P> 4.3.2 > 4.1.10? ' . ('4.3.2' > '4.1.10') . ' BUT.. using
    get_php_version _int? ' . (get_php_versio n_int('4.3.2') >
    get_php_version _int('4.1.10')) ;

    [/PHP]

    Produces the following results:

    5.0.4 > 4.3.11? 1 BUT.. using get_php_version _int?

    4.3.11 > 4.3.2? BUT.. using get_php_version _int? 1

    4.3.2 > 4.1.10? 1 BUT.. using get_php_version _int?
    The first line is correct if the versions are kept as strings but
    incorrect if converted to integer. The second line is wrong if the
    versions are kept as strings but correct if converted to integer. The
    third line is correct if the versions are kept as strings but incorrect
    if converted to integer.

    My code throughout my apps depends on an integer format for now for
    version number comparison using get_php_version _int(). What would you
    recommend I do that might be better yet requires the least amount of
    code changes?

    Thanks
    Phil

  • Chung Leong

    #2
    Re: PHP Version Number Comparison issue

    Why not version_compare ()?

    Comment

    Working...