Statements: echo and print


echo and print both are used to output data to the screen.

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, while print can take one argument. echo is faster than print.

Echo Statement:

  • echo is a statement i.e used to display the output. it can be used with parentheses echo( ) or without parentheses echo.
  • Using echo can pass multiple string seperated as ( , )
  • echo doesn’t return any value.

Example

<?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.";

?>

We can also output the variables with echo statement.

Example

<?php

$txt1 = "Learn PHP";

$txt2 = "W3Schools.com";

$x = 5;

$y = 4;

echo ($txt1);

echo "<h2>$txt1</h2>";

echo "Study PHP at $txt2<br>";

echo $x + $y;

?>

Print Statement

  • print is also a statement i.e used to display the output. it can be used with parentheses print( ) or without parentheses print.
  • it is slower than echo
  • using print can doesn’t pass multiple argument
  • print always return 1

Example

<?php

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

print "Hello world!<br>";

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

?>

We can also use variable with the print.

Example:

<?php

$txt1 = "Learn PHP";

$txt2 = "W3Schools.com";

$x = 5;

$y = 4;

print "<h2>$txt1</h2>";

print "Study PHP at $txt2<br>";

print $x + $y;

?>