PHP echo and print Statements
- 1. Purpose of echo and print statements
- 2. Syntax of PHP echo statement
- 3. An example of displaying a string by echo
- 4. Syntax of print statement
- 5. A simple print PHP statement example
- 6. Why do we use echo statement
- 7. The PHP print statement
- 8. Difference between echo and print
- 9. How to use echo statement
- 10. How to use the print statement
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:
1 |
echo “String to be displayed”; |
Or you can use parenthesis as well:
1 |
echo (“String to be displayed”); |
To display a variable:
1 |
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 and the second in double quotes with parenthesis and the third in the single quote. See the code and output:
1 2 3 |
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:
1 2 3 |
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.
1 2 3 4 |
$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:
1 2 3 |
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 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:
1 2 3 4 5 6 7 8 |
$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:
1 2 3 |
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 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:
1 print “String to be displayed”;
Or
1 |
print (“String to be displayed”); |
To display a variable by print statement:
1 |
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.
1 2 3 |
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:
1 2 3 |
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:
1 2 3 4 |
$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:
1 2 3 |
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.
1 2 3 4 5 6 7 8 |
$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:
1 2 3 |
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:
1 2 3 |
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:
1 |
echo $variable1; |
You may also display strings in multiple lines while using the echo statement as follows:
1 2 3 |
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:
1 |
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:
1 |
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:
1 |
print “This is a print statement demo”; |
You may enclose the same string in parenthesis as well:
1 |
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.
1 |
print “A string with a $var”; |