PHP echo statement

If you want to display text, message, or any data/information than PHP provide the two basic statements: echo and print. PHP echo statement can display any type of data such as string, number, variable value, the result of expressions etc. Since echo is a language construct not a real function, you can use it with or without parentheses e.g. echo or echo().

Example 1: Display String message using echo.

echo ("Hello World!");
echo "Hello World!";
echo 'Hello World!';
Hello World!
Hello World!
Hello World!

All the above echo statements are same and produce same output.

Example 2: Display HTML Code with echo statement.

echo "<h2>This is a simple heading.</h2>";
echo "<h2 style='color: red;'>This is heading with style.</h2>"
echo "<h2 class='red'>This is heading with style.</h2>"
echo "<input type='text' name='name'>";
echo "<input type=\"text\" name=\"name\" value=\"Name\">";
echo "<script>alert('Hi! I am Alert Box.');</script>";
echo '<script>alert("Hi! I am Alert Box.");</script>';

This is a simple heading.

This is heading with style.

This is heading with style.


Example 3: Display PHP Variable value using echo.

$txt = "I am PHP String";
echo $txt;

$a=10;
echo "Value = $a";
echo 'Value = $a';
echo "Value = ".$a;
I am PHP String
Value = 10
Value = $a
Value = 10

To display variable value, write variable inside double quotes or outside quotes, single quotes will treat variable as plain text and display variable as it is instead of its value.

PHP Print statement

You can also use PHP print statement to display output. Like echo, the print is also a language construct not a function. So you can use it with or without parentheses e.g. print or print().

Example 4: Display String message using print.

print ("Hello World!");
print "Hello World!";
print 'Hello World!';
Hello World!
Hello World!
Hello World!

Example 5: Display HTML Code with print statement.

print "<h2>This is a simple heading.</h2>";
print '<h2 style="color: red;">This is heading with style.</h2>';
print '<h2 class='red'>This is heading with style.</h2>';
print "<input type='text' name='name'>";
print "<input type=\"text\" name=\"name\" value=\"Name\">";
print "<script>alert('Hi! I am Alert Box.');</script>";
print '<script>alert("Hi! I am Alert Box.");</script>';

This is a simple heading.

This is heading with style.

This is heading with style.


Example 6: Display PHP Variable value using print.

$txt = "I am PHP String";
print $txt;

$a=10;
print "Value = $a";
print 'Value = $a';
print 'Value = '.$a;
I am PHP String
Value = 10
Value = $a
Value = 10

Echo and Print Statement

Both echo and print statement works exactly the same way but they have few differences.

Difference Between Echo and Print Statement
Sno Echo Print
Echo does not return any value.
$r = echo $name; //Error
Print has a return of 1. So it can be used in expressions.
Echo is slightly faster than print. Print is slower than Echo
Echo can take multiple parameters.
Echo "This ", "string ", "was ", "made ", "with multiple parameters ", "separated with comma (\,)" ;
Print can take only one argument. Print statement can not used with multiple parameters.
print "Hello ", "World"; //Error

Example 7: Embedding HTML inside PHP

<?php
   $date = 26;
   $month = "January";
   echo "Republic Day is on <b>".$date." ".$month."</b>";
?>
Republic Day is on 26 January

Example 8: Embedding PHP Inside HTML

<?php
    $text= "Enter username";
?>
<input type="text" value="<?php echo $text; ?>">