PHP echo() Function

What is PHP echo() Function?

To output one or more strings at a time in PHP, you can use echo. This is actually not a function but a PHP language construct like conditional statements (ex. if, else) or loops (ex. for, while). As it is not a function, you don’t need to use parentheses with this like echo() that you do in other typical functions. Though, it can also work with parentheses and still provides the same output.

Syntax:

echo (string1, string2, …)

Parameters:

string1, string2, …: The strings that you want to display. echo accepts one or more parameters. You must enclose the strings with double quotes or single quotes. Check Example 1

Though it allows multiple strings to display, you can concatenate multiple strings and variables to one string to display at a time using concatenate character(.). Check Example 2.

Any expression that produces a string like variable, array etc. can be used as an argument. Check Example 3.

Return Values:

It doesn’t return anything.  So, you can’t use this as an expression (because anything that has a value is expression). For example, to print a statement in ternary operator, you can’t use this otherwise you’ll receive an error. Check Example 4 (line 3). Though, after evaluating the expression, you can display the result by echo. Check Example 4 (line 5).

Examples:

Example 1: Displaying string using single or double quotes with echo

<?php
echo "This is string #1";   // Output: This is string #1
echo "<br />";
echo 'This is string #2';   // Output: This is string #2
echo "<br />";
echo ("This is string #3");   // Output: This is string #3
echo "<br />";
echo (10*2)/5;   // Output: 4
?>

Example 2: Concatenating multiple strings to display at a time

<?php
echo "Concatenating multiple strings, " . "you can display it by " . "print function";   // Output: Concatenating multiple strings, you can display it by print function
?>

Example 3: Displaying variables or array using echo

<?php
// Displaying variable
$str = "Hello printf";
echo $str;   // Output: Hello printf
echo "<br />";
// Displaying array
$arr = ["one", "two", "three"];
echo "First value is:" . $arr[0];   // Output: First value is:one
?>

Example 4: Using echo in expression

<?php
$Mark = 80;
( $Mark > 60 ) ? echo 'Passed' : echo 'Failed';   // Output: Parse error
echo "<br />";
echo ( $Mark > 60 ) ? 'Passed' : 'Failed';   // Output: Passed
?>

Notes on echo:

  • When you surround the argument with parentheses, it misleads as a function. But, it is not a part of the echo syntax, rather, it is a part of the output string. Check Example 1 (line 8).
  • When you try to display a variable within single quotes, it literally displays the variable itself, not the value of the variable. Check Example below (line 4). So, in this case use double quotes Check Example below (line 8).
  • You can add HTML tags. Check example below (line 4)-
  • You can use escape sequence. Check example below-
  • To display output, echo is faster than print.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP echo() function

PHP echo is a language construct to display strings which is listed under built-in string function. It is a very efficient way to print in PHP. it might be the most used method when comes to display data.

Reference:

https://www.php.net/manual/en/function.echo.php