WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 echo and print Statements


There are two basic output methods in PHP: echo and print.

In this chapter we will discuss the usage of the two statements in detail, and demonstrate how to use echo and print in examples.


PHP echo and print statements

The difference between echo and print:

  • echo-can output one or more strings
  • print-only one string is allowed, the return value is always 1

Hint: echo is faster than print, echo has no return value, and print has a return value of 1.


PHP echo statement

echo is a language structure that can be used without parentheses or parentheses: echo or echo ().

Show string

The following example shows how to use the echo command to output a string (the string can contain HTML tags):

Example

<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm going to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with various parameters.";
?>

Display variables

The following example demonstrates how to use the echo command to output variables and strings:

Example

<?php
$txt1 = "Learn PHP";
$txt2 = "welookups.com";
$x = 3;
$y = 2;

echo "<h2>$txt1</h2>";
echo "Study PHP at $txt2<br>";
echo $x + $y;
?>


PHP print statement

print is also a language structure, with or without parentheses: print or print ().

Show string

The following example shows how to use the print command to output a string (the string can contain HTML tags):

Example

<?php
print "<h2>PHP is Easy!</h2>";
print "Hello world!<br>";
print "I'm going to learn PHP!";
?>

Display variables

The following example shows how to print variables and strings using the print command:

Example

<?php
$txt1 = "Learn PHP";
$txt2 = "welookups.com";
$x = 3;
$y = 2;

print "<h2>$txt1</h2>";
print "Study PHP at $txt2<br>";
print $x + $y;
?>