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
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?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

Returns all elements in an array except for the first one

Example:

<?php
function tips_tail($items)
{
  return count($items) > 1 ? array_slice($items, 1) : $items;
}

print_r(tips_tail([1, 5, 7]));
?> 

Output:

Array
(
    [0] => 5
    [1] => 7
)