PHP echo and print Statements
The PHP echo and print statement display the output of the variable’s values to the browser. The echo statement is a language construct and does not return a value. Therefore, using it as a function is unnecessary, and no parentheses are needed. You can use echo to print multiple string values separated by a comma.
Displaying Simple String
A simple string value can be displayed using the echo statement. The string is passed as an argument within double quotes or single quotes.
The following example shows the text Hello World.
Output:
Displaying Multiple Strings
Multiple strings are printed by passing them as arguments separated by comma(,). Suppose you want to print the Hello and World texts, use the following echo statement.
Output:
Displaying HTML Code
HTML code is displayed in the same manner as we display string. You can include HTML code within the string. Consider the following example to show HTML Code.
Heading Text.’;
echo ‘Ths is parapraph text.
’;
?>
Output:
Heading Text.
Ths is parapraph text.
Displaying variables
Consider the following example to print the value of variables. The tag is appended to the variables using the concatenation operator to insert a new line.
";
echo $var2 . "
";
echo $var3[0] . "
";
?>
Output:
Print Statement
The print statement is similar to the echo statement with a few differences. It takes only a single argument and returns a value of 1. Most developers prefer an echo statement as it is slightly faster than a print statement.
To display a simple string.
Output:
To display the HTML Code.
Heading Text.’;
print ‘Ths is parapraph text.
’;
?>
Output:
Heading Text.
Ths is paragraph text.
To display variables.
";
print $var2 . "
";
print $var3[0] . "
";
?>
Output: