如果PHP版本的条件忽略新代码 [英] If condition for PHP Version ignore new code

查看:61
本文介绍了如果PHP版本的条件忽略新代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个需要在多个站点上运行的脚本.我已经获得了该脚本的一个版本,该版本已使用一些新的PHP 5.3函数进行了优化,但是某些站点是5.2等.

So I've got a script that needs to run on several sites. I've got one version of the script that is optimised with some new PHP 5.3 functions, however some sites are 5.2 etc.

此代码:

if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
    Do the optimised 5.3 code (Although 5.2 throws syntax errors for it)
} else {
  do the slower version of code
}

但是,在5.2服务器上,即使从技术上讲应该跳过该内容,它也会在第一个if条件下检测到语法错误",我知道PHP仍会扫描整个文件.

However, on the 5.2 servers, it will detect the "syntax errors" in the first if condition, even though it technically should skip that content, I'm aware that PHP still scans the whole file.

我如何才能使5.2完全忽略第一个(我知道我可以使用"@"忽略错误,但这感觉像是在作弊?)

How can I get 5.2 to ignore the first if completely (I know I could use "@" to ignore errors, but that feels like cheating?)

推荐答案

您可以根据版本包含不同的脚本,因此该版本永远不会包含5.2中无效语法的脚本.

You could include different scripts based on version, then the script with syntax that isn't valid in 5.2 would never be included for that version.

if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
     include("script53.php");
} else {
     include("script52.php");
}

这篇关于如果PHP版本的条件忽略新代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆