w3resource

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
// Check if PHP version is at least 6.0.0
if (version_compare(PHP_VERSION, '6.0.0') >= 0) {
    echo 'I am at least PHP version 6.0.0, my version: ' . PHP_VERSION . "\n";
}

// Check if PHP version is at least 5.3.0
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
    echo 'I am at least PHP version 5.3.0, my version: ' . PHP_VERSION . "\n";
}

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

// Check if PHP version is less than 5.0.0
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?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.