PHP Echo / Print

This tutorial helps you learn how to use a PHP Echo/Print. So, let's begin.

Both PHP echo and print are PHP statements. Therefore, you can use them to display the output in PHP.

 

What is echo?

  • The echo is a statement used for displaying the output. You can use it with parentheses echo or without parentheses echo.
  • Multiple strings separated as ( , ) can be passed by an echo.
  • No value is returned by the echo.
  • echo is faster than print

Example:

<?php
$name="Alex";
echo $name;
//or        
echo ($name);
?>

output: 

Alex Alex

Create and initialize a variable($name) holding a string value=" Alex" in the example above. We want to print the name for this ($name) variable declare inside the echo with or without parentheses. It will display the same output.

 

<?php
$name = "Alex";
$profile = "Tutorial With Example";
$age = 26;
echo $name , $profile , $age, " years old";
?>

Output:

Alex Tutorial With Example 26 years old

 

What is Print?

  • Print is also a statement used for displaying the output. You can use it with parentheses ( ) or without parentheses.
  • Using print, you cannot pass multiple arguments
  • print always returns an integer value, i.e., 1
  • it is slower than echo
<?php
$name="Alex";
print $name;
//or
print ($name);
?>

In the example above Declare a variable ($name) value="Alex". Next, we want to print the name. For this, we will simply define the $name inside the print statement with or without parentheses. It will display the output: "Alex".

 

Print does not output more than one.

Example

<?php
$name = "Alex";
$profile = "PHP Developer";
$age = 25;
print $name , $profile , $age, " years old"; 
?>

Output:

Parse error: syntax error