Is there any difference between print and echo in PHP? Here is one quick article about Basic PHP and it is about to differentiate echo and print.The two most basic construct to display output are echo and print.It can confuse user because its a basic construct of PHP language and also functions in PHP. We can use both print and echo either with parentheses or without parentheses.
NOTE: To call the function, we always have to use the name of the function first followed by a parenthesized list of the arguments to the function.
What is Echo?
The simplest usage of the echo is to display a string as an argument.
Example
1 2 3 | echo "This will display with the use of PHP."; |
Or we can use:
1 2 3 | echo("This will display with the use of PHP."); |
Both of the above statements will display string without the quote signs.You can also pass multiple arguments without parenthesis of echo with comma separated values like
1 2 3 | echo "This will display ", "with the use of PHP."; |
But we can pass the multiple arguments in echo with the parenthesize. however, the echo will not accept multiple arguments:
1 2 3 |
What is print?
The print is mostly similar to echo with two important differences:
- print can accept only single argument.
- print returns a value which represents whether the print statement succeeded or not.
The value returned by print will be 1 if the print was successful and 0 if unsuccessful.
The print is mostly similar to echo with two important differences:
Mostly echo and print both are used with string arguments but we can use any type of argument and it will not cause any error.
Example
1 2 3 4 | print("3.14159"); // print a string print(3.14159); // print a number |
The second line will not convert argument into the string.If you use var_dump you will get a second line as a number, not a string. so we can use print and echo both to display numbers as well as string arguments.
In addition,generally we are using two printing functions print_r() and var_dump() for debugging.The main purpose of both the functions are to help to visualize what’s going on with compound data structures like arrays.
Finally, at the end Share in comment section if you want to know more about print and echo statement.
Comments (3)