偶然看到别人写的一句代码:

<?<span style="color: #000000">php

</span><span style="color: #0000ff">if</span> (<span style="color: #008080">version_compare</span>("5.2", <span style="color: #ff00ff">PHP_VERSION</span>, ">"<span style="color: #000000">)) {
     </span><span style="color: #0000ff">die</span>("PHP 5.2 or greater is required!!!"<span style="color: #000000">);
 }

</span>?>

发现获取PHP的版本信息原来这么简单~~~ PHP_VERSION是一个常量,他就能获得PHP版本信息。 version_compare是一个版本比较工具,当然也可以作为其他的版本比较工具:

<?<span style="color: #000000">php
 </span><span style="color: #0000ff">if</span> (<span style="color: #008080">version_compare</span>(<span style="color: #ff00ff">PHP_VERSION</span>, '6.0.0') >= 0<span style="color: #000000">) {
     </span><span style="color: #0000ff">echo</span> 'I am at least PHP version 6.0.0, my version: ' . <span style="color: #ff00ff">PHP_VERSION</span> . "n"<span style="color: #000000">;
 }
 
 </span><span style="color: #0000ff">if</span> (<span style="color: #008080">version_compare</span>(<span style="color: #ff00ff">PHP_VERSION</span>, '5.3.0') >= 0<span style="color: #000000">) {
     </span><span style="color: #0000ff">echo</span> 'I am at least PHP version 5.3.0, my version: ' . <span style="color: #ff00ff">PHP_VERSION</span> . "n"<span style="color: #000000">;
 }
 
 </span><span style="color: #0000ff">if</span> (<span style="color: #008080">version_compare</span>(<span style="color: #ff00ff">PHP_VERSION</span>, '5.0.0', '>='<span style="color: #000000">)) {
     </span><span style="color: #0000ff">echo</span> 'I am using PHP 5, my version: ' . <span style="color: #ff00ff">PHP_VERSION</span> . "n"<span style="color: #000000">;
 }
 
 </span><span style="color: #0000ff">if</span> (<span style="color: #008080">version_compare</span>(<span style="color: #ff00ff">PHP_VERSION</span>, '5.0.0', '<'<span style="color: #000000">)) {
     </span><span style="color: #0000ff">echo</span> 'I am using PHP 4, my version: ' . <span style="color: #ff00ff">PHP_VERSION</span> . "n"<span style="color: #000000">;
 }
 </span>?>