PHP echo and print Statements

Featured image for PHP echo and print Statement Tutorial

Purpose of echo and print statements

  • The echo and print statements are used to display the strings in PHP programs.
  • 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:

echo “String to be displayed”;

Or you can use parenthesis as well:

echo (“String to be displayed”);

To display a variable:

echo $variable_name;

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.
  • The second is in double quotes with parenthesis, and the third is in the single quote.

See the code and output:

echo "This is echo demo with double quotes</BR>";
echo ("This is echo demo with double quote in parenthesis</BR>");
echo 'This is echo demo in single quote';

Output:

This is echo demo with double quotes
This is echo demo with double quote in parenthesis
This is echo demo in single quote

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.
$var_echo = "echo demo";
echo "This is $var_echo with double quotes</BR>";
echo ("This is $var_echo with double quote in parenthesis</BR>");
echo 'This is $var_echo in single quote';

Output:

This is echo demo with double quotes
This is echo demo with double quote in parenthesis
This is $var_echo in single quote

You can see, in the 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, a PHP 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:

$salaries = array(); 
$salaries["Anna"] = 5000;
$salaries["John"] = 4000;
$salaries["Aamir"] = 6000;

foreach($salaries as $key_arr => $val_arr){
    echo "The array element and value: " . $key_arr . " = " .$val_arr ."<br />";
}

Output:

The array element and value: Anna = 5000
The array element and value: John = 4000
The array element and value: Aamir = 6000

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 the 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:

print “String to be displayed”;

Or

print (“String to be displayed”);

To display a variable by print statement:

print $variable_name;

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.

print "This is print demo with double quotes</BR>";
print ("This is print demo with double quote in parenthesis</BR>");
print 'This is print demo in single quote';

Output:

This is print demo with double quotes
This is print demo with double quote in parenthesis
This is print demo in single quote

The print statement with variables

This is the print statement with variables:

$var_print = "print demo";
print "This is $var_print with double quotes</BR>";
print ("This is $var_print with double quote in parenthesis</BR>");
print 'This is $var_print in single quote';

Output:

This is print demo with double quotes
This is print demo with double quote in parenthesis
This is $var_print in single quote

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.

$salaries = array(); 
$salaries["Anna"] = 5000;
$salaries["John"] = 4000;
$salaries["Aamir"] = 6000;

foreach($salaries as $key_arr => $val_arr){
    print "The array element and value: " . $key_arr . " = " .$val_arr ."<br />";
}

Output:

The array element and value: Anna = 5000
The array element and value: John = 4000
The array element and value: Aamir = 6000

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:

echo “The string to be displayed”;

echo ‘A string with single quote”;

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:

echo $variable1;

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

echo “This is a string

with multiple lines”;

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

echo “This is a string with a $var name”;

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:

echo “The array element value is:” . $arr[1];

How to use the print statement

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

print “This is a print statement demo”;

You may enclose the same string in parenthesis as well:

print (“This is a print statement demo”);

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.

print “A string with a $var”;

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!