To display the output in php, two statements are used-
- echo– It can have multiple parameters separated by comas. It doesn’t return any value.
- print– It can have only one parameter. It always returns .
- This is the more common statement than print is.
- It is faster than print as it doesn’t waste any time in returning something.
- echo can be used with parentheses or without parentheses.
- Following example shows all forms of echo-
echo “<h4>Displaying Strings</h4>”;
echo “<i>This is a string without parentheses.</i><br>”;
echo (“<i>This is a string with parentheses.</i><br>”);
echo “These “,”are “,”multiple “,”parameters”;
echo “<h4>Displaying Variables</h4>”;
echo “Variable without parentheses. $var<br>”;
echo (“Variable with parentheses.. “.$var.”<br>”);
echo $var,” “,$var;
In the example above-
- First echo shows the heading Displaying Strings. It also shows that we can use HTML tags in echo statement.
- Second echo is without parentheses and shows a string in <i> tag.
- Third echo is with parentheses and shows a string in <i> tag.
- Fourth echo has multiple parameters.
- Next echo gives break of two lines.
- After the break of two lines, first echo shows the heading Displaying Variables.
- Then a variable var is created with the value hello. Next echo displays a string and then the value of the variable. Before PHP5, the variables were used in echo statement out of the inverted comas and appended using dot (.). Two strings also can be appended using dot.
- Last echo has multiple parameters.
- It always returns 1.
- print cannot have multiple parameters.
- It is used same as echo.
- Following example shows the use of print statement:
print “<h2>Play with PHP!</h2>”;
print “It is PHP Tutorial!”;