1. What is PHP Echo Statement?

In this tutorial, we will learn about the PHP echo statement, which we can utilize, in order to display the data output. Basically, PHP echo statement enable us to get some output from our programs in the form of HTML or simply text data. The output data by this statement can be of any type like time, date, a variable, a string, arrays, or even HTML markup. Moreover, this echo statement works almost similar to the PHP print statement and there is only a slight difference.

Tutorial Contents:

  1. Introduction to Echo Statment
  2. Syntax Types of PHP Echo Statment
  3. PHP Echo Statement Examples
  4. Get Time and Date Output Using Echo
  5. Conclusion

2. Syntax of PHP Echo Statement

Primarily, there are two syntax types for this PHP echo statement and both are the same with only a slight difference of parenthesis (). We can utilize both syntax variants just like the below instances.

2.1. Syntax Type 1

  • The echo keyword
  • Starting " or ' quotation mark
  • The Data to display come after the initial quotation mark
  • Ending " or ' quotation mark
  • Termination with a ; as it is a must in PHP statements

Syntax Type 1

<?php echo "Some data here"; ?>

2.2. Syntax Type 2

  • The echo keyword
  • The starting parenthesis (
  • Starting " or ' quotation mark
  • The Data to display come after the initial quotation mark
  • Ending " or ' quotation mark
  • The ending part of the parenthesis )
  • Termination with a ; as it is a must in PHP statements

Syntax Type 2

<?php echo("Some data here"); ?>

3. PHP Echo Statement Example

As the intro part of this tutorial says that we use a PHP echo statement to display the data output after some processing. Let us learn the functionality of this statement with the help of some examples.

3.1. String Output Example

The below example outputs a string using this statement.

Echo String Example

<?php

/* Syntax Type 1 */
echo "Hello World!";
/* Syntax Type 2 */
echo("My name is John, and I am 20 years old.");

?>
Hello World!
My name is John, and I am 20 years old.

3.2. Variable Output Example

The below example outputs the variable data using this PHP echo statement, and it also renders any HTML entities that are within the quotations.

Echo Variable Example

<?php

$name = "John";
$age = 20;

/* Syntax Type 1 */
echo "My name is : " . $name;
/* Syntax Type 2 */
echo("My age is : " . $age);

?>
My name is : John
My age is : 20

3.3. Object Output Example

The below example outputs object data using this statement.

Echo Object Example

<?php

/* Object Data Output */
class Book {
	public $title;
	public $author;
	public $price;
	public $pages;
}
$book = new Book;
$book->title = "The World as I see it";
$book->author = "Albert Einstein";
$book->pages = 97;
$book->price = "150$";

/* Syntax Type 1 */
echo "Title : " .$book->title;
echo "Author : " .$book->author;
/* Syntax Type 2 */
echo("Pages : " .$book->pages);
echo("Price : " .$book->price);

?>
Title : The World as I see it
Author : Albert Einstein
Pages : 97
Price : 150$

4. PHP Echo Statement For Date & Time

We have learned previously, how to get the output of different data types using this echo statements. Now, we will learn how to print the current date and time using this statement. It is really simple and efficient and with a simple command, we can get the desired output.

4.1. Date Output Using echo

We will utilize the first type of syntax to output the date.

Date Example

<?php

/* Display current date */
echo date("Y-m-d");

?>

4.1. Time Output Using echo()

In order to get the current time output, we will use the second syntax type.

Time Example

<?php

/* Display current time */
echo(date("h:i:s A"));

?>

5. Conclusion

As we have seen that, the PHP echo function is very useful, and we can utilize it to output any data type while writing our script, including HTML markup. We just have to write the appropriate syntax to display the data. Both syntax types are equally usable, however, the syntax without the parenthesis is preferred by developers in writing PHP code. Moreover, there is a slight difference in using the echo and print statements in PHP programming.

Give Us Your Feedback
OR
If You Need Any Help!
Contact Us