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

print

Description

The print of String for PHP outputs a string.

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

Syntax

print(
    string $expression
): int

Parameters

expression

The expression to be output. Non-string values will be coerced to strings, even when the strict_types directive is enabled.

Return

Returns 1.

Examples

1 · with parentheses

<?

$expression = "with ()";

print($expression);
with ()

2 · without parentheses

<?

$expression = "without ()";

print $expression;
without ()

3 · multiple lines

<?

$expression = "multiple
lines";

print $expression;
multiple
lines

4 · newline character

<?

$expression = "newline\ncharacter";

print $expression;
newline
character

5 · escape character

<?

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

print $expression;
escape characters "like this"

6 · double quotes

<?

$var = "variable";

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

print $expression;
double quotes expand variables: variable

7 · single quotes

<?

$var = "variable";

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

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

8 · curly braces

<?

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

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

print $expression;
array: value

9 · period

<?

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

print $expression1. $expression2. $expression3. PHP_EOL.
PHP_EOL;

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

    return $myparameter;
}

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

functionfunctionfunction123

10 · heredoc

<?

$var = "variable";

print <<<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.

11 · nowdoc

<?

$var = "variable";

print <<<'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.

12 · validity

<?

$expression = "expression";

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

// valid
print $expression ? "true" : "false";
truetrue

13 · return

<?

$expression = "";

$return = print $expression;

print $return;
1