<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.pdo-sqlite.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'pdo-sqlite.createfunction.php',
    1 => 'Pdo\\Sqlite::createFunction',
    2 => 'Registers a user-defined function for use in SQL statements',
  ),
  'up' => 
  array (
    0 => 'class.pdo-sqlite.php',
    1 => 'Pdo\\Sqlite',
  ),
  'prev' => 
  array (
    0 => 'pdo-sqlite.createcollation.php',
    1 => 'Pdo\\Sqlite::createCollation',
  ),
  'next' => 
  array (
    0 => 'pdo-sqlite.loadextension.php',
    1 => 'Pdo\\Sqlite::loadExtension',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pdo_sqlite/pdo/sqlite/createfunction.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="pdo-sqlite.createfunction" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Pdo\Sqlite::createFunction</h1>
  <p class="verinfo">(PHP 8 &gt;= 8.4.0)</p><p class="refpurpose"><span class="refname">Pdo\Sqlite::createFunction</span> &mdash; <span class="dc-title">
   Registers a user-defined function for use in SQL statements
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-pdo-sqlite.createfunction-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Pdo\Sqlite::createFunction</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$function_name</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$num_args</code><span class="initializer"> = -1</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span><br>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="simpara">
   This method allows PHP function to be registered with SQLite as a
   user-defined function, so that it can be called within SQL queries.
   The defined function can be used in any SQL query that allows function calls,
   for example <code class="literal">SELECT</code>, <code class="literal">UPDATE</code>, or triggers.
  </p>
  <div class="tip"><strong class="tip">Підказка</strong>
   <p class="simpara">
    By using this method it is possible to override native SQL functions.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-pdo-sqlite.createfunction-parameters">
  <h3 class="title">Параметри</h3>
  <dl>
   
    <dt><code class="parameter">function_name</code></dt>
    <dd>
     <span class="simpara">
      The name of the function used in SQL statements.
     </span>
    </dd>
   
   
    <dt><code class="parameter">callback</code></dt>
    <dd>
     <span class="simpara">
      Callback function to handle the defined SQL function.
     </span>
     <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
      <span class="simpara">
       Callback functions should return a type understood by SQLite (i.e.
       <a href="language.types.intro.php" class="link">scalar type</a>).
      </span>
     </p></blockquote>
     <p class="para">
      This function need to be defined as:
      <div class="methodsynopsis dc-description">
       <span class="methodname"><span class="replaceable">callback</span></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">...$values</code></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

      <dl>
       
        <dt><code class="parameter">value</code></dt>
        <dd>
         <p class="para">
          The first argument passed to the SQL function.
         </p>
        </dd>
       
       
        <dt><code class="parameter">values</code></dt>
        <dd>
         <p class="para">
          Further arguments passed to the SQL function.
         </p>
        </dd>
       
      </dl>
     </p>
    </dd>
   
   
    <dt><code class="parameter">num_args</code></dt>
    <dd>
     <span class="simpara">
      The number of arguments that the SQL function takes.
      If this parameter is <code class="literal">-1</code>,
      then the SQL function may take any number of arguments.
     </span>
    </dd>
   
   
    <dt><code class="parameter">flags</code></dt>
    <dd>
     <span class="simpara">
      A bitmask of flags.
      Currently, only <strong><code><a href="class.pdo-sqlite.php#pdo-sqlite.constants.deterministic">Pdo\Sqlite::DETERMINISTIC</a></code></strong> is supported,
      which specifies that the function always returns the same result given
      the same inputs within a single SQL statement.
     </span>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdo-sqlite.createfunction-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="simpara">
   Повертає <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> у разі успіху або <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> в разі помилки.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdo-sqlite.createfunction-examples">
  <h3 class="title">Приклади</h3>
  <div class="example" id="pdo-sqlite.createfunction.example.basic">
   <p><strong>Приклад #1 <span class="methodname"><strong>Pdo\Sqlite::createFunction()</strong></span> example</strong></p>
   <div class="example-contents"><p>
    In this example, we have a function that calculates the SHA256 sum of a
    string, and then reverses it.  When the SQL statement executes, it
    returns the value of the filename transformed by our function.
    The data returned in <var class="varname">$rows</var> contains the processed result.
   </p></div>
   <div class="example-contents"><p>
    The beauty of this technique is that there is no need to process the
    result using a <a href="control-structures.foreach.php" class="link">foreach</a> loop after the query.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">sha256_and_reverse</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)<br />{<br />    return </span><span style="color: #0000BB">strrev</span><span style="color: #007700">(</span><span style="color: #0000BB">hash</span><span style="color: #007700">(</span><span style="color: #DD0000">'sha256'</span><span style="color: #007700">, </span><span style="color: #0000BB">$string</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">$db </span><span style="color: #007700">= new </span><span style="color: #0000BB">Pdo\Sqlite</span><span style="color: #007700">(</span><span style="color: #DD0000">'sqlite::sqlitedb'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sqliteCreateFunction</span><span style="color: #007700">(</span><span style="color: #DD0000">'sha256rev'</span><span style="color: #007700">, </span><span style="color: #DD0000">'sha256_and_reverse'</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rows </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT sha256rev(filename) FROM files'</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdo-sqlite.createfunction-seealso">
  <h3 class="title">Прогляньте також</h3>
  <ul class="simplelist">
   <li><span class="methodname"><a href="pdo-sqlite.createaggregate.php" class="methodname" rel="rdfs-seeAlso">Pdo\Sqlite::createAggregate()</a> - Registers an aggregating user-defined function for use in SQL statements</span></li>
   <li><span class="methodname"><a href="pdo-sqlite.createcollation.php" class="methodname" rel="rdfs-seeAlso">Pdo\Sqlite::createCollation()</a> - Registers a user-defined function for use as a collating function in SQL statements</span></li>
   <li><span class="function"><strong>sqlite_create_function()</strong></span></li>
   <li><span class="function"><strong>sqlite_create_aggregate()</strong></span></li>
  </ul>
 </div>


</div><?php manual_footer($setup); ?>