HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

echo

Description

The echo of String for PHP outputs one or more strings.

echo is a language construct, not a function, so parentheses are not required.

Syntax

echo(
    string ...$expressions
): void

Parameters

expressions

One or more string expressions to output, separated by commas. Non-string values will be coerced to strings, even when the strict_types directive is enabled.

Return

No value is returned.

Examples

1 · with parentheses

<?

$expression = "with ()";

echo($expression);
with ()

2 · without parentheses

<?

$expression = "without ()";

echo $expression;
without ()

3 · multiple lines

<?

$expression = "multiple
lines";

echo $expression;
multiple
lines

4 · newline character

<?

$expression = "newline\ncharacter";

echo $expression;
newline
character

5 · escape character

<?

$expression = "escape characters \"like this\"";

echo $expression;
escape characters "like this"

6 · double quotes

<?

$var = "variable";

$expression = "double quotes expand variables: $var";

echo $expression;
double quotes expand variables: variable

7 · single quotes

<?

$var = "variable";

$expression = 'single quotes do not expand variables: $var';

echo $expression;
single quotes do not expand variables: $var

8 · curly braces

<?

$array =
[
    "key" => "value"
];

$expression = "array: {$array['key']}";

echo $expression;
array: value

9 · comma

<?

$expression1 = "comma, ";
$expression2 = "passes, ";
$expression3 = "separately";

echo $expression1, $expression2, $expression3, PHP_EOL,
PHP_EOL;

function myfunction($myparameter)
{
    echo "function";

    return $myparameter;
}

echo myfunction(1), myfunction(2), myfunction(3);
comma, passes, separately

function1function2function3

10 · period

<?

$expression1 = "period. ";
$expression2 = "passes. ";
$expression3 = "together";

echo $expression1. $expression2. $expression3. PHP_EOL.
PHP_EOL;

function myfunction($myparameter)
{
    echo "function";

    return $myparameter;
}

echo myfunction(1). myfunction(2). myfunction(3);
period. passes. together

functionfunctionfunction123

11 · compare · comma

<?

$expression1 = str_repeat("a", 100);
$expression2 = str_repeat("b", 100);
$expression3 = str_repeat("c", 100);

$time_start = microtime(true);

for($i = 0; $i < 1000; ++$i)
{
    ob_start();

    for($j = 0; $j < 1000; ++$j)
    {
        echo $expression1, $expression2, $expression3;
    }

    ob_clean();
}

$time_end = microtime(true);

$time = $time_end - $time_start;

echo $time;
0.39258909225464

12 · compare · period

<?

$expression1 = str_repeat("a", 100);
$expression2 = str_repeat("b", 100);
$expression3 = str_repeat("c", 100);

$time_start = microtime(true);

for($i = 0; $i < 1000; ++$i)
{
    ob_start();

    for($j = 0; $j < 1000; ++$j)
    {
        echo $expression1. $expression2. $expression3;
    }

    ob_clean();
}

$time_end = microtime(true);

$time = $time_end - $time_start;

echo $time;
0.43325805664062

13 · heredoc

<?

$var = "variable";

echo <<<MYIDENTIFIER
        The heredoc allows output on multiple lines.
        Syntax begins with <<< and an identifier.
        Variables are expanded: $var.
        Syntax ends with an identifier whose placement determines the indentation.
    MYIDENTIFIER;
    The heredoc allows output on multiple lines.
    Syntax begins with <<< and an identifier.
    Variables are expanded: variable.
    Syntax ends with an identifier whose placement determines the indentation.

14 · nowdoc

<?

$var = "variable";

echo <<<'MYIDENTIFIER'
        The nowdoc allows output on multiple lines.
        Syntax begins with <<< and an identifier in single quotes.
        Variables are not expanded: $var.
        Syntax ends with an identifier whose placement determines the indentation.
    MYIDENTIFIER;
    The nowdoc allows output on multiple lines.
    Syntax begins with <<< and an identifier in single quotes.
    Variables are not expanded: $var.
    Syntax ends with an identifier whose placement determines the indentation.

15 · validity

<?

$expression = "expression";

// not valid
//($expression) ? echo "true" : echo "false";

// valid
echo $expression ? "true" : "false";
true