w3resource
php Programming Exercies

PHP Exercises : Compare the PHP version

PHP : Exercise-23 with Solution

Write a PHP script to compare the PHP version.

Note : Use version_compare() function and PHP_VERSION constant.

Sample Solution: -

PHP Code:


<?php
if (version_compare(PHP_VERSION, '6.0.0') >= 0) {
echo 'I am at least PHP version 6.0.0, my version: ' . PHP_VERSION . "\n";
}
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
echo 'I am at least PHP version 5.3.0, my version: ' . PHP_VERSION . "\n";
}

if (version_compare(PHP_VERSION, '5.0.0', '>=')) {
echo 'I am using PHP 5, my version: ' . PHP_VERSION . "\n";
}

if (version_compare(PHP_VERSION, '5.0.0', '<')) {
echo 'I am using PHP 4, my version: ' . PHP_VERSION . "\n";
}
?>

Sample Output:

I am at least PHP version 6.0.0, my version: 7.0.15-0ubuntu0.16.04.4
I am at least PHP version 5.3.0, my version: 7.0.15-0ubuntu0.16.04.4
I am using PHP 5, my version: 7.0.15-0ubuntu0.16.04.4  

Flowchart:

Flowchart: Compare the PHP version

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to get the full URL.
Next: Write a PHP script to get the name of the owner of the current PHP script.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming