PHP echo and print - GeeksforGeeks

06 Dec, 2021
In this article, we will see what is echo & print statements in PHP, along with understanding their basic implementation through the examples. The echo is used to display the output of parameters that are passed to it. It displays the outputs of one or more strings separated by commas. The print accepts one argument at a time & cannot be used as a variable function in PHP. The print outputs only the strings.
Note: Both are language constructs in PHP programs that are more or less the same as both are used to output data on the browser screen. The print statement is an alternative to echo.
PHP echo statement: It is a language construct and never behaves like a function, hence no parenthesis is required. But the developer can use parenthesis if they want. The end of the echo statement is identified by the semi-colon (‘;’).  It output one or more strings. We can use ‘echo‘ to output strings, numbers, variables, values, and results of expressions. Below is some usage of echo statements in PHP: 
Displaying Strings: We can simply use the keyword echo followed by the string to be displayed within quotes. The below example shows how to display strings with PHP.
PHP
<?php    echo "Hello,This is a display string example!";?>
Output:
Hello,This is a display string example!
Displaying Strings as multiple arguments: We can pass multiple string arguments to the echo statement instead of a single string argument, separating them by comma (‘,’) operator. For example, if we have two strings i.e “Hello” and “World” then we can pass them as (“Hello”, “World”).
PHP
<?php    echo "Multiple ","argument ","string!";?>
Output: 
Multiple argument string!
Displaying Variables: Displaying variables with echo statements is also as easy as displaying normal strings. The below example shows different ways to display variables with the help of a PHP echo statement.
PHP
<?php  // 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
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 many times. It is also a language construct, and so we may not use parenthesis i.e print or print(). 
The main difference between the print and echo statement is that echo does not behave like a function whereas print behaves like a function. The print statement can have only one argument at a time and thus can print a single string. Also, the print statement always returns a value of 1. Like an echo, the print statement can also be used to print strings and variables. Below are some examples of using print statements in PHP: 
Displaying String of Text: We can display strings with the print statement in the same way we did with echo statements. The only difference is we cannot display multiple strings separated by comma(,) with a single print statement. The below example shows how to display strings with the help of a PHP print statement.
PHP
<?php    print "Hello, world!";?>
Output: 
Hello, world!
Displaying Variables: Displaying variables with print statements is also the same as that of echo statements. The example below shows how to display variables with the help of a PHP print statement.
PHP
<?php      // 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
Difference between echo and print statements in PHP:
echo accepts a list of arguments (multiple arguments can be passed), separated by commas.
print accepts only one argument at a time.
It returns no value or returns void. 
It returns the value 1.
 It displays the outputs of one or more strings separated by commas.
The print outputs only the strings.
It is comparatively faster than the print statement.
It is slower than an echo statement.
References:
http://php.net/manual/en/function.echo.php
http://php.net/manual/en/function.print.php
This article is contributed by Barun Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Vaishalia
ManasChhabra2
geetanjali16
bhaskargeeksforgeeks
PHP-basics
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to execute PHP code using command line ?
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
PHP in_array() Function
How to pop an alert message box using PHP ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?