Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

Difference between echo and print in php

Author: Neha Admec
by Neha Admec
Posted: Jan 09, 2022

Echo and print is used in php to display data, we can use echo in php with or without the parenthesis, and print can also be used same as the echo and both are used with the same purpose but the only difference between them is that the print statement will return the value with true or false, if it was printed successfully will return true otherwise return false.

Learn all these concepts in detail with PHP training institute in Delhi.

Lets see the difference between echo and print:

echo

  • echo is faster than print.
  • echo will not return any value.
  • echo can take multiple arguments.

print

  • print always return 1(integer), but echo does not return any value.
  • In case of echo we can pass multiple arguments, but in case of print we can not pass any argument in it.
  • print behaves like a function.

Attain more important skills in website development with web development courses in Delhi.

Syntax difference

echo :

echo "Hello world!";

?>

In this example, here is start and end php tag. Inside this tag there is a echo statement. Through that echo statement we can display "Hello world!" on the screen

But there are some important points we need to undertsand:

echo is basically not a function, thats why you are not required to use parentheses in it. But,

In case of more than one arguments you can use parenthese with this, otherwise parentheses can create a parse error in your php code.

print :

print "Hello world!";

?>

Same as the echo statement it also has a PHP tag and a print function. By using this tag we can also print the same output as echo funtion i.e. "Hello world!".

Some points you need to remember :

Print is not actually a function, So, same as the echo function you dont need to use parentheses.

As mentioned above, it will always return a value that is 1.

But print do behave like a function.

Let’s see an example of echo and print here:

Echo:

//defining the variables

$text = "Hello, World!";

$num1 = 10;

$num2 = 20;

//echoing the variables

echo $text."\n";

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

echo $num1 + $num2;

?>

Output:

Hello, World!

10+20=30

Print:

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

We can say that using echo in place print is more convenient and faster than print function.

About the Author

Hey, I am Neha and I am pursuing web design courses in Delhi at ADMEC.

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Neha Admec

Neha Admec

Member since: Jan 06, 2022
Published articles: 2

Related Articles