<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.operators.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.operators.functional.php',
    1 => 'Functional',
    2 => 'Functional Operators',
  ),
  'up' => 
  array (
    0 => 'language.operators.php',
    1 => 'Operators',
  ),
  'prev' => 
  array (
    0 => 'language.operators.type.php',
    1 => 'Type',
  ),
  'next' => 
  array (
    0 => 'language.control-structures.php',
    1 => 'Control Structures',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/operators/functional.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.operators.functional" class="sect1">
 <h2 class="title">Functional Operators</h2>
 
 <p class="para">
  PHP 8.5 and later supports one operator that works directly on callables.  The <code class="literal">|&gt;</code>
  operator, or “pipe,” accepts a single-parameter callable on the right and passes
  the left-side value to it, evaluating to the callable&#039;s result.  The callable
  on the right may be any valid PHP callable: a <span class="classname"><a href="class.closure.php" class="classname">Closure</a></span>,
  a <a href="functions.first_class_callable_syntax.php" class="link">first-class callable</a>,
  an object that implements <a href="language.oop5.magic.php#object.invoke" class="link">__invoke()</a>, etc.
 </p>
 <p class="para">
  That means the following two lines are logically equivalent.
  <div class="example" id="example-1">
   <p><strong>Example #1 Using <code class="literal">|&gt;</code></strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$result </span><span style="color: #007700">= </span><span style="color: #DD0000">"Hello World" </span><span style="color: #007700">|&gt; </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(...);<br />echo </span><span style="color: #0000BB">$result</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #DD0000">"Hello World"</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$result</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
11
11
</pre></div>
   </div>
  </div>
 </p>
 <p class="para">
  For a single call that is not especially useful. It becomes useful when multiple calls are chained together.
  That is, the following two code fragments are logically equivalent:
  <div class="example" id="example-2">
   <p><strong>Example #2 Chaining |&gt; calls</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$result </span><span style="color: #007700">= </span><span style="color: #DD0000">"PHP Rocks"<br />    </span><span style="color: #007700">|&gt; </span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(...)<br />    |&gt; </span><span style="color: #0000BB">str_split</span><span style="color: #007700">(...)<br />    |&gt; (fn(</span><span style="color: #0000BB">$x</span><span style="color: #007700">) =&gt; </span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(...), </span><span style="color: #0000BB">$x</span><span style="color: #007700">))<br />    |&gt; (fn(</span><span style="color: #0000BB">$x</span><span style="color: #007700">) =&gt; </span><span style="color: #0000BB">array_filter</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">, fn(</span><span style="color: #0000BB">$v</span><span style="color: #007700">) =&gt; </span><span style="color: #0000BB">$v </span><span style="color: #007700">!= </span><span style="color: #DD0000">'O'</span><span style="color: #007700">))<br />;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$temp </span><span style="color: #007700">= </span><span style="color: #DD0000">"PHP Rocks"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$temp </span><span style="color: #007700">= </span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$temp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$temp </span><span style="color: #007700">= </span><span style="color: #0000BB">str_split</span><span style="color: #007700">(</span><span style="color: #0000BB">$temp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$temp </span><span style="color: #007700">= </span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(...), </span><span style="color: #0000BB">$temp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$temp </span><span style="color: #007700">= </span><span style="color: #0000BB">array_filter</span><span style="color: #007700">(</span><span style="color: #0000BB">$temp</span><span style="color: #007700">, fn(</span><span style="color: #0000BB">$v</span><span style="color: #007700">) =&gt; </span><span style="color: #0000BB">$v </span><span style="color: #007700">!= </span><span style="color: #DD0000">'O'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$temp</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Array
(
    [0] =&gt; P
    [1] =&gt; H
    [2] =&gt; P
    [3] =&gt;
    [4] =&gt; R
    [6] =&gt; C
    [7] =&gt; K
    [8] =&gt; S
)
Array
(
    [0] =&gt; P
    [1] =&gt; H
    [2] =&gt; P
    [3] =&gt;
    [4] =&gt; R
    [6] =&gt; C
    [7] =&gt; K
    [8] =&gt; S
)
</pre></div>
   </div>
  </div>
 </p>
 <p class="para">
  The left-hand side of the pipe may be any value or expression.  The right-hand side
  may be any valid PHP callable that takes a single parameter, or any expression
  that evaluates to such a callable.  Functions with more than one required parameter
  are not allowed and will fail as if the function were called normally
  with insufficient arguments. Functions that take a variable by reference are not allowed.
  If the right-hand side does not evaluate to a valid callable it will throw an Error.
 </p>
 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   Be aware that, to avoid syntax ambiguity, <a href="functions.arrow.php" class="link">arrow functions</a>
   MUST be wrapped in parentheses when used with a pipe operator, as in the examples above.
   Failing to do so will result in a fatal error.
  </p>
 </p></blockquote>

 <div class="sect2">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="classname"><a href="class.closure.php" class="classname">Closure</a></span></li>
   </ul>
  </p>
 </div>
</div><?php manual_footer($setup); ?>