<!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>Registers a User Defined Function for use as a collating function in SQL statements</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pdo.sqlitecreateaggregate.html">PDO::sqliteCreateAggregate</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdo.sqlitecreatefunction.html">PDO::sqliteCreateFunction</a></div> <div class="up"><a href="ref.pdo-sqlite.html">SQLite (PDO)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="pdo.sqlitecreatecollation" class="refentry"> <div class="refnamediv"> <h1 class="refname">PDO::sqliteCreateCollation</h1> <p class="verinfo">(PHP 5 >= 5.3.11, PHP 7)</p><p class="refpurpose"><span class="refname">PDO::sqliteCreateCollation</span> — <span class="dc-title"> Registers a User Defined Function for use as a collating function in SQL statements </span></p> </div> <div class="refsect1 description" id="refsect1-pdo.sqlitecreatecollation-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">bool</span> <span class="methodname"><strong>PDO::sqliteCreateCollation</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span> , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span> )</div> <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function is <em class="emphasis">EXPERIMENTAL</em>. The behaviour of this function, its name, and surrounding documentation may change without notice in a future release of PHP. This function should be used at your own risk. </p></div> </div> <div class="refsect1 parameters" id="refsect1-pdo.sqlitecreatecollation-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">name</code></dt> <dd> <p class="para"> Name of the SQL collating function to be created or redefined. </p> </dd> <dt> <code class="parameter">callback</code></dt> <dd> <p class="para"> The name of a PHP function or user-defined function to apply as a callback, defining the behavior of the collation. It should accept two strings and return as strcmp() does, i.e. it should return -1, 1, or 0 if the first string sorts before, sorts after, or is equal to the second. </p> <p class="para"> This function need to be defined as: <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><span class="replaceable">collation</span></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$string1</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$string2</code></span> )</div> </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-pdo.sqlitecreatecollation-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 examples" id="refsect1-pdo.sqlitecreatecollation-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-1113"> <p><strong>Example #1 <span class="function"><strong>PDO::sqliteCreateCollation()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$db </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'sqlite::memory:'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"CREATE TABLE test (col1 string)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT INTO test VALUES ('a1')"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT INTO test VALUES ('a10')"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT INTO test VALUES ('a2')"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">sqliteCreateCollation</span><span style="color: #007700">(</span><span style="color: #DD0000">'NATURAL_CMP'</span><span style="color: #007700">, </span><span style="color: #DD0000">'strnatcmp'</span><span style="color: #007700">);<br />foreach (</span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT col1 FROM test ORDER BY col1"</span><span style="color: #007700">) as </span><span style="color: #0000BB">$row</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'col1'</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br />echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />foreach (</span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT col1 FROM test ORDER BY col1 COLLATE NATURAL_CMP"</span><span style="color: #007700">) as </span><span style="color: #0000BB">$row</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'col1'</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 class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> a1 a10 a2 a1 a2 a10 </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-pdo.sqlitecreatecollation-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> This method is not available with the SQLite2 driver. Use the old style sqlite API for that instead. </p> </p></blockquote> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pdo.sqlitecreateaggregate.html">PDO::sqliteCreateAggregate</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdo.sqlitecreatefunction.html">PDO::sqliteCreateFunction</a></div> <div class="up"><a href="ref.pdo-sqlite.html">SQLite (PDO)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>