What is the difference between echo and print

What is the difference between echo and print

What is the difference between echo and print

With PHP, there are two basic ways to get the output: echo and print.

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

 

PHP echo and print Statements

echo and print is more or less the same. They are both used to output data to the screen.

The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.

You may like

Difference between echo and print

echo

  • echo is a statement, which is used to display the output.
  • echo can be used with or without parentheses.
  • echo does not return any value.
  • We can pass multiple strings separated by comma (,) in echo.
  • echo is faster than a print statement.

 

print

  • print is also a statement, used as an alternative to echo at many times to display the output.
  • print can be used with or without parentheses.
  • print always returns an integer value, which is 1.
  • Using print, we cannot pass multiple arguments.
  • print is slower than the echo statement.

 

The PHP echo Statement

The echo statement can output one or more strings. In general terms, the echo statement can display anything that can be displayed to the browser, such as string, numbers, variables values, the results of expressions etc.

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

 

Display Text

Example

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 HTML Code

The following example will show you how to display HTML code using the echo statement:

Example

// Displaying HTML code

echo "<h4>This is a simple heading.</h4>";

echo "<h4 style='color: red;'>This is heading with style.</h4>";

Output

This is a simple heading.

This is heading with style.

 

Display Variables

The following example will show you how to display variable using the echo statement:

// Defining variables

$txt = "Hello World!";

$num = 123456789;

$colors = array("Red", "Green", "Blue");

// Displaying variables

echo $txt;

echo "<br>";

echo $num;

echo "<br>";

echo $colors[0];

 

Output

Hello World!

123456789

Red

 

The PHP print Statement

The print the statement can be used with or without parentheses: print or print().

You can also use the 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().

Both print or print(). the print statement works exactly the same way except that the print statement can only output one string, and always returns 1. That’s why the echo statement is considered marginally faster than the print statement since it doesn’t return any value.

Display Text

The following example shows how to output text with the print command (notice that the text can contain HTML markup):

Example

print "<h2>PHP is Fun!</h2>";

print "Hello world!<br>";

print "I'm about to learn PHP!";

 

Output

PHP is Fun!

Hello, world!

I'm about to learn PHP!

Display Strings of Text

The following example will show you how to display a string of text with the print statement:

Example

// Displaying string of text

print "Hello World!";

Output

Hello World!

 

Display Variables

The following example will show you how to display variable using the print statement:

Example

// Defining variables

$txt = "Hello World!";

$num = 123456789;

$colors = array("Red", "Green", "Blue");

// Displaying variables

print $txt;

print "<br>";

print $num;

print "<br>";

print $colors[0];

 

Output

Hello World!

123456789

You may like

2 thoughts on “What is the difference between echo and print”

Leave a Comment

Your email address will not be published. Required fields are marked *