PHP echo function

echo

(PHP 3, PHP 4, PHP 5)

echo -- Output one or more stringsDescriptionvoid echo ( string arg1 [, string ...] )

Outputs all parameters.

echo() is not actually a function (it is a
language construct), so you are not required to use parentheses
with it. echo() (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 echo(), the parameters
must not be enclosed within parentheses.

Example 1. echo() examples

PHP:
  1. echo "Hello World";
  2.  
  3. echo "This spans
  4. multiple lines. The newlines will be
  5. output as well";
  6.  
  7. echo "This spans\nmultiple lines. The newlines will be\noutput as well.";
  8.  
  9. echo "Escaping characters is done \"Like this\".";
  10.  
  11. // You can use variables inside of an echo statement
  12. $foo = "foobar";
  13. $bar = "barbaz";
  14.  
  15. echo "foo is $foo"; // foo is foobar
  16.  
  17. // You can also use arrays
  18. $bar = array("value" => "foo");
  19.  
  20. echo "this is {$bar['value']} !"; // this is foo !
  21.  
  22. // Using single quotes will print the variable name, not the value
  23. echo 'foo is $foo'; // foo is $foo
  24.  
  25. // If you are not using any other characters, you can just echo variables
  26. echo $foo;          // foobar
  27. echo $foo,$bar;     // foobarbarbaz
  28.  
  29. // Some people prefer passing multiple parameters to echo over concatenation.
  30. echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);
  31. echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";
  32.  
  33. echo <<<END
  34. This uses the "here document" syntax to output
  35. multiple lines with $variable interpolation. Note
  36. that the here document terminator must appear on a
  37. line with just a semicolon. no extra whitespace!
  38.  
  39. // Because echo does not behave like a function, the following code is invalid.
  40. ($some_var) ? echo 'true' : echo 'false';
  41.  
  42. // However, the following examples will work:
  43. ($some_var) ? print 'true' : print 'false'; // print is also a construct, but
  44.                                             // it behaves like a function, so
  45.                                             // it may be used in this context.
  46. echo $some_var ? 'true': 'false'; // changing the statement around

echo() also has a shortcut syntax, where you can
immediately follow the opening tag with an equals sign. This short syntax
only works with the short_open_tag configuration setting
enabled.


I have <?=$foo[/php] foo.


For a short discussion about the differences between
print() and echo(), see this FAQTs
Knowledge Base Article: http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40

Note: Because this is a
language construct and not a function, it cannot be called using
variable functions

See also
print(),
printf(), and
flush().

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank

Home | PHP Functions | PHP echo function