PHP Echo and Print Statement

In this lesson, we’ll learn what echo and print articulations are in PHP with their execution through the illustrations. PHP utilizes two statements, echo and print, to show any text or output on the screen.


PHP Echo

Listed below are a few features of the echo function in PHP:

  • PHP’s echo statement is used with or without parentheses.
  • The PHP echo statement works as a language construct, not a function. So it does not have to be enclosed in parentheses. However, use a comma when more than one option is required.
  • If necessary, the PHP echo statement can pass several strings separated by commas or concatenated with another string. With parenthesis, only one parameter is enclosed.
  • The difference between the PHP echo and print statements is that echo does not return a value.
  • Additionally, the PHP echo statement is faster than print.
  • With the PHP echo statement, you can send out a multiline string, an array, a variable, escape characters, and more.

Single Line String Example

With the PHP echo statement, you can send out a multiline string, an array, a variable, escape characters, and more.

Canonical PHP tags Style Example

<?php
echo "Canonical PHP tags style";
?>

Output

Canonical PHP tags style

Multi-Line String Example

<!DOCTYPE html>
<html>
  <body>
    <?php
echo "PHP echo also used
to display multi-line string
result";
?>
  </body>
</html>

Output

PHP echo also used to display multiline string result

The PHP Echo (including parenthesis and excluding parenthesis)

The PHP engine will execute all statements in the example below, including and excluding arguments, except the final statement. After uncommenting the last line of code, PHP will produce a parse error: syntax error: “multi-value is not allowed with echo parenthesis.”

Example

<!DOCTYPE html>
<html>
  <body>
    <?PHP

$myString1 = "First, My PHP String " . "
        <br>";
$myString2 = "Second My PHP String " . "
            <br>";
$myString3 = "Third, My PHP String " . "
                <br>";
echo($myString1); // Statement will run successfully
echo $myString1; // Statement will run successfully
echo $myString1, $myString2, $myString3; // Statement will run successfully
//echo($myString1, $myString2, $myString3); // Statement will not execute. Parse error: syntax error, unexpected ',' on line
?>
  </body>
</html>

Output

First, My PHP String
First, My PHP String
First, My PHP String
Second My PHP String
Third, My PHP String

The PHP Escaping Characters

PHP only treats special characters within double quotation marks. In single quotes, PHP treats special characters as normal ones.

Example

<!DOCTYPE html>
<html>
  <body>
    <?php
echo "And that is how the Escape Character appears."."
        <br>";
echo "Example of Escape \"Sequence\" character";
?>
  </body>
</html>

Output

And that is how the Escape Character appears.
Example of Escape "Sequence" character

The PHP multiple parameters statements (including Comma and Concatenations)

The PHP echo statement has the advantage of utilizing commas with multiple arguments in terms of precedence. For example, when using many parameters in your echo statement, It does not require parentheses when concatenating. In concatenation, the priority of the period operator is higher than that of other operators. Therefore, using the parentheses in the statement is recommended when using concatenation for correct behavior.

In the below example, preceding two cases, PHP will execute the script successfully with commas and concatenation using parenthesis. Numerous arguments behave correctly with commas and concatenations without parenthesis. In concatenation, the period operator has greater priority than the addition and ternary operators; hence, parentheses are typically required to achieve the desired behavior.

In the third case, however, the PHP engine couldn’t figure out the math formula because the point was more important than the addition.

Example

<!DOCTYPE html>
<html>
  <body>
    <?php
//Concatenation requires parenthesis because the period operator takes precedence.:
$myString = 'PHP';

//For instance Comma
echo "For instance Comma ->", "
        <br>";
echo "Addition = ", 128 + 120, "
            <br>";
echo "Including Comma = ", isset( $myString ) ? $myString : "Excellent", "
                <br>";
echo "_____________________________________", "
                    <br>";
//For instance Concat
echo "For instance Concat ->", "
                        <br>";
echo 'Addition = ' . ( 128 + 120 ), "
                            <br>";
echo 'Concat including Parenthesis = ' . ( isset( $myString ) ? $myString : 'Excellent' ) . "
                                <br>";
echo "_____________________________________", "
                                    <br>";
//For instance Concat excluding parenthesis
echo "For instance, Concat No Parenthesis->", "
                                        <br>";
echo 'Addition = ' . 128 + 120, "
                                            <br>";
echo 'Concat excluding Parenthesis = ' . isset( $myString ) ? $myString : 'Bad' . "
                                                <br>";

?>
  </body>
</html>

Output

For instance Comma ->
Addition = 248
Including Comma = PHP
_____________________________________
For instance Concat ->
Addition = 248
Concat including Parenthesis = PHP
_____________________________________
For instance, Concat No Parenthesis ->
120
PHP

The PHP Print Special Character (Including Single and Double Quotes)

In the example below, the echo will display the variable name in a single quote, not the value, but double quotes will display the value.

Example

<!DOCTYPE html>
<html>
  <body> <?php
$myString = "Double Quote will display the string, not single quote ";
echo "$myString", "
    </br>"; //Double Quote will display the string
echo '$myString'; // Single Quote will not display the string

?> </body>undefined
</html>

Output

Double Quote will display the string, not single quote
$myString

PHP Print

PHP also uses the print statement to display output. Below are listed a few traits:

  • In PHP programming, the print statement uses parentheses inclusively, like print(). The PHP print statement uses parenthesis exclusively, like print.
  • You cannot put Multiple arguments into print.
  • Print always returns one and is deemed slower than echo as it does not return a value.

Print Statement Including and excluding parenthesis Example

<!DOCTYPE html>
<html>
  <body> <?php
$myString = "My First, String with print" . "
        <br>";
print $myString;
print( $myString );
?> </body>
</html>

Output

My First, String with print
My First, String with print

Another Example

<!DOCTYPE html>
<html>
  <body> <?php
$myString = "My First, String with print" . "
        <br>";
$myString2 = "My Second String with print" . "
            <br>";
print $myString,$myString2 ;
?> </body>
</html>

The code above will return an error.

Output

PHP Parse error: syntax error, unexpected ',' in /home/O1s6Xa/prog.PHP on line 8

It works when you use the print statement with multiple arguments concatenated.

Example

<!DOCTYPE html>
<html>
<body>
<?PHP
$myString = "My First, String with print" . "<br>";
$myString2 = "My Second String with print" . "<br>";
print $myString . $myString2;

?>
</body>
</html>

Output

First, String
Second String

The PHP Single and Double Quote print difference

In the below example, the print will display the variable name in a single quote, not the value, whereas double quotes will show the value. After uncommenting the second print statement, PHP will display the variable name in a single quote $myString.

Example

<!DOCTYPE html>
<html>
  <body>
    <?PHP
$myString= "Double Quote will display the string, not a single quote ";
print "$myString";//Double Quote will display the string
//print '$myString';// Single Quote will not display the string
?>
  </body>
</html>

Output

Double Quote will display the string, not a single quote

PHP variable and array values printing

Example

<!DOCTYPE html>
<html>
<body>
<?PHP
// Assigning the variable value
$myString = "PHP is a Programming Language";
$myString2 = "myString2";
print "myString is $myString" . "<br>";
// Use Array
$myString2 = array(
    "value" => "myString"
);
print "This is {$myString2['value']}" . "<br>";
// In PHP, print will display the variable name, not the value within single quote
print 'myString is $myString';
?>
</body>
</html>

Output

myString is PHP is a Programming Language
This is myString
myString is $myString

PHP Echo vs. Print

Print statements behave similarly to echo statements in terms of displaying variables. However, there are a few differences, and programmers choose per their demands.

Echo Print
The echo can pass many arguments. Print only accepts one argument at a time.
It either returns void or doesn’t return anything. It returns you the value 1.
It displays the outcomes of one or more strings separated by commas. The print command only displays the strings.
It is significantly faster than the printed statement. It takes longer to display results than using an echo statement.

This concludes the PHP Echo and Print Statement lesson. In the next lesson, we will discuss another topic in our PHP learning journey, PHP Variables!