Basic knowledge

Echo - output to source code

16. 02. 2020

Obsah článku

The `echo` construct is used to dump a variable or string into the source code.

Support: All versions
Brief description: Output one or more strings
Type: command, construct (not a function)


echo 'hello world';

Print "hello world".

$var = 'text';
echo $var;

Print the value of the variable $var, i.e. "Text".

Echo is not a function (it's a command), so you may or may not use the parenthesis. Thus, writing echo ('hello world'); is also correct.

Extra note: PHP treats Echo as a command (a construct) and thus treats it as an expression. The parenthesis is optional in this case. If we give the notation: echo ('something');, the Echo statement does not become a function and is not treated as such. The parenthesis in this case means enclosing the exact value of the expression, similar to how it works in mathematics.

Quotation marks

Strings can be enclosed in quotation marks and apostrophes.

So this:

echo "hello";

It's the same as this:

echo 'hello';

But beware that each string must start and end with the same type of quote character and this quote character must not be used in the string.

For example, if you want to output an HTML link (or any HTML code), you must precede the quotation mark with a slash. A slash means "exactly this character", so it is not understood as an expression in the language.

echo "<a href="index.php">link text</a>";

Technical note: Quotation marks have a special meaning in PHP.


  • arg Output parameter.

Return values

No value is returned.

Cannot be used as a variable.


Note: Because this is a language construct (construct = command) (not a function), it cannot be loaded into a variable.


echo "hello world";
echo "echo can output multiple lines of text.
But beware of the HTML tag <br>, it will not be printed. That's what the nl2br() function is for.";
$a = "php"; // variable definition
echo "I like " . $a; // Prints: I like php

Echo also has a shortened syntax, where you can use only the equals sign after the opening php tag.

Hi <?=$name;?>!
This is useful if we need to dump some quick information into the page. For example, the current year:
By Jan Barášek © <?=date('Y');?>
> This shortened syntax will only work if shortened opening php tags are enabled, i.e. the `short_open_tag` directive is set to `on`.
All common mathematical operations can be performed within the **echo** command.
For a detailed discussion of mathematics, see <a href="/mathematics">a separate article</a>.
echo 5 + 3 * 2; // prints 11

Jan Barášek   Více o autorovi

Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.

Rád vám pomůžu:

Související články