PHP echo and print both are PHP statement which are used to display the output in PHP.
The PHP echo Statement:
The echo is a language construct not a function, so you don’t need to use parenthesis with it. The echo statement end with a semi-colon (;). We can use ‘echo’ to output one or more string or variables. The following example shows the use of echo in PHP:
In the below example, we simply use the keyword echo followed by the string to display a string of text.
Displaying Multiple Strings:
It is possible to pass multiple string arguments to the echo statement instead of a single string argument. These arguments are separated by comma (,) operator. If you want to pass two strings say “Hello” and “PHP” then you can pass them like (“Hello”, “PHP”).
Displaying HTML Code:
The below example shows you how to display HTML code using the echo statement:
The below example will show you how to display variables using the echo statement:
The PHP print Statement:
The print statement is used to print the given data to the browser. The difference between echo and print statement is print accepts single data and prints it on the browser. It is a PHP language construct and not a real function. So, you can use ‘print’ without parenthesis for creating a print statement.
Displaying String of Text:
The below example shows, how to display string with the help of a print statement. It is same as we did with echo statement but the only difference is that we can not display multiple strings separated by comma (,).
Displaying HTML Code:
The below example will show you how to display HTML code using the print statement:
The below example will show you how to display variables using the print statement:
Difference between echo and print in PHP:
|1||echo is a statement which used to display the output. it can be written with parentheses echo or without parentheses echo.||Print is also a statement which used to display the output. it can be written with parentheses print( ) or without parentheses print.|
|2||echo can pass multiple string arguments separated as ( , ).||Using print can doesn't pass multiple string arguments.|
|3||It doesn't return any value.||It always return 1.|
|4||echo is faster than print.||It is slower than echo.|