Difference between Echo and Print in PHP

What is PHP?

PHP stands for Hypertext Preprocessor. It is a multipurpose scripting language that is open-source for its users. It is used by developers for web development because it can be easily embedded into HTML. PHP is easy to understand and provides advanced features for professional users. A major advantage of PHP is that it is executed on the server, generating HTML, which is then sent to the client. The client can see the output by running the script without knowing the underlying code.

PHP is a must for students and professional developers to become better Software Engineers, especially in Web Development Domain.

Application Areas of PHP

  • PHP can perform system functions such as to read, write, create, open and close files.
  • It gathers data from files, saves data to a file, you can also send and receive data through e-mails.
  • It can access cookies variables and it can also set cookies.
  • It can help you restrict users from accessing some pages of your website.
  • It can also be used to encrypt data.

What is a Statement in PHP?

A statement can be anything, such as an assignment, loops, calling of a function, a conditional statement, etc. A statement always ends with a semicolon.

There are so many functions available for displaying output in PHP. In this article, we will study print and echo functions for displaying output in PHP and also the difference between the two. The basic functions for displaying output in PHP are as follows:

  • Print() Function
  • Echo() Function
  • Printf() Function
  • Sprintf() Function
  • Var_dump() Function
  • Print_r() Function

What is an Echo Statement in PHP?

An echo statement in PHP is a statement that is used to print the output on the screen. An echo statement can be used with as well as without parenthesis. An echo statement will always end with a semi-colon.

  • echo or echo()

Output:

Good Morning!
Good Morning!
123

You can see in the above example that the echo statement uses both single as well as double quotes. In the above example, we used an echo statement without parenthesis.

Output:

Good Morning!
HelloMorning!

In the above example, we used an echo statement with parenthesis. Also, we can see that an echo statement can also be used to print a group of strings.

Output:

GoodMorning

In the above example, we have concatenated two strings, thus echo can also be used for the concatenation of two strings.

Output:

300

In the above example, we have used an echo statement to add two variables and print the result.

Output:

Good Morning

In the above example, we have used an echo statement with HTML. HTML tags should be enclosed in double-quotes.

Return type:

The echo statement does not have any return type. Thus, no value is returned.

What is the difference between using single quotes and double quotes?

Single quotes are used to print the variable name and not its value whereas; double quotes are used to print the value of the variable.

Here is an example which shows the difference between single quotes and double quotes.

Output:

sky is blue
sky is $color

What is a Print Statement in PHP?

As the name suggests, the print statement is also used to print the output on the screen. Print statement can be used with as well as without parenthesis.

  • print or print()

Output:

Good Morning!
Good Morning!
123;

In the above examples, we can see how a print statement is used to print strings which is similar to the echo statement we studied earlier.

Output:

300

In the above example, a print statement is used to add two variables and print the output(similar to an echo statement).

Output:

Good Morning

In the above example, we can see how a print statement is used with HTML tags.

Output:

GoodMorning

The above example shows how a print statement is used for concatenating two strings.

Output:

PHP Parse error: syntax error, unexpected ',' in line 2

In the above example, when we try to print multiple arguments together, the compiler throws an error. Thus, the print statement does not support multiple arguments, unlike the echo statement which allows the printing of multiple arguments and throws no error.

Output:

Good Morning....
What is your Roll No.?
My Roll No. is : 12
4567.67

Return type of Print Statement

The print statement always returns an integer value that is, the print statement will always return 1.

Difference between Print and Echo Statements in PHP

Since both print and echo statements are used to display the output, is there any difference between the two or are they same?

So the answer is, yes both statements are used to display the output but there are a few differences between the two.

PrintEcho
Print statement does not support multiple arguments. Passing multiple arguments to it at a time will result in an error. For example: "Hello", "World"; will throw an error.Echo statement supports multiple arguments. For example, echo "Hello", "World"; is valid.
Print statement is slow as compared to the echo statement.Echo statement is faster than a print statement.
Print statement returns an integer value that is set to 1.Echo statement does not return any value thus echo statement has return type as void.
Print statement behaves like a function.Echo statement does not behave like a function.
Print statement is less preferable in comparison with the echo statement.Echo statement is more preferred by developers as it is faster than a print statement.





Latest Courses