Home / Core PHP / PHP 5 echo and print Statements

PHP 5 echo and print Statements

In PHP there are two basic ways to get output: echo and print.

In this tutorial we use echo (and print) in almost every example. So, this chapter contains a little more info about those two output statements.

PHP echo and print Statements
There are some differences between echo and print:

echo – can output one or more strings
print – can only output one string, and returns always 1
Tip: echo is marginally faster compared to print as echo does not return any value.

The PHP echo Statement
echo is a language construct, and can be used with or without parentheses: echo or echo().

Display Strings

The following example shows how to display different strings with the echo command (also notice that the strings can contain HTML markup):

Example

Select Code
<?php
echo "<h2>PHP is fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This", " string", " was", " made", " with multiple parameters.";
?>


Display Variables

The following example shows how to display strings and variables with the echo command:

Example

Select Code
<?php
$txt1="Learn PHP";
$txt2="vshakya.in";
$cars=array("Volvo","BMW","Toyota");

echo $txt1;
echo "<br>";
echo "Study PHP at $txt2";
echo "My car is a {$cars[0]}";
?>

The PHP print Statement
print is also a language construct, and can be used with or without parentheses: print or print().

Display Strings

The following example shows how to display different strings with the print command (also notice that the strings can contain HTML markup):

Example

Select Code
<?php
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>


Display Variables

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

Example

Select Code
<?php
$txt1="Learn PHP";
$txt2="vshakya.in";
$cars=array("Volvo","BMW","Toyota");

print $txt1;
print "<br>";
print "Study PHP at $txt2";
print "My car is a {$cars[0]}";
?>

About v.shakya

I am V.Shakya, Freelance PHP Programmer. I like to share my ideas, views and knowledge to all of you who come across my website. I am young, enthusiastic, highly motivated and self disciplined person. I completed my studies in Master of Computer Application and currently giving my technical expertise to one of the Big IT company. I have more than Ten years of experience in vast field of Programming , Designing and Development of websites and various software's.

Check Also

PHP 5 Arrays

An array stores multiple values in one single variable: Example <?php $cars=array("Volvo","BMW","Toyota"); echo "I like …

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.