<!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>Output one or more strings</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.crypt.html">crypt</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.explode.html">explode</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.echo" class="refentry"> <div class="refnamediv"> <h1 class="refname">echo</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">echo</span> — <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="type"><span class="type void">void</span></span> <span class="methodname"><strong>echo</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$arg1</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$...</code></span> ] )</div> <p class="simpara"> Outputs all parameters. </p> <p class="para"> <em>echo</em> is not actually a function (it is a language construct), so you are not required to use parentheses with it. <em>echo</em> (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to <em>echo</em>, the parameters must not be enclosed within parentheses. </p> <p class="para"> <em>echo</em> also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0, this short syntax only works with the <a href="ini.core.html#ini.short-open-tag" class="link">short_open_tag</a> configuration setting enabled. <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> I have <span style="color: #0000BB"><?=$foo?></span> foo.</span> </code></div> </div> </div> </p> </div> <div class="refsect1 parameters" id="refsect1-function.echo-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">arg1</code></em></span> <dd> <p class="para"> The parameter to output. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">...</code></em></span> <dd> <p class="para"> </p> </dd> </dt> </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-4827"> <p><strong>Example #1 <em>echo</em> examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Hello World"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"This 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 spans\nmultiple lines. The newlines will be\noutput as well."</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"Escaping characters is done \"Like this\"."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// You can use variables inside of an echo statement<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">"foobar"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= </span><span style="color: #DD0000">"barbaz"</span><span style="color: #007700">;<br /><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 foobar<br /><br />// You can also use arrays<br /></span><span style="color: #0000BB">$baz </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"value" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"this is </span><span style="color: #007700">{</span><span style="color: #0000BB">$baz</span><span style="color: #007700">[</span><span style="color: #DD0000">'value'</span><span style="color: #007700">]}</span><span style="color: #DD0000"> !"</span><span style="color: #007700">; </span><span style="color: #FF8000">// this is foo !<br /><br />// Using single quotes will print the variable name, not the value<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'foo is $foo'</span><span style="color: #007700">; </span><span style="color: #FF8000">// foo is $foo<br /><br />// If you are not using any other characters, you can just echo variables<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">; </span><span style="color: #FF8000">// foobar<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">,</span><span style="color: #0000BB">$bar</span><span style="color: #007700">; </span><span style="color: #FF8000">// foobarbarbaz<br /><br />// Some people prefer passing multiple parameters to echo over concatenation.<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: #0000BB">chr</span><span style="color: #007700">(</span><span style="color: #0000BB">10</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 />echo <<<END<br /></span><span style="color: #DD0000">This uses the "here document" syntax to output<br />multiple lines with </span><span style="color: #0000BB">$variable</span><span style="color: #DD0000"> interpolation. Note<br />that the here document terminator must appear on a<br />line with just a semicolon. no extra whitespace!<br /></span><span style="color: #007700">END;<br /><br /></span><span style="color: #FF8000">// Because echo does not behave like a function, 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 /><br /></span><span style="color: #FF8000">// 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 behaves like a function, so<br /> // it may be used in this context.<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">// changing the statement around<br /></span><span style="color: #0000BB">?></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.html" class="link">variable functions</a>.</span> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.echo-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.print.html" class="function" rel="rdfs-seeAlso">print</a> - Output a string</span></li> <li class="member"> <span class="function"><a href="function.printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - Output a formatted string</span></li> <li class="member"> <span class="function"><a href="function.flush.html" class="function" rel="rdfs-seeAlso">flush()</a> - Flush the output buffer</span></li> <li class="member"><a href="language.types.string.html#language.types.string.syntax.heredoc" class="link">Heredoc syntax</a></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.crypt.html">crypt</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.explode.html">explode</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>