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.
Listed below are a few features of the echo function in PHP:
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 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
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 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
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 also uses the print statement to display output. Below are listed a few traits:
print()
. The PHP print statement uses parenthesis exclusively, like print.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
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
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
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 | |
---|---|
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!