<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Compares two "PHP-standardized" version number strings</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.sys-get-temp-dir.html">sys_get_temp_dir</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.zend-logo-guid.html">zend_logo_guid</a></div> <div class="up"><a href="ref.info.html">PHP Options/Info Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.version-compare" class="refentry"> <div class="refnamediv"> <h1 class="refname">version_compare</h1> <p class="verinfo">(PHP 4 >= 4.1.0, PHP 5)</p><p class="refpurpose"><span class="refname">version_compare</span> — <span class="dc-title">Compares two "PHP-standardized" version number strings</span></p> </div> <div class="refsect1 description" id="refsect1-function.version-compare-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>version_compare</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$version1</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$version2</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$operator</code></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>version_compare()</strong></span> compares two "PHP-standardized" version number strings. This is useful if you would like to write programs working only on some versions of PHP. </p> <p class="para"> The function first replaces <em>_</em>, <em>-</em> and <em>+</em> with a dot <em>.</em> in the version strings and also inserts dots <em>.</em> before and after any non number so that for example '4.3.2RC1' becomes '4.3.2.RC.1'. Then it splits the results like if you were using explode('.', $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: <em>any string not found in this list</em> < <em>dev</em> < <em>alpha</em> = <em>a</em> < <em>beta</em> = <em>b</em> < <em>RC</em> = <em>rc</em> < <em>#</em> < <em>pl</em> = <em>p</em>. This way not only versions with different levels like '4.1' and '4.1.2' can be compared but also any PHP specific version containing development state. </p> </div> <div class="refsect1 parameters" id="refsect1-function.version-compare-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">version1</code></em></span> <dd> <p class="para"> First version number. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">version2</code></em></span> <dd> <p class="para"> Second version number. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">operator</code></em></span> <dd> <p class="para"> If you specify the third optional <em><code class="parameter">operator</code></em> argument, you can test for a particular relationship. The possible operators are: <em><</em>, <em>lt</em>, <em><=</em>, <em>le</em>, <em>></em>, <em>gt</em>, <em>>=</em>, <em>ge</em>, <em>==</em>, <em>=</em>, <em>eq</em>, <em>!=</em>, <em><></em>, <em>ne</em> respectively. </p> <p class="para"> This parameter is case-sensitive, so values should be lowercase. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.version-compare-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> By default, <span class="function"><strong>version_compare()</strong></span> returns <em>-1</em> if the first version is lower than the second, <em>0</em> if they are equal, and <em>1</em> if the second is lower. </p> <p class="para"> When using the optional <em><code class="parameter">operator</code></em> argument, the function will return <strong><code>TRUE</code></strong> if the relationship is the one specified by the operator, <strong><code>FALSE</code></strong> otherwise. </p> </div> <div class="refsect1 examples" id="refsect1-function.version-compare-examples"> <h3 class="title">Examples</h3> <p class="para"> The examples below use the <strong><code>PHP_VERSION</code></strong> constant, because it contains the value of the PHP version that is executing the code. </p> <p class="para"> <div class="example" id="example-523"> <p><strong>Example #1 <span class="function"><strong>version_compare()</strong></span> examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">version_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">PHP_VERSION</span><span style="color: #007700">, </span><span style="color: #DD0000">'6.0.0'</span><span style="color: #007700">) >= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'I am at least PHP version 6.0.0, my version: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_VERSION </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />if (</span><span style="color: #0000BB">version_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">PHP_VERSION</span><span style="color: #007700">, </span><span style="color: #DD0000">'5.3.0'</span><span style="color: #007700">) >= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'I am at least PHP version 5.3.0, my version: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_VERSION </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />if (</span><span style="color: #0000BB">version_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">PHP_VERSION</span><span style="color: #007700">, </span><span style="color: #DD0000">'5.0.0'</span><span style="color: #007700">, </span><span style="color: #DD0000">'>='</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">'I am using PHP 5, my version: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_VERSION </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />if (</span><span style="color: #0000BB">version_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">PHP_VERSION</span><span style="color: #007700">, </span><span style="color: #DD0000">'5.0.0'</span><span style="color: #007700">, </span><span style="color: #DD0000">'<'</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">'I am using PHP 4, my version: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_VERSION </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.version-compare-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The <strong><code>PHP_VERSION</code></strong> constant holds current PHP version. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Note that pre-release versions, such as 5.3.0-dev, are considered lower than their final release counterparts (like 5.3.0). </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Special version strings such as <em>alpha</em> and <em>beta</em> are case sensitive. Version strings from arbitrary sources that do not adhere to the PHP standard may need to be lowercased via <span class="function"><a href="function.strtolower.html" class="function">strtolower()</a></span> before calling <span class="function"><strong>version_compare()</strong></span>. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.version-compare-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.phpversion.html" class="function" rel="rdfs-seeAlso">phpversion()</a> - Gets the current PHP version</span></li> <li class="member"> <span class="function"><a href="function.php-uname.html" class="function" rel="rdfs-seeAlso">php_uname()</a> - Returns information about the operating system PHP is running on</span></li> <li class="member"> <span class="function"><a href="function.function-exists.html" class="function" rel="rdfs-seeAlso">function_exists()</a> - Return TRUE if the given function has been defined</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.sys-get-temp-dir.html">sys_get_temp_dir</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.zend-logo-guid.html">zend_logo_guid</a></div> <div class="up"><a href="ref.info.html">PHP Options/Info Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>