What is the difference between echo and print in PHP?
June 18, 2016
I am learning PHP at the moment and I have a question about printing outputs using PHP. I know the both “echo” and “print” displays an output.
For example:
gives the same output.
My question is what is the difference between the two? When do you use echo and print?
My other question is how do I add a space between different text outputs? For example, the above code when put next to each other outputs “Hello WorldHello World”. How can I add a space between World and Hello?
Thanks you very much!
MasterA
For example:
echo “Hello World”;
print “Hello World”;
gives the same output.
My question is what is the difference between the two? When do you use echo and print?
My other question is how do I add a space between different text outputs? For example, the above code when put next to each other outputs “Hello WorldHello World”. How can I add a space between World and Hello?
Thanks you very much!
MasterA
Comments 2
June 25, 2016
Not much, they are essentially the same thing. Here are the key (minor) differences:
- Print returns a value. The return value is always "1". This means it can be used in expressions. Example:
$myReturn = print "hello";
$myReturn will equal 1.
- Echo is faster. Not much, but it is since it doesn't return a value.
- Echo can take more than one parameter.
- Echo supports short syntax:
<?=$somevariable?>
September 27, 2017
According to me, it is the same but echo is more popular and almost of time I used this commend in PHP file. I think they are only having a different thing is echo has no return value while print has a return value of 1.According to me, it is the same but echo is more popular and almost of time I used this commend in PHP file. I think they are only having a different thing is echo has no return value while print has a return value of 1.
Please login or sign up to leave a commentJoin

