Hit enter after type your search item

PHP echo and print Statements

Purpose of echo and print statements

The echo and print statements are used to display the strings in PHP programs.

While both, the echo and PHP print statements, are not functions but are language constructs.

That means you do not need to use parenthesis like in the normal functions.

Keep reading to learn how to use both statements with online examples in the following section.

Syntax of PHP echo statement

The syntax of echo PHP statement is:


Or you can use parenthesis as well:

To display a variable:

An example of displaying a string by echo

In the following example, three simple strings are displayed by using the echo statement. The first string is enclosed in double quotes and the second in double quotes with parenthesis and the third in the single quote. See the code and output:


Output:

An echo example with variables

This is almost the same example as above except we used a variable inside the string. The variable is used in three sentences; with double quotes, double quoted with parenthesis, and single quoted string.


Output:

You can see, in first two strings the variable name is replaced by the variable value i.e. for the double quoted text with and without parenthesis. While in the single quoted string, the variable itself is displayed.

An example of echo array

In this example, an array is created with three elements. Then a foreach loop is used to iterate through array elements. Inside the foreach loop, the echo statement is used to display the array keys and element values. See the example below:


Output:

In the demo, you can see first a string is used in the echo PHP statement. After that, the string is concatenated by “.” followed by the variable name containing the key of associated array. Then another concatenation containing the “=” sign and another variable containing the value of the current element of the array.

Syntax of print statement

Following is the general syntax to use the print statement:


 

Or


To display a variable by print statement:

A simple print PHP statement example

As such, the print statement is almost like the PHP echo statement. We are using the same example to show demos for the print statement.

In the following example, three simple strings are displayed by using the print statement.


Output:

The print statement with variables

This is the print statement with variables:


Output:

Print statement with arrays

In this example, an array is created along with a PHP foreach loop. The print statement is used to display the array keys and element values.


Output:

Why do we use echo statement

The echo statement is used to display the information on the screen. You may specify one or more strings in the echo statement.

Although you may enclose strings in parenthesis just like a function, the PHP echo is not a function but a language construct. It does not return any value. However, if you want to pass more than one argument in echo statement then you must not use the parenthesis.

The PHP print statement

The print statement is also used to display the strings on the screen. This is also a language construct and not a function in PHP, though, the print statement returns a value like a function.

The returned value is an integer type which is always 1.

Difference between echo and print

There is not much difference between the two statements:

  • Both, echo and print are language constructs.
  • Both display the strings.
  • The echo statement does not return any value.
  • The print statement returns an integer, which is always 1. However, the print statement is not a function as well.

How to use echo statement

This is how the echo statement is used generally:


So, the echo statement is followed by the string in double or single quotes.

To display a variable in echo statement, you may use this:


You may also display strings in multiple lines while using the echo statement as follows:

You can display a string that also includes variable names inside the PHP echo statement as shown below:

In that case, $var will be replaced by the value in the $var variable. However, if you use the single quote for the same echo statement the variable will be displayed instead of its value.

You can display array elements by using the echo statement as below:

How to use the print statement

This is how you can display a simple string by using the PHP print statement:


You may enclose the same string in parenthesis as well:

Just like in the echo statement, you can use the double or single quotes in the print statement. However, if you use the variable names with strings in the single quote, the variable name will be displayed.

To display the variable’s value, you have to use double quotes.


 
This div height required for enabling the sticky sidebar