We have seen echo statement quite frequently in PHP codes of previous article. It is the most basic way for displaying output in PHP.
However, there are two basic ways to get output in PHP:-
- echo
PHP echo statement
In PHP ‘echo’ statement is a language construct and never behaves like a function, hence no parenthesis required. The end of echo statement is identified by the semi-colon (‘;’).
We can use ‘echo’ to output strings or variables. Below are some of the usage of echo statement in PHP:
-
Displaying Strings: We can simply use the keyword echo followed by the string to be displayed withing quotes. Below example shows how to display strings with PHP:
<?phpecho"Hello,This is a display string example!";?>chevron_rightfilter_noneOutput:
Hello,This is a display string example!
-
Displaying Strings as multiple arguments: We can pass multiple string arguments to the echo statement instead of single string argument, separating them by comma (‘,’) operator. For example, if we have two strings say “Hello” and “World” then we can pass them as (“Hello”,”World”). Below example shows how to do this:
<?phpecho"Multiple ","argument ","string!";?>chevron_rightfilter_noneOutput:
Multiple argument string!
-
Displaying Variables: Displaying variables with echo statement is also as easy as displaying normal strings. Below example shows different ways to display variables with the help of PHP echo statement:-
<?php//defining the variables$text="Hello, World!";$num1= 10;$num2= 20;//echoing the variablesecho$text."\n";echo$num1."+".$num2."=";echo$num1+$num2;?>chevron_rightfilter_noneOutput:
Hello, World! 10+20=30
The (.) operator in the above code can be used to concatenate two strings in PHP and the “\n” is used for a new line and is also known as line-break. We will learn about these in further articles.
PHP print statement
The PHP print statement is similar to the echo statement and can be used alternative to echo at many times.It is also language construct and so we may not use parenthesis : print or print(). The main difference between the print and echo statement is that print statement can have only one argument at a time and thus can print a single string. Also, print statement always returns a value 1.
Like echo, print statement can also be used to print strings and variables. Below are some examples of using print statement in PHP:
-
Displaying String of Text: We can display strings with print statement in the same way we did with echo statements. The only difference is we can not display multiple strings separated by comma(,) with a single print statement. Below example shows how to display strings with the help of PHP print statement:-
<?phpprint"Hello, world!";?>chevron_rightfilter_noneOutput:
Hello, world!
-
Displaying Variables: Displaying variables with print statement is also same as that of echo statement. The example below shows how to display variables with the help of PHP print statement:-
<?php//defining the variables$text="Hello, World!";$num1= 10;$num2= 20;//echoing the variablesprint$text."\n";print$num1."+".$num2."=";print$num1+$num2;?>chevron_rightfilter_noneOutput:
Hello, World! 10+20=30
Comparison between Echo and Print in PHP:

References:
This article is contributed by Barun Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- What is the difference between echo, print, and print_r in PHP?
- How to echo HTML in PHP ?
- PHP program to print an arithmetic progression series using inbuilt functions
- PHP | Print the last value of an array without affecting the pointer
- PHP str_pad to print string patterns
- Print PHP Call Stack
- How to print JavaScript console with PHP ?
- How to print all the values of an array in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- Print current day and time using HTML and JavaScript
- What is stacktrace and how to print in node.js ?
- How to select and upload multiple files with HTML and PHP, using HTTP POST?
- Difference and Similarities between PHP and C
- Node.js REPL (READ, EVAL, PRINT, LOOP)
- p5.js | print() function
- How to print the content of an object in JavaScript ?
- Print the content of a div element using JavaScript


