{"id":266,"date":"2013-08-21T16:16:24","date_gmt":"2013-08-21T08:16:24","guid":{"rendered":"http:\/\/www.zyuns.com\/?p=266"},"modified":"2015-02-15T20:47:16","modified_gmt":"2015-02-15T12:47:16","slug":"php-ban-ben-bi-jiao-han-shu-version-compare","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=266","title":{"rendered":"php\u7248\u672c\u6bd4\u8f83\u51fd\u6570version_compare()"},"content":{"rendered":"<p>php\u4e2d\u6bd4\u8f83php\u7248\u672c\uff0c\u4e00\u822c\u7528version_compare()\u51fd\u6570\uff0c\u5e2e\u52a9\u6587\u6863\u89c1\uff1ahttp:\/\/php.net\/manual\/en\/function.version-compare.php<br \/>\n\u7528\u9014\uff1aCompares two &#8220;PHP-standardized&#8221; version number strings\u3002<br \/>\n\u8bed\u6cd5\uff1a<b>version_compare<\/b> ( string <tt>$version1<\/tt> , string <tt>$version2<\/tt> [, string <tt>$operator<\/tt> ] )<br \/>\n\u5177\u4f53\u63cf\u8ff0\uff1a<b>version_compare()<\/b> compares two &#8220;PHP-standardized&#8221; version number strings. This is useful if you would like to write programs working only on some versions of PHP.<\/p>\n<p>The function first replaces <i>_<\/i>, <i>&#8211;<\/i> and <i>+<\/i> with a dot <i>.<\/i> in the version strings and also inserts dots <i>.<\/i> before and after any non number so that for example &#8216;4.3.2RC1&#8217; becomes &#8216;4.3.2.RC.1&#8217;. Then it splits the results like if you were using explode(&#8216;.&#8217;, $ver). Then it compares the parts starting from left to right. If a part contains special version strings these are handled in the following order: <i>any string not found in this list<\/i> &lt; <i>dev<\/i> &lt; <i>alpha<\/i> = <i>a<\/i> &lt; <i>beta<\/i> = <i>b<\/i> &lt; <i>RC<\/i> = <i>rc<\/i> &lt; <i>#<\/i> &lt; <i>pl<\/i> = <i>p<\/i>. This way not only versions with different levels like &#8216;4.1&#8217; and &#8216;4.1.2&#8217; can be compared but also any PHP specific version containing development state.<\/p>\n<p>\u5176\u4e2d\u7b2c\u4e09\u4e2a\u53ef\u9009\u53c2\u6570\u662f\u6bd4\u8f83\u7b26\uff1a<\/p>\n<dl>\n<dd>If you specify the third optional <i><tt>operator<\/tt><\/i> argument, you can test for a particular relationship. The possible operators are: <i>&lt;<\/i>, <i>lt<\/i>, <i>&lt;=<\/i>, <i>le<\/i>, <i>&gt;<\/i>, <i>gt<\/i>, <i>&gt;=<\/i>, <i>ge<\/i>, <i>==<\/i>, <i>=<\/i>, <i>eq<\/i>, <i>!=<\/i>, <i>&lt;&gt;<\/i>, <i>ne<\/i> respectively.<\/p>\n<p>This parameter is case-sensitive, so values should be lowercase.<\/p>\n<\/dd>\n<dt>\u00a0\u00a0\u00a0 \u8fd4\u56de\u503c\uff1a<\/dt>\n<\/dl>\n<p>By default, <b>version_compare()<\/b> returns <i>-1<\/i> if the first version is lower than the second, <i>0<\/i> if they are equal, and <i>1<\/i> if the second is lower.<\/p>\n<p>When using the optional <i><tt>operator<\/tt><\/i> argument, the function will return <b><tt>TRUE<\/tt><\/b> if the relationship is the one specified by the operator, <b><tt>FALSE<\/tt><\/b> otherwise.<\/p>\n<p>\u4ece\u4e0b\u9762\u7684\u4f8b\u5b50\uff0c\u5c31\u53ef\u4ee5\u5f88\u597d\u5730\u8bf4\u660eversion_compare\u7684\u7528\u6cd5\uff1a<\/p>\n<p><code> &lt;?php<br \/>\nif\u00a0(version_compare(PHP_VERSION,\u00a0'6.0.0')\u00a0&gt;=\u00a00)\u00a0{<br \/>\necho\u00a0'I\u00a0am\u00a0at\u00a0least\u00a0PHP\u00a0version\u00a06.0.0,\u00a0my\u00a0version:\u00a0'\u00a0.\u00a0PHP_VERSION\u00a0.\u00a0\"n\";<br \/>\n}<\/p>\n<p>if\u00a0(version_compare(PHP_VERSION,\u00a0'5.3.0')\u00a0&gt;=\u00a00)\u00a0{<br \/>\necho\u00a0'I\u00a0am\u00a0at\u00a0least\u00a0PHP\u00a0version\u00a05.3.0,\u00a0my\u00a0version:\u00a0'\u00a0.\u00a0PHP_VERSION\u00a0.\u00a0\"n\";<br \/>\n}<\/p>\n<p>if\u00a0(version_compare(PHP_VERSION,\u00a0'5.0.0',\u00a0'&gt;='))\u00a0{<br \/>\necho\u00a0'I\u00a0am\u00a0using\u00a0PHP\u00a05,\u00a0my\u00a0version:\u00a0'\u00a0.\u00a0PHP_VERSION\u00a0.\u00a0\"n\";<br \/>\n}<\/p>\n<p>if\u00a0(version_compare(PHP_VERSION,\u00a0'5.0.0',\u00a0'&lt;'))\u00a0{<br \/>\necho\u00a0'I\u00a0am\u00a0using\u00a0PHP\u00a04,\u00a0my\u00a0version:\u00a0'\u00a0.\u00a0PHP_VERSION\u00a0.\u00a0\"n\";<br \/>\n}<br \/>\n?&gt;<\/p>\n<p>\u6700\u8fd1\u5728nagios\u7684\u56fe\u8868\u76d1\u63a7\u63d2\u4ef6pnp\u5b89\u88c5\u4e2d\u9047\u5230\u4e00\u4e2a\u9519\u8bef\u63d0\u793a\uff1aKohana requires PHP 5.2 or newer.\u67e5\u770b\u9875\u9762\u6e90\u7801\uff0c\u53d1\u73b0\u5982\u4e0b\u8bed\u53e5\uff1a<br \/>\nversion_compare(PHP_VERSION, '5.2', '&lt;') and exit('Kohana requires PHP 5.2 or newer.');<br \/>\n\u4e5f\u5c31\u662f\u7248\u672c\u4e0d\u6ee1\u8db3\u5c31\u9000\u51fa\u3002<\/p>\n<p><\/code>strcmp()\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u8fdb\u884c\u6bd4\u8f83\uff0c\u4f46\u50cf\u4e0b\u9762\u8fd9\u6837\uff0c\u5c31\u4f1a\u51fa\u73b0\u9519\u8bef\uff1a<br \/>\n4.1.10\u4e0e4.1.2\u6bd4\u8f83<br \/>\nstrcmp(\u201c4.1.10\u2033, \u201c4.1.2\u2033); \u4f1a\u8fd4\u56de-1 \u9519\u8bef<br \/>\nversion_compare(\u201c4.1.10\u2033. \u201c4.1.2\u2033); \u5c31\u4f1a\u8fd4\u56de1 \u6b63\u786e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u4e2d\u6bd4\u8f83php\u7248\u672c\uff0c\u4e00\u822c\u7528version_compare()\u51fd\u6570\uff0c\u5e2e\u52a9\u6587\u6863\u89c1\uff1ahttp:\/\/php.net [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-266","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=266"}],"version-history":[{"count":1,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/266\/revisions"}],"predecessor-version":[{"id":431,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/266\/revisions\/431"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}