PHP echo() Function

Usage — The PHP echo() function is used to output one or more strings

It has the following syntax:


void echo ( string $arg1 [, string $... ] )

Here is an example of using echo():


// Output — Hello Readers!
echo "Hello Readers!";

Return Value — This function returns nothing. It just outputs all parameters and no additional newline is appended to the output.

Additional Information — Actually, echo() is not really a function (it is a language construct). This means that you are not required to use parentheses with it. Also, 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. It is possible to escape characters or use variables and arrays inside an echo() statement as shown in the examples.

The major differences between print() and echo() is that echo() accepts an argument list and doesn’t have a return value.

PHP Version and Changelog — The echo() function is available in PHP 4, PHP 5, PHP 7.

Relevant Functions — Other related PHP functions that you should know about are: print() which outputs a string, printf() which outputs a formatted string and flush which flushes system output buffer.

Go Directly To — usage, parameters, working examples or additional tips.



The arg1 parameter is used to specify the parameter to output.

Working Examples

Here are some examples of using the echo() function:


// Output — Hello Reader!
echo "Hello Reader!\n";

/* Output —
The output can also span
    multiple lines. The newlines will be
    output as well.
echo "The output can also span
      multiple lines. The newlines will be
      output as well.\n";

// Output — You can also escape characters "Like this".
echo "You can also escape characters \"Like this\".";

// You can use variables inside of an echo statement.
$position = 123;

// Output — You need to go to seat number 123.
echo "You need to go to seat number $position";

// You can also use arrays.
$planets = array("largest" => "Jupiter");

// Output — Jupiter is the largest planet in Solar System!
echo "{$planets['largest']} is the largest planet in Solar System!";

// Using single quotes will print the variable name, not the value.
// Output — You need to go to seat number $position
echo 'You need to go to seat number $position';

// If you are not using any other characters, you can just echo variables
// Output — 123
echo $position;

// Output — Jupiter
echo $planets['largest'];

// You can either pass strings individually as multiple arguments or
// concatenated together as a single argument.
// Output — This string was made with multiple parameters.
echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);

// Output — This string was made with concatenation.
echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";

// Because echo does not behave like a function, the following code is invalid.
($something) ? echo 'true' : echo 'false';

// Returns Syntax Error
// "Parse error: syntax error, unexpected 'echo'"
// changing the statement around
echo $something ? 'true': 'false';

Additional Tips

Here are some of the most upvoted tips taken from the comment section of the PHP manual:

  1. Passing multiple parameters to echo() using commas (‘,’) is not exactly identical to using the concatenation operator (‘.’). There are two notable differences.

    First, concatenation operators have much higher precedence. There are many operators with lower precedence than concatenation, so it is a good idea to use the multi-argument form instead of passing concatenated strings.


    // Output — "2". Parentheses needed.
    echo "The sum is " . 1 | 2;
    // Output — "The sum is 3". Fine.
    echo "The sum is ", 1 | 2;

    Second, a slightly confusing phenomenon is that unlike passing arguments to functions, the values are evaluated one by one.


    function f($arg){
        return $arg;
    echo "Foo" . f("bar") . "Foo";
    echo "\n\n";
    echo "Foo", f("bar"), "Foo";
    The output would be:
    string(3) "bar"FoobarFoo
    Foostring(3) "bar"

    Suggested by - pemapmodder1970 at gmail dot com

  2. The {} syntax is useful for printing non array variables as well, an example to illustrate:


    $foo = "foobar";
    $bar = "barbaz";
    //Will produce the error: Undefined variable: $foo_
    echo "$foo_$bar";
    //Will print the intended string: "foobar_barbaz"
    echo "{$foo}_$bar";

    Could even be worth getting into the habit of enclosing all variables in {} when writing echo() strings, to be on the safe side.

    Suggested by - Jamie Robinson

Further Reading

  1. You can read more about the PHP echo() function on

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

Go To Top