<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.strings.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.echo.php',
    1 => 'echo',
    2 => 'Output one or more strings',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'String Functions',
  ),
  'prev' => 
  array (
    0 => 'function.crypt.php',
    1 => 'crypt',
  ),
  'next' => 
  array (
    0 => 'function.explode.php',
    1 => 'explode',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/strings/functions/echo.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.echo" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">echo</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">echo</span> &mdash; <span class="dc-title">Output one or more strings</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.echo-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>echo</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">...$expressions</code></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

  <p class="simpara">
   Outputs one or more expressions, with no additional newlines or spaces.
  </p>
  <p class="para">
   <code class="literal">echo</code> is not a function but a language construct.
   Its arguments are a list of expressions following the <code class="literal">echo</code>
   keyword, separated by commas, and not delimited by parentheses.
   Unlike some other language constructs, <code class="literal">echo</code> does not have
   any return value, so it cannot be used in the context of an expression.
  </p>
  <p class="para">
   <code class="literal">echo</code> also has a shortcut syntax, where you can
   immediately follow the opening tag with an equals sign. This syntax is available
   even with the <a href="ini.core.php#ini.short-open-tag" class="link">short_open_tag</a> configuration
   setting disabled.
   <div class="informalexample">
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000">I have <span style="color: #0000BB">&lt;?=$foo?&gt;</span> foo.</span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   The major differences to <span class="function"><a href="function.print.php" class="function">print</a></span> are that
   <code class="literal">echo</code> accepts multiple arguments and doesn&#039;t have a return value.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.echo-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">expressions</code></dt>
     <dd>
      <p class="para">
       One or more string expressions to output, separated by commas.
       Non-string values will be coerced to strings, even when
       <a href="language.types.declarations.php#language.types.declarations.strict" class="link">the
       <code class="literal">strict_types</code> directive</a> is enabled.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.echo-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   No value is returned.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.echo-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <code class="literal">echo</code> examples</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"echo does not require parentheses."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Strings can either be passed individually as multiple arguments or<br />// concatenated together and passed as a single argument<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'This '</span><span style="color: #007700">, </span><span style="color: #DD0000">'string '</span><span style="color: #007700">, </span><span style="color: #DD0000">'was '</span><span style="color: #007700">, </span><span style="color: #DD0000">'made '</span><span style="color: #007700">, </span><span style="color: #DD0000">'with multiple parameters.'</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">'This ' </span><span style="color: #007700">. </span><span style="color: #DD0000">'string ' </span><span style="color: #007700">. </span><span style="color: #DD0000">'was ' </span><span style="color: #007700">. </span><span style="color: #DD0000">'made ' </span><span style="color: #007700">. </span><span style="color: #DD0000">'with concatenation.' </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// No newline or space is added; the below outputs "helloworld" all on one line<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"world"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Same as above<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">, </span><span style="color: #DD0000">"world"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"This string spans<br />multiple lines. The newlines will be<br />output as well"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"This string spans\nmultiple lines. The newlines will be\noutput as well."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// The argument can be any expression which produces a string<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">"example"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"foo is </span><span style="color: #0000BB">$foo</span><span style="color: #DD0000">"</span><span style="color: #007700">; </span><span style="color: #FF8000">// foo is example<br /><br /></span><span style="color: #0000BB">$fruits </span><span style="color: #007700">= [</span><span style="color: #DD0000">"lemon"</span><span style="color: #007700">, </span><span style="color: #DD0000">"orange"</span><span style="color: #007700">, </span><span style="color: #DD0000">"banana"</span><span style="color: #007700">];<br />echo </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">" and "</span><span style="color: #007700">, </span><span style="color: #0000BB">$fruits</span><span style="color: #007700">); </span><span style="color: #FF8000">// lemon and orange and banana<br /><br />// Non-string expressions are coerced to string, even if declare(strict_types=1) is used<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">6 </span><span style="color: #007700">* </span><span style="color: #0000BB">7</span><span style="color: #007700">; </span><span style="color: #FF8000">// 42<br /><br />// However, the following examples will work:<br /></span><span style="color: #007700">(</span><span style="color: #0000BB">$some_var</span><span style="color: #007700">) ? print </span><span style="color: #DD0000">'true' </span><span style="color: #007700">: print </span><span style="color: #DD0000">'false'</span><span style="color: #007700">; </span><span style="color: #FF8000">// print is also a construct, but<br />                                            // it is a valid expression, returning 1,<br />                                            // so it may be used in this context.<br /><br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$some_var </span><span style="color: #007700">? </span><span style="color: #DD0000">'true'</span><span style="color: #007700">: </span><span style="color: #DD0000">'false'</span><span style="color: #007700">; </span><span style="color: #FF8000">// evaluating the expression first and passing it to echo<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Example #2 <code class="literal">echo</code> is not an expression</strong></p>
    <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Because echo does not behave as an expression, the following code is invalid.<br /></span><span style="color: #007700">(</span><span style="color: #0000BB">$some_var</span><span style="color: #007700">) ? echo </span><span style="color: #DD0000">'true' </span><span style="color: #007700">: echo </span><span style="color: #DD0000">'false'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.echo-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">Because this is a
language construct and not a function, it cannot be called using
<a href="functions.variable-functions.php" class="link">variable functions</a>,
or <a href="functions.arguments.php#functions.named-arguments" class="link">named arguments</a>.</span>
</p></blockquote>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <strong>Using with parentheses</strong><br />
   <p class="para">
    Surrounding a single argument to <code class="literal">echo</code> with parentheses will not
    raise a syntax error, and produces syntax which looks like a normal
    function call. However, this can be misleading, because the parentheses are actually
    part of the expression being output, not part of the <code class="literal">echo</code>
    syntax itself.

    <div class="example" id="example-3">
     <p><strong>Example #3 Using Parentheses</strong></p>
     <div class="example-contents">
     <div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// outputs "hello"<br /><br /></span><span style="color: #007700">echo(</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// also outputs "hello", because ("hello") is a valid expression<br /><br /></span><span style="color: #007700">echo(</span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #0000BB">2</span><span style="color: #007700">) * </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// outputs "9"; the parentheses cause 1+2 to be evaluated first, then 3*3<br />// the echo statement sees the whole expression as one argument<br /><br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">, </span><span style="color: #DD0000">" world"</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// outputs "hello world"<br /><br /></span><span style="color: #007700">echo(</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">), (</span><span style="color: #DD0000">" world"</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// outputs "hello world"; the parentheses are part of each expression<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>

  <p class="para">
    <div class="example" id="example-4">
     <p><strong>Example #4 Invalid Expression</strong></p>
     <div class="example-contents">
     <div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo(</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">, </span><span style="color: #DD0000">" world"</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// Throws a Parse Error because ("hello", " world") is not a valid expression<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  </p></blockquote>

  <div class="tip"><strong class="tip">Tip</strong>
   <p class="para">
    Passing multiple arguments to <code class="literal">echo</code> can avoid
    complications arising from the precedence of the concatenation operator in
    PHP. For instance, the concatenation operator has higher precedence than
    the ternary operator, and prior to PHP 8.0.0 had the same precedence as addition
    and subtraction:
   </p>
   <div class="example-contents">
    <div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Below, the expression 'Hello ' . isset($name) is evaluated first,<br />// and is always true, so the argument to echo is always $name<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Hello ' </span><span style="color: #007700">. isset(</span><span style="color: #0000BB">$name</span><span style="color: #007700">) ? </span><span style="color: #0000BB">$name </span><span style="color: #007700">: </span><span style="color: #DD0000">'John Doe' </span><span style="color: #007700">. </span><span style="color: #DD0000">'!'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// The intended behaviour requires additional parentheses<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Hello ' </span><span style="color: #007700">. (isset(</span><span style="color: #0000BB">$name</span><span style="color: #007700">) ? </span><span style="color: #0000BB">$name </span><span style="color: #007700">: </span><span style="color: #DD0000">'John Doe'</span><span style="color: #007700">) . </span><span style="color: #DD0000">'!'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// In PHP prior to 8.0.0, the below outputs "2", rather than "Sum: 3"<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Sum: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Again, adding parentheses ensures the intended order of evaluation<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Sum: ' </span><span style="color: #007700">. (</span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #0000BB">2</span><span style="color: #007700">);</span></span></code></div>
   </div>

   <p class="para">
    If multiple arguments are passed in, then parentheses will not be
    required to enforce precedence, because each expression is separate:
   </p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Hello "</span><span style="color: #007700">, isset(</span><span style="color: #0000BB">$name</span><span style="color: #007700">) ? </span><span style="color: #0000BB">$name </span><span style="color: #007700">: </span><span style="color: #DD0000">"John Doe"</span><span style="color: #007700">, </span><span style="color: #DD0000">"!"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"Sum: "</span><span style="color: #007700">, </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #0000BB">2</span><span style="color: #007700">;</span></span></code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.echo-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.print.php" class="function" rel="rdfs-seeAlso">print</a> - Output a string</span></li>
    <li><span class="function"><a href="function.printf.php" class="function" rel="rdfs-seeAlso">printf()</a> - Output a formatted string</span></li>
    <li><span class="function"><a href="function.flush.php" class="function" rel="rdfs-seeAlso">flush()</a> - Flush system output buffer</span></li>
    <li><a href="language.types.string.php" class="link">Ways to specify literal strings</a></li>
   </ul>
  </p>
 </div>


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