Search code examples
phpversion

How to make a checking upto third level of a string like version checking like '8.1.1' is greater that '8.1.0'?


I have a raw php (vanilla php) script and upgraded php v8.1.0. And now I don't want to run the script in a lower version of that. I want to make that check-in raw PHP as I don't have a composer or don't use any framework, how can I do that?

Actually I want to make a checking of my own third level number like

if ('7.4.9' > '8.1.0') not using php built in function in some case we also see version: '^7.4'. In that case what also can be done.

Can anyone give any suggestions please, or anywhere I can get the answers?


Solution

  • I think you mean vanilla PHP instead of raw PHP. You can test the PHP version into the script like this:

    if (phpversion() < '8.1.0') {
       die ('At least PHP version 8.1.0 is required');
    }
    

    If you need a more granular comparison on the subversion or on the release version you can use version_compare() which does all dirty job for you:

    if (version_compare(phpversion(), '8.1.0', '<')) {
       die ('At least PHP version 8.1.0 is required');
    }
    

    Constants are also available 'for all tastes':

    • PHP_VERSION
    • PHP_VERSION_ID
    • PHP_MAJOR_VERSION
    • PHP_MINOR_VERSION
    • PHP_RELEASE_VERSION