What is the difference between echo and print in php

difference between echo and print in php

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

We frequently use the echo statement to display the output. There are two basic ways to get the output in PHP:

  • echo
  • print

echo and print are language constructs, and they never behave like a function. Therefore, there is no requirement for parentheses. However, both statements can be used with or without parentheses. We can use these statements to output variables or strings

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.

 

You can see the difference between echo and print statements with the help of the following programs.

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.

Example (echo)

echo “Multiple “,”argument “,”string!”;

Output:

Multiple argument string!

Displaying Variables: Displaying variables with echo statements is also as easy as displaying normal strings. The below example shows different ways to display variables with the help of a PHP echo statement.

 

//defining the variables

$text = “Hello, World!”;

$num1 = 10;

$num2 = 20;

//echoing the variables

echo $text.”n”;

echo $num1.”+”.$num2.”=”;

echo $num1 + $num2;

 

Hello, World!

10+20=30

 

PHP print statement:

The PHP print statement is similar to the echo statement and can be used alternative to echo many times. It is also a language-construct, and so we may not use parenthesis i.e print or print().

print “Hello, world!”;

Output:

Hello, world!

Displaying Variables: Displaying variables with print statements is also the same as that of echo statements. The example below shows how to display variables with the help of a PHP print statement.

 

//defining the variables

$text = “Hello, World!”;

$num1 = 10;

$num2 = 20;

//echoing the variables

print $text.”n”;

print $num1.”+”.$num2.”=”;

print $num1 + $num2;

Output:

Hello, World!

10+20=30

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..

 

Related searches

Scroll to Top