CINXE.COM
PHP: strcasecmp - Manual
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP: strcasecmp - Manual </title> <link rel="icon" type="image/svg+xml" sizes="any" href="https://www.php.net/favicon.svg?v=2"> <link rel="icon" type="image/png" sizes="196x196" href="https://www.php.net/favicon-196x196.png?v=2"> <link rel="icon" type="image/png" sizes="32x32" href="https://www.php.net/favicon-32x32.png?v=2"> <link rel="icon" type="image/png" sizes="16x16" href="https://www.php.net/favicon-16x16.png?v=2"> <link rel="shortcut icon" href="https://www.php.net/favicon.ico?v=2"> <link rel="search" type="application/opensearchdescription+xml" href="http://php.net/phpnetimprovedsearch.src" title="Add PHP.net search"> <link rel="alternate" type="application/atom+xml" href="https://www.php.net/releases/feed.php" title="PHP Release feed"> <link rel="alternate" type="application/atom+xml" href="https://www.php.net/feed.atom" title="PHP: Hypertext Preprocessor"> <link rel="canonical" href="https://www.php.net/manual/en/function.strcasecmp.php"> <link rel="shorturl" href="https://www.php.net/strcasecmp"> <link rel="alternate" href="https://www.php.net/strcasecmp" hreflang="x-default"> <link rel="contents" href="https://www.php.net/manual/en/index.php"> <link rel="index" href="https://www.php.net/manual/en/ref.strings.php"> <link rel="prev" href="https://www.php.net/manual/en/function.str-word-count.php"> <link rel="next" href="https://www.php.net/manual/en/function.strchr.php"> <link rel="alternate" href="https://www.php.net/manual/en/function.strcasecmp.php" hreflang="en"> <link rel="alternate" href="https://www.php.net/manual/de/function.strcasecmp.php" hreflang="de"> <link rel="alternate" href="https://www.php.net/manual/es/function.strcasecmp.php" hreflang="es"> <link rel="alternate" href="https://www.php.net/manual/fr/function.strcasecmp.php" hreflang="fr"> <link rel="alternate" href="https://www.php.net/manual/it/function.strcasecmp.php" hreflang="it"> <link rel="alternate" href="https://www.php.net/manual/ja/function.strcasecmp.php" hreflang="ja"> <link rel="alternate" href="https://www.php.net/manual/pt_BR/function.strcasecmp.php" hreflang="pt_BR"> <link rel="alternate" href="https://www.php.net/manual/ru/function.strcasecmp.php" hreflang="ru"> <link rel="alternate" href="https://www.php.net/manual/tr/function.strcasecmp.php" hreflang="tr"> <link rel="alternate" href="https://www.php.net/manual/uk/function.strcasecmp.php" hreflang="uk"> <link rel="alternate" href="https://www.php.net/manual/zh/function.strcasecmp.php" hreflang="zh"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1707321815&f=/fonts/Fira/fira.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1707321815&f=/fonts/Font-Awesome/css/fontello.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1732428602&f=/styles/theme-base.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1730558402&f=/styles/theme-medium.css" media="screen"> <base href="https://www.php.net/manual/en/function.strcasecmp.php"> <meta name="Description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:site" content="@official_php" /> <meta name="twitter:title" content="PHP: Hypertext Preprocessor" /> <meta name="twitter:description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <meta name="twitter:creator" content="@official_php" /> <meta name="twitter:image:src" content="https://www.php.net/images/meta-image.png" /> <meta itemprop="name" content="PHP: Hypertext Preprocessor" /> <meta itemprop="description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <meta itemprop="image" content="https://www.php.net/images/meta-image.png" /> <meta property="og:image" content="https://www.php.net/images/meta-image.png" /> <meta property="og:description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <link href="https://fosstodon.org/@php" rel="me" /> </head> <body class="docs "> <nav class="navbar navbar-fixed-top"> <div class="navbar__inner"> <a href="/" aria-label="PHP Home" class="navbar__brand"> <img src="/images/logos/php-logo-white.svg" aria-hidden="true" width="80" height="40" > </a> <div id="navbar__offcanvas" tabindex="-1" class="navbar__offcanvas" aria-label="Menu" > <button id="navbar__close-button" class="navbar__icon-item navbar_icon-item--visually-aligned navbar__close-button" > <svg xmlns="http://www.w3.org/2000/svg" width="24" viewBox="0 0 24 24" fill="currentColor"><path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" /></svg> </button> <ul class="navbar__nav"> <li class="navbar__item"> <a href="/downloads.php" class="navbar__link " > Downloads </a> </li> <li class="navbar__item"> <a href="/docs.php" aria-current="page" class="navbar__link navbar__link--active " > Documentation </a> </li> <li class="navbar__item"> <a href="/get-involved.php" class="navbar__link " > Get Involved </a> </li> <li class="navbar__item"> <a href="/support.php" class="navbar__link " > Help </a> </li> <li class="navbar__item"> <a href="/releases/8.4/index.php" class="navbar__link navbar__release" > <img src="/images/php8/logo_php8_4.svg" alt="PHP 8.4"> </a> </li> </ul> </div> <div class="navbar__right"> <!-- Desktop default search --> <form action="/manual-lookup.php" class="navbar__search-form" > <label for="navbar__search-input" aria-label="Search docs"> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </label> <input type="search" name="pattern" id="navbar__search-input" class="navbar__search-input" placeholder="Search docs" accesskey="s" > <input type="hidden" name="scope" value="quickref"> </form> <!-- Desktop encanced search --> <button id="navbar__search-button" class="navbar__search-button" hidden > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> Search docs </button> <!-- Mobile default items --> <a id="navbar__search-link" href="/lookup-form.php" aria-label="Search docs" class="navbar__icon-item navbar__search-link" > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </a> <a id="navbar__menu-link" href="/menu.php" aria-label="Menu" class="navbar__icon-item navbar_icon-item--visually-aligned navbar_menu-link" > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="currentColor" > <path d="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z" /> </svg> </a> <!-- Mobile enhanced items --> <button id="navbar__search-button-mobile" aria-label="Search docs" class="navbar__icon-item navbar__search-button-mobile" hidden > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </button> <button id="navbar__menu-button" aria-label="Menu" class="navbar__icon-item navbar_icon-item--visually-aligned" hidden > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="currentColor" > <path d="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z" /> </svg> </button> </div> <div id="navbar__backdrop" class="navbar__backdrop" ></div> </div> <div id="flash-message"></div> </nav> <div class="headsup"><a href='/conferences/index.php#2024-12-03-1'>PHP Conference Nagoya 2025</a></div> <nav id="trick"><div><dl> <dt><a href='/manual/en/getting-started.php'>Getting Started</a></dt> <dd><a href='/manual/en/introduction.php'>Introduction</a></dd> <dd><a href='/manual/en/tutorial.php'>A simple tutorial</a></dd> <dt><a href='/manual/en/langref.php'>Language Reference</a></dt> <dd><a href='/manual/en/language.basic-syntax.php'>Basic syntax</a></dd> <dd><a href='/manual/en/language.types.php'>Types</a></dd> <dd><a href='/manual/en/language.variables.php'>Variables</a></dd> <dd><a href='/manual/en/language.constants.php'>Constants</a></dd> <dd><a href='/manual/en/language.expressions.php'>Expressions</a></dd> <dd><a href='/manual/en/language.operators.php'>Operators</a></dd> <dd><a href='/manual/en/language.control-structures.php'>Control Structures</a></dd> <dd><a href='/manual/en/language.functions.php'>Functions</a></dd> <dd><a href='/manual/en/language.oop5.php'>Classes and Objects</a></dd> <dd><a href='/manual/en/language.namespaces.php'>Namespaces</a></dd> <dd><a href='/manual/en/language.enumerations.php'>Enumerations</a></dd> <dd><a href='/manual/en/language.errors.php'>Errors</a></dd> <dd><a href='/manual/en/language.exceptions.php'>Exceptions</a></dd> <dd><a href='/manual/en/language.fibers.php'>Fibers</a></dd> <dd><a href='/manual/en/language.generators.php'>Generators</a></dd> <dd><a href='/manual/en/language.attributes.php'>Attributes</a></dd> <dd><a href='/manual/en/language.references.php'>References Explained</a></dd> <dd><a href='/manual/en/reserved.variables.php'>Predefined Variables</a></dd> <dd><a href='/manual/en/reserved.exceptions.php'>Predefined Exceptions</a></dd> <dd><a href='/manual/en/reserved.interfaces.php'>Predefined Interfaces and Classes</a></dd> <dd><a href='/manual/en/reserved.attributes.php'>Predefined Attributes</a></dd> <dd><a href='/manual/en/context.php'>Context options and parameters</a></dd> <dd><a href='/manual/en/wrappers.php'>Supported Protocols and Wrappers</a></dd> </dl> <dl> <dt><a href='/manual/en/security.php'>Security</a></dt> <dd><a href='/manual/en/security.intro.php'>Introduction</a></dd> <dd><a href='/manual/en/security.general.php'>General considerations</a></dd> <dd><a href='/manual/en/security.cgi-bin.php'>Installed as CGI binary</a></dd> <dd><a href='/manual/en/security.apache.php'>Installed as an Apache module</a></dd> <dd><a href='/manual/en/security.sessions.php'>Session Security</a></dd> <dd><a href='/manual/en/security.filesystem.php'>Filesystem Security</a></dd> <dd><a href='/manual/en/security.database.php'>Database Security</a></dd> <dd><a href='/manual/en/security.errors.php'>Error Reporting</a></dd> <dd><a href='/manual/en/security.variables.php'>User Submitted Data</a></dd> <dd><a href='/manual/en/security.hiding.php'>Hiding PHP</a></dd> <dd><a href='/manual/en/security.current.php'>Keeping Current</a></dd> <dt><a href='/manual/en/features.php'>Features</a></dt> <dd><a href='/manual/en/features.http-auth.php'>HTTP authentication with PHP</a></dd> <dd><a href='/manual/en/features.cookies.php'>Cookies</a></dd> <dd><a href='/manual/en/features.sessions.php'>Sessions</a></dd> <dd><a href='/manual/en/features.file-upload.php'>Handling file uploads</a></dd> <dd><a href='/manual/en/features.remote-files.php'>Using remote files</a></dd> <dd><a href='/manual/en/features.connection-handling.php'>Connection handling</a></dd> <dd><a href='/manual/en/features.persistent-connections.php'>Persistent Database Connections</a></dd> <dd><a href='/manual/en/features.commandline.php'>Command line usage</a></dd> <dd><a href='/manual/en/features.gc.php'>Garbage Collection</a></dd> <dd><a href='/manual/en/features.dtrace.php'>DTrace Dynamic Tracing</a></dd> </dl> <dl> <dt><a href='/manual/en/funcref.php'>Function Reference</a></dt> <dd><a href='/manual/en/refs.basic.php.php'>Affecting PHP's Behaviour</a></dd> <dd><a href='/manual/en/refs.utilspec.audio.php'>Audio Formats Manipulation</a></dd> <dd><a href='/manual/en/refs.remote.auth.php'>Authentication Services</a></dd> <dd><a href='/manual/en/refs.utilspec.cmdline.php'>Command Line Specific Extensions</a></dd> <dd><a href='/manual/en/refs.compression.php'>Compression and Archive Extensions</a></dd> <dd><a href='/manual/en/refs.crypto.php'>Cryptography Extensions</a></dd> <dd><a href='/manual/en/refs.database.php'>Database Extensions</a></dd> <dd><a href='/manual/en/refs.calendar.php'>Date and Time Related Extensions</a></dd> <dd><a href='/manual/en/refs.fileprocess.file.php'>File System Related Extensions</a></dd> <dd><a href='/manual/en/refs.international.php'>Human Language and Character Encoding Support</a></dd> <dd><a href='/manual/en/refs.utilspec.image.php'>Image Processing and Generation</a></dd> <dd><a href='/manual/en/refs.remote.mail.php'>Mail Related Extensions</a></dd> <dd><a href='/manual/en/refs.math.php'>Mathematical Extensions</a></dd> <dd><a href='/manual/en/refs.utilspec.nontext.php'>Non-Text MIME Output</a></dd> <dd><a href='/manual/en/refs.fileprocess.process.php'>Process Control Extensions</a></dd> <dd><a href='/manual/en/refs.basic.other.php'>Other Basic Extensions</a></dd> <dd><a href='/manual/en/refs.remote.other.php'>Other Services</a></dd> <dd><a href='/manual/en/refs.search.php'>Search Engine Extensions</a></dd> <dd><a href='/manual/en/refs.utilspec.server.php'>Server Specific Extensions</a></dd> <dd><a href='/manual/en/refs.basic.session.php'>Session Extensions</a></dd> <dd><a href='/manual/en/refs.basic.text.php'>Text Processing</a></dd> <dd><a href='/manual/en/refs.basic.vartype.php'>Variable and Type Related Extensions</a></dd> <dd><a href='/manual/en/refs.webservice.php'>Web Services</a></dd> <dd><a href='/manual/en/refs.utilspec.windows.php'>Windows Only Extensions</a></dd> <dd><a href='/manual/en/refs.xml.php'>XML Manipulation</a></dd> <dd><a href='/manual/en/refs.ui.php'>GUI Extensions</a></dd> </dl> <dl> <dt>Keyboard Shortcuts</dt><dt>?</dt> <dd>This help</dd> <dt>j</dt> <dd>Next menu item</dd> <dt>k</dt> <dd>Previous menu item</dd> <dt>g p</dt> <dd>Previous man page</dd> <dt>g n</dt> <dd>Next man page</dd> <dt>G</dt> <dd>Scroll to bottom</dd> <dt>g g</dt> <dd>Scroll to top</dd> <dt>g h</dt> <dd>Goto homepage</dd> <dt>g s</dt> <dd>Goto search<br>(current page)</dd> <dt>/</dt> <dd>Focus search box</dd> </dl></div></nav> <div id="goto"> <div class="search"> <div class="text"></div> <div class="results"><ul></ul></div> </div> </div> <div id="breadcrumbs" class="clearfix"> <div id="breadcrumbs-inner"> <div class="next"> <a href="function.strchr.php"> strchr » </a> </div> <div class="prev"> <a href="function.str-word-count.php"> « str_word_count </a> </div> <ul> <li><a href='index.php'>PHP Manual</a></li> <li><a href='funcref.php'>Function Reference</a></li> <li><a href='refs.basic.text.php'>Text Processing</a></li> <li><a href='book.strings.php'>Strings</a></li> <li><a href='ref.strings.php'>String Functions</a></li> </ul> </div> </div> <div id="layout" class="clearfix"> <section id="layout-content"> <div class="page-tools"> <div class="change-language"> <form action="/manual/change.php" method="get" id="changelang" name="changelang"> <fieldset> <label for="changelang-langs">Change language:</label> <select onchange="document.changelang.submit()" name="page" id="changelang-langs"> <option value='en/function.strcasecmp.php' selected="selected">English</option> <option value='de/function.strcasecmp.php'>German</option> <option value='es/function.strcasecmp.php'>Spanish</option> <option value='fr/function.strcasecmp.php'>French</option> <option value='it/function.strcasecmp.php'>Italian</option> <option value='ja/function.strcasecmp.php'>Japanese</option> <option value='pt_BR/function.strcasecmp.php'>Brazilian Portuguese</option> <option value='ru/function.strcasecmp.php'>Russian</option> <option value='tr/function.strcasecmp.php'>Turkish</option> <option value='uk/function.strcasecmp.php'>Ukrainian</option> <option value='zh/function.strcasecmp.php'>Chinese (Simplified)</option> <option value='help-translate.php'>Other</option> </select> </fieldset> </form> </div> </div><div id="function.strcasecmp" class="refentry"> <div class="refnamediv"> <h1 class="refname">strcasecmp</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">strcasecmp</span> — <span class="dc-title">Binary safe case-insensitive string comparison</span></p> </div> <div class="refsect1 description" id="refsect1-function.strcasecmp-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="methodname"><strong>strcasecmp</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string1</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string2</code></span>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div> <p class="para rdfs-comment"> Binary safe case-insensitive string comparison. The comparison is not locale aware; only ASCII letters are compared in a case-insensitive way. </p> </div> <div class="refsect1 parameters" id="refsect1-function.strcasecmp-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt><code class="parameter">string1</code></dt> <dd> <p class="para"> The first string </p> </dd> <dt><code class="parameter">string2</code></dt> <dd> <p class="para"> The second string </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.strcasecmp-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <code class="literal">-1</code> if <code class="parameter">string1</code> is less than <code class="parameter">string2</code>; <code class="literal">1</code> if <code class="parameter">string1</code> is greater than <code class="parameter">string2</code>, and <code class="literal">0</code> if they are equal. </p> </div> <div class="refsect1 changelog" id="refsect1-function.strcasecmp-changelog"> <h3 class="title">Changelog</h3> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>8.2.0</td> <td> This function now returns <code class="literal">-1</code> or <code class="literal">1</code>, where it previously returned a negative or positive number. </td> </tr> </tbody> </table> </div> <div class="refsect1 examples" id="refsect1-function.strcasecmp-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5006"> <p><strong>Example #1 <span class="function"><strong>strcasecmp()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$var1 </span><span style="color: #007700">= </span><span style="color: #DD0000">"Hello"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$var2 </span><span style="color: #007700">= </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">;<br />if (</span><span style="color: #0000BB">strcasecmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$var1</span><span style="color: #007700">, </span><span style="color: #0000BB">$var2</span><span style="color: #007700">) == </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'$var1 is equal to $var2 in a case-insensitive string comparison'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.strcasecmp-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li><span class="function"><a href="function.strcmp.php" class="function" rel="rdfs-seeAlso">strcmp()</a> - Binary safe string comparison</span></li> <li><span class="function"><a href="function.preg-match.php" class="function" rel="rdfs-seeAlso">preg_match()</a> - Perform a regular expression match</span></li> <li><span class="function"><a href="function.substr-compare.php" class="function" rel="rdfs-seeAlso">substr_compare()</a> - Binary safe comparison of two strings from an offset, up to length characters</span></li> <li><span class="function"><a href="function.strncasecmp.php" class="function" rel="rdfs-seeAlso">strncasecmp()</a> - Binary safe case-insensitive string comparison of the first n characters</span></li> <li><span class="function"><a href="function.stristr.php" class="function" rel="rdfs-seeAlso">stristr()</a> - Case-insensitive strstr</span></li> <li><span class="function"><a href="function.substr.php" class="function" rel="rdfs-seeAlso">substr()</a> - Return part of a string</span></li> </ul> </p> </div> </div> <div class="contribute"> <h3 class="title">Found A Problem?</h3> <div> </div> <div class="edit-bug"> <a href="https://github.com/php/doc-base/blob/master/README.md" title="This will take you to our contribution guidelines on GitHub" target="_blank" rel="noopener noreferrer">Learn How To Improve This Page</a> • <a href="https://github.com/php/doc-en/blob/master/reference/strings/functions/strcasecmp.xml">Submit a Pull Request</a> • <a href="https://github.com/php/doc-en/issues/new?body=From%20manual%20page:%20https:%2F%2Fphp.net%2Ffunction.strcasecmp%0A%0A---">Report a Bug</a> </div> </div><section id="usernotes"> <div class="head"> <span class="action"><a href="/manual/add-note.php?sect=function.strcasecmp&repo=en&redirect=https://www.php.net/manual/en/function.strcasecmp.php">+<small>add a note</small></a></span> <h3 class="title">User Contributed Notes <span class="count">4 notes</span></h3> </div><div id="allnotes"> <div class="note" id="107016"> <div class="votes"> <div id="Vu107016"> <a href="/manual/vote-note.php?id=107016&page=function.strcasecmp&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd107016"> <a href="/manual/vote-note.php?id=107016&page=function.strcasecmp&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V107016" title="64% like this..."> 28 </div> </div> <a href="#107016" class="name"> <strong class="user"><em>chris at cmbuckley dot co dot uk</em></strong></a><a class="genanchor" href="#107016"> ¶</a><div class="date" title="2011-12-28 04:59"><strong>12 years ago</strong></div> <div class="text" id="Hcom107016"> <div class="phpcode"><code><span class="html">A simple multibyte-safe case-insensitive string comparison:<br /><br /><span class="default"><?php<br /><br /></span><span class="keyword">function </span><span class="default">mb_strcasecmp</span><span class="keyword">(</span><span class="default">$str1</span><span class="keyword">, </span><span class="default">$str2</span><span class="keyword">, </span><span class="default">$encoding </span><span class="keyword">= </span><span class="default">null</span><span class="keyword">) {<br /> if (</span><span class="default">null </span><span class="keyword">=== </span><span class="default">$encoding</span><span class="keyword">) { </span><span class="default">$encoding </span><span class="keyword">= </span><span class="default">mb_internal_encoding</span><span class="keyword">(); }<br /> return </span><span class="default">strcmp</span><span class="keyword">(</span><span class="default">mb_strtoupper</span><span class="keyword">(</span><span class="default">$str1</span><span class="keyword">, </span><span class="default">$encoding</span><span class="keyword">), </span><span class="default">mb_strtoupper</span><span class="keyword">(</span><span class="default">$str2</span><span class="keyword">, </span><span class="default">$encoding</span><span class="keyword">));<br />}<br /><br /></span><span class="default">?><br /></span><br />Caveat: watch out for edge cases like "ß".</span></code></div> </div> </div> <div class="note" id="123230"> <div class="votes"> <div id="Vu123230"> <a href="/manual/vote-note.php?id=123230&page=function.strcasecmp&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd123230"> <a href="/manual/vote-note.php?id=123230&page=function.strcasecmp&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V123230" title="71% like this..."> 20 </div> </div> <a href="#123230" class="name"> <strong class="user"><em>chrislarham at NOSPAM dot outlook dot com</em></strong></a><a class="genanchor" href="#123230"> ¶</a><div class="date" title="2018-10-13 08:47"><strong>6 years ago</strong></div> <div class="text" id="Hcom123230"> <div class="phpcode"><code><span class="html">I didn't see any explanation in the documentation as to precisely how the positive/negative return values are calculated for unequal strings.<br /><br />After a bit of experimentation it appears that it's the difference in alphabetical position of the first character in unequal strings.<br /><br />For example, the letter 'z' is the 26th letter while the letter 'a' is the 1st letter:<br /><br /><span class="default"><?php<br /><br /> $zappl </span><span class="keyword">= </span><span class="string">"zappl"</span><span class="keyword">;<br /> </span><span class="default">$apple </span><span class="keyword">= </span><span class="string">"apple"</span><span class="keyword">;<br /><br /> echo </span><span class="default">strcasecmp</span><span class="keyword">(</span><span class="default">$zappl</span><span class="keyword">, </span><span class="default">$apple</span><span class="keyword">); </span><span class="comment">#outputs 25 [26 - 1]<br /> </span><span class="keyword">echo </span><span class="default">strcasecmp</span><span class="keyword">(</span><span class="default">$apple</span><span class="keyword">, </span><span class="default">$zappl</span><span class="keyword">); </span><span class="comment">#outputs -25 [1 - 26]<br /><br /></span><span class="default">?><br /></span><br />This might be incredibly obvious to most people, but hopefully it will clarify the calculation process for some others.</span></code></div> </div> </div> <div class="note" id="24729"> <div class="votes"> <div id="Vu24729"> <a href="/manual/vote-note.php?id=24729&page=function.strcasecmp&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd24729"> <a href="/manual/vote-note.php?id=24729&page=function.strcasecmp&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V24729" title="57% like this..."> 9 </div> </div> <a href="#24729" class="name"><strong class="user"><em>Anonymous</em></strong></a><a class="genanchor" href="#24729"> ¶</a><div class="date" title="2002-08-27 06:53"><strong>22 years ago</strong></div> <div class="text" id="Hcom24729"> <div class="phpcode"><code><span class="html">The sample above is only true on some platforms that only use a simple 'C' locale, where individual bytes are considered as complete characters that are converted to lowercase before being differentiated.<br /><br />Other locales (see LC_COLLATE and LC_ALL) use the difference of collation order of characters, where characters may be groups of bytes taken from the input strings, or simply return -1, 0, or 1 as the collation order is not simply defined by comparing individual characters but by more complex rules.<br /><br />Don't base your code on a specific non null value returned by strcmp() or strcasecmp(): it is not portable. Just consider the sign of the result and be sure to use the correct locale!</span></code></div> </div> </div> <div class="note" id="99128"> <div class="votes"> <div id="Vu99128"> <a href="/manual/vote-note.php?id=99128&page=function.strcasecmp&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd99128"> <a href="/manual/vote-note.php?id=99128&page=function.strcasecmp&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V99128" title="54% like this..."> 4 </div> </div> <a href="#99128" class="name"> <strong class="user"><em>alvaro at demogracia dot com</em></strong></a><a class="genanchor" href="#99128"> ¶</a><div class="date" title="2010-07-29 05:35"><strong>14 years ago</strong></div> <div class="text" id="Hcom99128"> <div class="phpcode"><code><span class="html">Don't forget this is a single-byte function: in Unicode strings it'll provide incoherent results as soon as both strings differ only in case. There doesn't seem to exist a built-in multi-byte alternative so you need to write your own, taking into account both character encoding and collation.</span></code></div> </div> </div></div> <div class="foot"><a href="/manual/add-note.php?sect=function.strcasecmp&repo=en&redirect=https://www.php.net/manual/en/function.strcasecmp.php">+<small>add a note</small></a></div> </section> </section><!-- layout-content --> <aside class='layout-menu'> <ul class='parent-menu-list'> <li> <a href="ref.strings.php">String Functions</a> <ul class='child-menu-list'> <li class=""> <a href="function.addcslashes.php" title="addcslashes">addcslashes</a> </li> <li class=""> <a href="function.addslashes.php" title="addslashes">addslashes</a> </li> <li class=""> <a href="function.bin2hex.php" title="bin2hex">bin2hex</a> </li> <li class=""> <a href="function.chop.php" title="chop">chop</a> </li> <li class=""> <a href="function.chr.php" title="chr">chr</a> </li> <li class=""> <a href="function.chunk-split.php" title="chunk_​split">chunk_​split</a> </li> <li class=""> <a href="function.convert-uudecode.php" title="convert_​uudecode">convert_​uudecode</a> </li> <li class=""> <a href="function.convert-uuencode.php" title="convert_​uuencode">convert_​uuencode</a> </li> <li class=""> <a href="function.count-chars.php" title="count_​chars">count_​chars</a> </li> <li class=""> <a href="function.crc32.php" title="crc32">crc32</a> </li> <li class=""> <a href="function.crypt.php" title="crypt">crypt</a> </li> <li class=""> <a href="function.echo.php" title="echo">echo</a> </li> <li class=""> <a href="function.explode.php" title="explode">explode</a> </li> <li class=""> <a href="function.fprintf.php" title="fprintf">fprintf</a> </li> <li class=""> <a href="function.get-html-translation-table.php" title="get_​html_​translation_​table">get_​html_​translation_​table</a> </li> <li class=""> <a href="function.hebrev.php" title="hebrev">hebrev</a> </li> <li class=""> <a href="function.hex2bin.php" title="hex2bin">hex2bin</a> </li> <li class=""> <a href="function.html-entity-decode.php" title="html_​entity_​decode">html_​entity_​decode</a> </li> <li class=""> <a href="function.htmlentities.php" title="htmlentities">htmlentities</a> </li> <li class=""> <a href="function.htmlspecialchars.php" title="htmlspecialchars">htmlspecialchars</a> </li> <li class=""> <a href="function.htmlspecialchars-decode.php" title="htmlspecialchars_​decode">htmlspecialchars_​decode</a> </li> <li class=""> <a href="function.implode.php" title="implode">implode</a> </li> <li class=""> <a href="function.join.php" title="join">join</a> </li> <li class=""> <a href="function.lcfirst.php" title="lcfirst">lcfirst</a> </li> <li class=""> <a href="function.levenshtein.php" title="levenshtein">levenshtein</a> </li> <li class=""> <a href="function.localeconv.php" title="localeconv">localeconv</a> </li> <li class=""> <a href="function.ltrim.php" title="ltrim">ltrim</a> </li> <li class=""> <a href="function.md5.php" title="md5">md5</a> </li> <li class=""> <a href="function.md5-file.php" title="md5_​file">md5_​file</a> </li> <li class=""> <a href="function.metaphone.php" title="metaphone">metaphone</a> </li> <li class=""> <a href="function.money-format.php" title="money_​format">money_​format</a> </li> <li class=""> <a href="function.nl-langinfo.php" title="nl_​langinfo">nl_​langinfo</a> </li> <li class=""> <a href="function.nl2br.php" title="nl2br">nl2br</a> </li> <li class=""> <a href="function.number-format.php" title="number_​format">number_​format</a> </li> <li class=""> <a href="function.ord.php" title="ord">ord</a> </li> <li class=""> <a href="function.parse-str.php" title="parse_​str">parse_​str</a> </li> <li class=""> <a href="function.print.php" title="print">print</a> </li> <li class=""> <a href="function.printf.php" title="printf">printf</a> </li> <li class=""> <a href="function.quoted-printable-decode.php" title="quoted_​printable_​decode">quoted_​printable_​decode</a> </li> <li class=""> <a href="function.quoted-printable-encode.php" title="quoted_​printable_​encode">quoted_​printable_​encode</a> </li> <li class=""> <a href="function.quotemeta.php" title="quotemeta">quotemeta</a> </li> <li class=""> <a href="function.rtrim.php" title="rtrim">rtrim</a> </li> <li class=""> <a href="function.setlocale.php" title="setlocale">setlocale</a> </li> <li class=""> <a href="function.sha1.php" title="sha1">sha1</a> </li> <li class=""> <a href="function.sha1-file.php" title="sha1_​file">sha1_​file</a> </li> <li class=""> <a href="function.similar-text.php" title="similar_​text">similar_​text</a> </li> <li class=""> <a href="function.soundex.php" title="soundex">soundex</a> </li> <li class=""> <a href="function.sprintf.php" title="sprintf">sprintf</a> </li> <li class=""> <a href="function.sscanf.php" title="sscanf">sscanf</a> </li> <li class=""> <a href="function.str-contains.php" title="str_​contains">str_​contains</a> </li> <li class=""> <a href="function.str-decrement.php" title="str_​decrement">str_​decrement</a> </li> <li class=""> <a href="function.str-ends-with.php" title="str_​ends_​with">str_​ends_​with</a> </li> <li class=""> <a href="function.str-getcsv.php" title="str_​getcsv">str_​getcsv</a> </li> <li class=""> <a href="function.str-increment.php" title="str_​increment">str_​increment</a> </li> <li class=""> <a href="function.str-ireplace.php" title="str_​ireplace">str_​ireplace</a> </li> <li class=""> <a href="function.str-pad.php" title="str_​pad">str_​pad</a> </li> <li class=""> <a href="function.str-repeat.php" title="str_​repeat">str_​repeat</a> </li> <li class=""> <a href="function.str-replace.php" title="str_​replace">str_​replace</a> </li> <li class=""> <a href="function.str-rot13.php" title="str_​rot13">str_​rot13</a> </li> <li class=""> <a href="function.str-shuffle.php" title="str_​shuffle">str_​shuffle</a> </li> <li class=""> <a href="function.str-split.php" title="str_​split">str_​split</a> </li> <li class=""> <a href="function.str-starts-with.php" title="str_​starts_​with">str_​starts_​with</a> </li> <li class=""> <a href="function.str-word-count.php" title="str_​word_​count">str_​word_​count</a> </li> <li class="current"> <a href="function.strcasecmp.php" title="strcasecmp">strcasecmp</a> </li> <li class=""> <a href="function.strchr.php" title="strchr">strchr</a> </li> <li class=""> <a href="function.strcmp.php" title="strcmp">strcmp</a> </li> <li class=""> <a href="function.strcoll.php" title="strcoll">strcoll</a> </li> <li class=""> <a href="function.strcspn.php" title="strcspn">strcspn</a> </li> <li class=""> <a href="function.strip-tags.php" title="strip_​tags">strip_​tags</a> </li> <li class=""> <a href="function.stripcslashes.php" title="stripcslashes">stripcslashes</a> </li> <li class=""> <a href="function.stripos.php" title="stripos">stripos</a> </li> <li class=""> <a href="function.stripslashes.php" title="stripslashes">stripslashes</a> </li> <li class=""> <a href="function.stristr.php" title="stristr">stristr</a> </li> <li class=""> <a href="function.strlen.php" title="strlen">strlen</a> </li> <li class=""> <a href="function.strnatcasecmp.php" title="strnatcasecmp">strnatcasecmp</a> </li> <li class=""> <a href="function.strnatcmp.php" title="strnatcmp">strnatcmp</a> </li> <li class=""> <a href="function.strncasecmp.php" title="strncasecmp">strncasecmp</a> </li> <li class=""> <a href="function.strncmp.php" title="strncmp">strncmp</a> </li> <li class=""> <a href="function.strpbrk.php" title="strpbrk">strpbrk</a> </li> <li class=""> <a href="function.strpos.php" title="strpos">strpos</a> </li> <li class=""> <a href="function.strrchr.php" title="strrchr">strrchr</a> </li> <li class=""> <a href="function.strrev.php" title="strrev">strrev</a> </li> <li class=""> <a href="function.strripos.php" title="strripos">strripos</a> </li> <li class=""> <a href="function.strrpos.php" title="strrpos">strrpos</a> </li> <li class=""> <a href="function.strspn.php" title="strspn">strspn</a> </li> <li class=""> <a href="function.strstr.php" title="strstr">strstr</a> </li> <li class=""> <a href="function.strtok.php" title="strtok">strtok</a> </li> <li class=""> <a href="function.strtolower.php" title="strtolower">strtolower</a> </li> <li class=""> <a href="function.strtoupper.php" title="strtoupper">strtoupper</a> </li> <li class=""> <a href="function.strtr.php" title="strtr">strtr</a> </li> <li class=""> <a href="function.substr.php" title="substr">substr</a> </li> <li class=""> <a href="function.substr-compare.php" title="substr_​compare">substr_​compare</a> </li> <li class=""> <a href="function.substr-count.php" title="substr_​count">substr_​count</a> </li> <li class=""> <a href="function.substr-replace.php" title="substr_​replace">substr_​replace</a> </li> <li class=""> <a href="function.trim.php" title="trim">trim</a> </li> <li class=""> <a href="function.ucfirst.php" title="ucfirst">ucfirst</a> </li> <li class=""> <a href="function.ucwords.php" title="ucwords">ucwords</a> </li> <li class=""> <a href="function.vfprintf.php" title="vfprintf">vfprintf</a> </li> <li class=""> <a href="function.vprintf.php" title="vprintf">vprintf</a> </li> <li class=""> <a href="function.vsprintf.php" title="vsprintf">vsprintf</a> </li> <li class=""> <a href="function.wordwrap.php" title="wordwrap">wordwrap</a> </li> </ul> </li> <li> <span class="header">Deprecated</span> <ul class="child-menu-list"> <li class=""> <a href="function.convert-cyr-string.php" title="convert_​cyr_​string">convert_​cyr_​string</a> </li> <li class=""> <a href="function.hebrevc.php" title="hebrevc">hebrevc</a> </li> <li class=""> <a href="function.utf8-decode.php" title="utf8_​decode">utf8_​decode</a> </li> <li class=""> <a href="function.utf8-encode.php" title="utf8_​encode">utf8_​encode</a> </li> </ul> </li> </ul> </aside> </div><!-- layout --> <footer> <div class="container footer-content"> <div class="row-fluid"> <ul class="footmenu"> <li><a href="/manual/en/copyright.php">Copyright © 2001-2024 The PHP Documentation Group</a></li> <li><a href="/my.php">My PHP.net</a></li> <li><a href="/contact.php">Contact</a></li> <li><a href="/sites.php">Other PHP.net sites</a></li> <li><a href="/privacy.php">Privacy policy</a></li> </ul> </div> </div> </footer> <script src="/cached.php?t=1731172202&f=/js/ext/jquery-3.6.0.min.js"></script> <script src="/cached.php?t=1723177202&f=/js/ext/FuzzySearch.min.js"></script> <script src="/cached.php?t=1707321815&f=/js/ext/mousetrap.min.js"></script> <script src="/cached.php?t=1707321815&f=/js/ext/jquery.scrollTo.min.js"></script> <script src="/cached.php?t=1730558402&f=/js/search.js"></script> <script src="/cached.php?t=1732876201&f=/js/common.js"></script> <script type="module" src="/cached.php?t=1733276402&f=/js/interactive-examples.js"></script> <a id="toTop" href="javascript:;"><span id="toTopHover"></span><img width="40" height="40" alt="To Top" src="/images/to-top@2x.png"></a> <div id="search-modal__backdrop" class="search-modal__backdrop"> <div role="dialog" aria-label="Search modal" id="search-modal" class="search-modal" > <div class="search-modal__header"> <div class="search-modal__form"> <div class="search-modal__input-icon"> <!-- https://feathericons.com search --> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </div> <input type="search" id="search-modal__input" class="search-modal__input" placeholder="Search docs" aria-label="Search docs" /> </div> <button aria-label="Close" class="search-modal__close"> <!-- https://pictogrammers.com/library/mdi/icon/close/ --> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" > <path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z"/> </svg> </button> </div> <div role="listbox" aria-label="Search results" id="search-modal__results" class="search-modal__results" ></div> <div class="search-modal__helper-text"> <div> <kbd>↑</kbd> and <kbd>↓</kbd> to navigate • <kbd>Enter</kbd> to select • <kbd>Esc</kbd> to close </div> <div> Press <kbd>Enter</kbd> without selection to search using Google </div> </div> </div> </div> </body> </html>