469,658 Members | 1,844 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,658 developers. It's quick & easy.

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_exists('get_php_version_int')) {
/**
* Get PHP version into integer form
*
* @access public
* @return int $phpVersionInt
*/
function get_php_version_int($seedVersion = '') {
$phpVersion = (strlen($seedVersion) > 0) ? str_replace('.', '',
$seedVersion) : str_replace('.', '', phpversion());
return (int)(preg_replace('/^([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_version_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_version_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_version_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

Dec 20 '05 #1
1 2078
Why not version_compare()?

Dec 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jordan T. Cox | last post: by
8 posts views Thread by Keith | last post: by
8 posts views Thread by Frodio Jose Serrano | last post: by
122 posts views Thread by Einar | last post: by
reply views Thread by ewingate | last post: by
1 post views Thread by ewingate | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.