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

PHP Echo

PHP’s `echo` is a language construct, not a function, so parentheses are unnecessary when using it. However, if you wish to echo more than one parameter, you must enclose them within parentheses.

Here’s the basic syntax for using echo in PHP:

				
					echo expression;

				
			

Where expression can be a string, a variable, or a combination of both. You can also echo multiple expressions separated by commas:

				
					echo expression1, expression2, ...;

				
			

Here,

  • echo: This is the language construct used to output content.
  • expression: This represents the content you want to output. It can be a string enclosed in quotes (‘single’ or “double”), a variable containing a string or other data types, or a combination of strings and variables.
PHP echo statement can be used to print the string, multi-line strings, escaping characters, variable, array, etc. Some important points that you must know about the echo statement are:
  • The echo statement is utilized to output content.
  • It can display strings, multi-line strings, escaped characters, variables, arrays, etc.
  • echo is a statement, not a function, used for displaying output.
  • It can be written with or without parentheses, i.e., echo or echo().
  • Unlike functions, echo doesn’t return any value.
  • Multiple strings can be passed to echo, separated by commas.
  • In terms of performance, echo is generally faster than the print statement.
 

Printing a Simple String:

				
					<?php
echo "Hello, world!";
?>

				
			

Output:

				
					Hello, world!
				
			

Printing a Multi-Line String:

There are two ways to print a multi-line string in PHP:

  1. Using Double Quotes with Newlines:
				
					<?php
$message = "This is a multi-line string.\nIt can span across multiple lines.";
echo $message;
?>

				
			

2. Using Heredoc Syntax (Triple Quotes):

				
					<?php
$message = <<<EOT
This is a multi-line string.
It can span across multiple lines.
EOT;

echo $message;
?>

				
			

Both methods produce the same output:

				
					This is a multi-line string.
It can span across multiple lines.
				
			

Printing Escaping Characters:

To print special characters like quotation marks within a string, you need to escape them using a backslash (\) before the character.

				
					<?php
echo "This string contains an escaped quotation mark \"";
?>

				
			

outputs:

				
					This string contains an escaped quotation mark "
				
			

Printing Variable Value:

				
					<?php
$name = "Maruti";
echo "Hello, $name";
?>

				
			

Output:

				
					Hello, Maruti
				
			

In this example, the variable $name is interpolated directly into the string using double quotes.

Scroll to Top