XF 1.5 if statement in config

MattW

Well-known member
I'm trying to switch the Memcached version being used based on the PHP version

Is going something like this possible in the config.php file?

PHP:
if (phpversion() < "7") {
        $config['cache']['backend'] = 'Memcached';
} else {
        $config['cache']['backend'] = 'Libmemcached';
}
 
You are using a comparison operator on a string though. I'm not sure if you can really do that in the way you intended it. if phpversion() is an integer you should have 7 as an integer I guess, not as a string.
 
phpversion() returns a string with the major and minor versions, and may return other info. IE: 7.0.15-0ubuntu0.16.04.4

So you'll need to explode that to get the major version out of the string. Other than that, the general idea of what you want to do would work.
 
Last edited:
I've added it per my original post, and it's returning the correct values on this specific server to switch purely based on the initial if statement.

Code:
# /opt/cpanel/ea-php56/root/usr/bin/php matt.php
5.6.30

# /opt/cpanel/ea-php70/root/usr/bin/php matt.php
7.0.16

# /opt/cpanel/ea-php71/root/usr/bin/php matt.php
7.1.2
 
Snog and Robust bring up good points that I didn't notice on my initial skim read. You can change your conditional to something like this to ensure it doesn't fall over in a case where the version isn't what you expect:
PHP:
if (version_compare(phpversion(), '7.0.0', '<'))
{
    $config['cache']['backend'] = 'Memcached';
}
else
{
    $config['cache']['backend'] = 'Libmemcached';
}
 
Top Bottom