W e b t u t o r i a l s

Define PHP

PHP Installation


PHP Case Sensitivity

PHP Variable

PHP Echo & Print

PHP Operators

PHP Receiving Input

PHP Decision Making

PHP Loops

PHP Jumping Statement

PHP Image Gallery

PHP Arrays

PHP Date Functions

PHP String Functions

PHP Math Functions

PHP Functions

PHP Variable Scope

PHP Constant Variable

PHP Superglobals

PHP Form Validation

PHP Include Statement

PHP Filter

PHP File Handling

PHP File Upload

PHP Cookies

PHP Session

PHP Send Emails

PHP Captcha

PHP-MySQL Connectivity

PHP MySQL Select

PHP MySQL Insert

PHP MySQL Delete

PHP MySQL Update

Echo and Print Statements

In PHP, there are two basic ways to display the output: echo and print.

The PHP echo Statement

The echo statement can output one or more strings and can display anything such as string, numbers, variables values, the results 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().

Display Strings

echo "Hello World!";
echo 'Hello World!';

Display HTML Code

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

Display Variables

$text = "Hello World!";
echo $text;

echo "Value = $a"; //Output - Value = 10
echo 'Value= $a'; //Output - Value= $a

The PHP print Statement

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

Display Strings of Text

print "Hello World!";
print 'Hello World!';

Display HTML Code

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

Display Variables

$text = "Hello World!";
print $text;

print "Value = $a"; //Output - Value = 10
print 'Value= $a'; //Output - Value= $a

Difference Between Echo and Print Statement.

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

Sno Echo Print
1. Echo does not return any value.
$r = echo $name; //Error
Print has a return of 1. So it can be used in expressions.
2. Echo is slightly faster than print. Print is slower than Echo
3. 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

Embedding HTML inside PHP

 $date = 26;
 $month = "January";
 echo "Republic Day is on  ".$date." ".$month."";

Embedding PHP Inside HTML

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