PHP Echo Statement with Example
In this tutorial, we will understand the echo statement in PHP and its usage with the help of examples.
PHP provides two basic ways to display output data in the browser. You can use any of the two statements in your program.
- Echo statement
- Print statement
Both echo and print statements in PHP are used to send the output data to the browser. However, both are almost the same, but there are few differences between them which we will understand one by one.
Echo Statement in PHP
The echo statement in PHP is used to display output data in the browser. In other words, if you want to send back the output data to the browser, use the echo statement.
You can use the echo statement with parentheses or without parentheses such as echo or echo(). This is because the echo statement is a language construct in PHP, not a function, meaning you can use it without parentheses.
This makes it faster and simpler for displaying text or HTML code. The general syntax to use the echo statement in PHP program is as follows:
echo expression1 [, expression2, ...];
In the above syntax, the echo statement starts with an echo keyword. expression1, expression2, …: These are the expressions which can be strings, variables, or a combination of both that you want to output.
You can pass multiple expressions or parameters separated with commas to the echo statement. Although, multiple expressions or parameters (arguments) are rarely passed as output.
Note that the echo statement does not return any value, which makes it faster than compared to the print statement.
Example based on Echo Statement
Let us take some examples where we will understand how to use the echo statement in PHP program.
Example 1:
<?php
// Simple string.
echo "Hello world!";
?>
Output: Hello world!
In this example, we have used the echo statement that displays the text “Hello world!” on the web page. The double quotes indicate the start and end of the text to display. However, you can also use the single quote to display message without any variables or arrays.
Example 2:
<?php
// Multiple strings separated by commas.
echo "Hello, ", "World!", " How are you?";
?>
Output: Hello, World! How are you?
If you have a much larger amount of text to display, you can span the string over multiple lines.
Example 3:
<?php
echo "Hello, this string
spans over
multiple lines.";
?>
Output: Hello, this string spans over multiple lines.
If you want to display any value or text assigned to a variable, you do not need to use quotation marks. Let’s take an example in which we will display text assigned to the variable msg.
Example 4:
<?php
$msg = "Welcome to Scientech Easy's PHP tutorial.";
echo $msg;
?>
Output: Welcome to Scientech Easy's PHP tutorial.
Let’s take an example in which we will display a combination of value or text assigned to a variable and a string.
Example 5:
<?php
$name = "Alice";
echo "Hello, ", $name, "!";
?>
Output: Hello, Alice!
Let’s take an example in which we will display value or text assigned to a variable within a string.
Example 6:
<?php
$name = "Alice";
echo "Hello! $name";
?>
Output: Hello! Alice
Let’s take an example in which we will use the echo statement with HTML code.
Example 7:
<?php
echo "<h1>Welcome to Scientech Easy!</h1>";
echo "<p>Learn PHP scripting programming language in simple words.</p>";
?>
Output: Welcome to Scientech Easy! Learn PHP scripting programming language in simple words.
In this example, we have used the PHP echo statement to output HTML content.
Example 8:
<?php
echo("Learn PHP language.");
?>
Output: Learn PHP language.
In this example, we have used the echo statement with parentheses. However, it is not necessary because it behaves the same way as without parentheses.
Key Points to Remember
There are the following key points that you should keep in mind to use the echo statement in PHP program. They are as:
- The purpose of using echo statement is to output text, variables, or HTML to the web browser.
- The echo is a language construct, which means that it is not a function. Therefore, you don’t need to use parentheses.
- You write echo keyword followed by one or more expressions or parameters separated by commas.
- The echo does not return a value, so you cannot use in expressions that will return a value.
- You can pass multiple parameters separated by commas, but only without parentheses.
- The echo is slightly faster than print because it doesn’t return a value.
- You can directly use it to output HTML tags as part of the string.
- Use the dot (.) operator to concatenate strings and variables for output.
These are the key points that tell a concise overview of the echo statement’s functionality and usage in PHP language.