PHP Tutorial
Control Statement
PHP Programs
PHP Functions
PHP Arrays
PHP Strings
PHP Math
PHP Form
PHP Include
State Management
PHP File

PHP Echo vs Print

In PHP, both echo and print are used to output strings, but there are some differences between them. Here is a comparison of the two:

Additional Details

  1. Usage:

    • echo: Can output multiple strings separated by commas, e.g., echo "Hello", "World";.
    • print: Outputs a single string, e.g., print "Hello World";.
  2. Performance:

    • echo is slightly faster since it does not return a value.
    • print returns 1, making it a bit slower.
  3. Return Value:

    • echo has no return value.
    • print returns 1, which can be useful in conditional statements.
  4. Number of Arguments:

    • echo can take multiple arguments separated by commas, e.g., echo "Hello", " ", "World";.
    • print takes a single argument.
  5. Concatenation:

    • echo allows for comma-separated strings, e.g., echo "Hello", " ", "World";.
    • print requires concatenation using the dot operator (.), e.g., print "Hello" . " " . "World";.
  6. Example with HTML:

    • echo "<h1>Hello, World!</h1>"; directly outputs HTML content.
    • print "<h1>Hello, World!</h1>"; also directly outputs HTML content.
  7. Example with Return Value:

    • echo cannot be used in expressions.
    • print can be used in expressions, e.g., if (print "Hello, World!") { /* do something */ }.

Example Code Snippets

Echo Example:

				
					print expression;
				
			
				
					<?php
echo "Hello, ", "World!"; // Outputs: Hello, World!
echo "<br>"; // Outputs a line break in HTML
echo "This is ", "PHP ", "echo"; // Outputs: This is PHP echo
?>

				
			

Print Example:

				
					<?php
print "Hello, World!"; // Outputs: Hello, World!
print "<br>"; // Outputs a line break in HTML
if (print "This is PHP print") { // Outputs: This is PHP print and returns 1
    print "<br>"; // Outputs a line break in HTML
}
?>

				
			

These details should give you a comprehensive understanding of the differences and use cases for echo and print in PHP.

Scroll to Top