Google News
logo
PHP Echo and Print Statements
In PHP there are two basic ways to get outputs are echo and print.
The PHP echo Statement

The PHP echo statement is a language construct not actually a function, so you can use it without parentheses e.g. echo or echo(). But if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses.

Display Text
The following example shows how to output text with the echo command (That the text can contain HTML)
<!DOCTYPE html>
<html>
<head>
	<title>PHP echo Statement</title>
</head>

<body>
	<?php
		echo "<h1>Free Time Learn</h1>";
		echo "<h2>www.freetimelearning.com</h2>";
		echo "<p>www.freetimelearn.com</p>";
		echo "Free Time Learning!<br /><br />";
		echo "This is ", "Sample ", "string.";
	?> 
</body>
</html>​
Output :
Display Variables

The following example shows how to output text and variables with the echo statement :

<!DOCTYPE html>
<html>
<head>
	<title>PHP echo Statement</title>
</head>

<body>
	<?php
		$var_name = "FTL (Free Time Learning)";
		$var_website = "www.freetimelearning.com";
		$x = 9;
		$y = 18;
		$z = 27;
		
		echo "<h2>" . $var_name . "</h2>";
		echo "Learn PHP at " . $var_website . "<br /><br />";
		echo $x + $y. "<br /><br />";
		echo $y * $z;
	?>
    
</body>
</html>
Output :
The PHP print Statement

The PHP print statement (an alternative to echo) to display output to the browser. Like echo the print is also a language construct not a real function. So you can also use it without parentheses like: print or print().

Display Text
The following example shows how to output text with the print command (That the text can contain HTML).
<!DOCTYPE html>
<html>
<head>
	<title>PHP print Statement</title>
</head>

<body>
	<?php
		print "<h1>Free Time Learn</h1>";
		print "<h2>www.freetimelearning.com</h2>";
		print "<p>www.freetimelearn.com</p>";
		print "Free Time Learning!";
	?> 
</body>
</html>
Output :
Display Variables
The following example shows how to output text and variables with the print statement :
<!DOCTYPE html>
<html>
<head>
	<title>PHP print Statement</title>
</head>

<body>
	<?php
		print "<h1>Free Time Learn</h1>";
		print "<h2>www.freetimelearning.com</h2>";
		print "<p>www.freetimelearn.com</p>";
		print "Free Time Learning!";
	?> 
</body>
</html>
Output :