What are the differences between print and echo in php select all that apply?


With PHP, there are two basic ways to get 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 are 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.


The PHP echo Statement

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

Display Text

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

Example

echo "

PHP is Fun!

";
echo "Hello world!
";
echo "I'm about to learn PHP!
";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>

Try it Yourself »

Display Variables

The following example shows how to output text and variables with the echo statement:

Example

$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

echo "

" . $txt1 . "

";
echo "Study PHP at " . $txt2 . "
";
echo $x + $y;
?>

Try it Yourself »



The PHP print Statement

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

Display Text

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

Example

print "

PHP is Fun!

";
print "Hello world!
";
print "I'm about to learn PHP!";
?>

Try it Yourself »

Display Variables

The following example shows how to output text and variables with the print statement:

Example

$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

print "

" . $txt1 . "

";
print "Study PHP at " . $txt2 . "
";
print $x + $y;
?>

Try it Yourself »



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 the statements can be used with or without parentheses. We can use these statements to output variables or strings.

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

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

For Example (Check multiple arguments)

You can pass multiple arguments separated by a comma (,) in echo. It will not generate any syntax error.

Output:

What are the differences between print and echo in php select all that apply?

It will generate a syntax error because of multiple arguments in a print statement.

Output:

What are the differences between print and echo in php select all that apply?

For Example (Check Return Value)

echo statement does not return any value. It will generate an error if you try to display its return value.

Output:

What are the differences between print and echo in php select all that apply?

As we already discussed that print returns a value, which is always 1.

Output:

What are the differences between print and echo in php select all that apply?

What is the difference between echo and print PHP?

echo and print are 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.

What is the difference between print and printf in PHP?

They are all used to print text on the screen but printf function is way more complicated. I believe print is simply for text, printf for Formatting text with some HTML markup (F = Formatting), and echo can be for anything.

What is the meaning of echo in PHP?

PHP echo statement can be used to print the string, multi-line strings, escaping characters, variable, array, etc. Some important points that you must know about the echo statement are: echo is a statement, which is used to display the output.

What is the use of print in PHP?

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.