如果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");
}