PHP Print | Difference between Echo and Print
Like the echo statement, the print statement in PHP is used to display the output data on the web browser. In other words, the print statement sends the output data back to the browser.
This statement returns a value 1, therefore, we can use it in complex expressions that require a return value. However, the echo statement does not return a value. Therefore, we cannot use it in the complex expression that need a return value.
This is the main difference between echo and print statements in PHP. Like echo statement, we can also use the print statement with parentheses or without parentheses such as print or print().
Like the echo, the print is an also language construct, but it behaves as it is a function because it returns a value 1.
Basic Syntax to Use Print Statement in PHP
The general syntax to use the print statement in PHP program is as follows:
print(expression);
or,
print expression;
In the above syntax, the print is a keyword. The parameter expression in the parentheses is the data you want to output on the browser. This can be a string, a variable, or a more complex expression. The main purpose of print statement is to display string values. Although, it can also output numbers and other data types converted to strings.
PHP automatically converts non-string values into strings. It handles this type of conversion seamlessly, allowing you to use print to display various types of data.
Note that you can only pass one argument in the print statement. This statement does not accept multiple arguments, unlike the echo statement accepts separated by commas.
Examples based on Print Statement
Let’s take some basic examples based on the PHP print statement to understand it better.
Example 1:
<?php
// Displaying string value.
print("Hello world!"); // print statement with parentheses.
?>
In this example, <?php is the start tag of PHP while ?> is the end tag of PHP. The print(“Hello world!”); is the print statement which will display the output “Hello world!” on the web browser.
Output: Hello world!
You can also write the print statement without parentheses. Let’s take an example on it.
Example 2:
<?php
print "Scientech Easy";
?>
Output: Scientech Easy
Let’s take an example in which we will display the value of a variable using the print statement in PHP.
Example 3:
<?php
$greeting = "Hello, PHP!";
print($greeting);
?>
Output: Hello, PHP!
Let’s take an example, in which we will use the print statement to concatenate strings and variables.
Example 4:
<?php
$name = "Alice";
print("Hello, " . $name . "!");
?>
Output: Hello, Alice!
Let’s take an example where we will display the output data in the browser by using print statement.
Example 5:
<?php
// Displaying the HTML Markup in the browser.
print("<h2>Welcome to My Website</h2>");
print("<p>Learn PHP Scripting Language step by step.</p>")
?>
Output:
PHP allows us to use the print statement with the non-string values. In this case, PHP automatically converts those values to strings. Here are a few examples of exhibiting this behavior.
Example 6:
<?php
// Printing an integer
$number = 42;
print $number; // Outputs: 42
// Printing a float
$floatNumber = 3.14;
print $floatNumber; // Outputs: 3.14
// Printing a boolean
$booleanValue = true;
print $booleanValue; // Outputs: 1 (true is converted to "1")
?>
Output: 423.141
In this example, we have used the print statement to display the values of different data types, such as integer, float, and boolean. When you will print numbers using the print statement, PHP converts them to their string representation, such as “42” and “3.14”.
Similarly, when you print a boolean value using the print statement, PHP converts the true value into the string “1”, and false into an empty string “”.
Common Errors with print Statement
1. Multiple Arguments: Unlike the echo statement, the print statement does not support multiple arguments. This will cause a syntax error:
<?php
// print "PHP", " Language"; // Syntax error
?>
2. Missing Semicolon: Always end statements with a semicolon. Omitting it will result in a parse error.
<?php
print("Hello, World!") // Missing semicolon
?>
3. Unmatched Quotes: Check quotation marks are matched properly. Unmatched quotes will cause syntax errors.
<?php
// print("Hello, World!); // Syntax error
?>
4. Incorrect Syntax: Using the print like a function with parentheses is optional for a single argument, but correct usage if used.
<?php
print("PHP, Language"); // Correct
print "PHP, Language"; // Also correct
?>
Difference Between echo and print Statements in PHP
Both echo and print statements in PHP are used to output data in the browser, but there are some differences between them. Let’s understand them with the help of examples.
1. Parameters:
The echo statement can take more than one parameter if you do not use parentheses. You can separate multiple arguments with commas. It will output them sequentially on the browser. However, when using parentheses with echo, you cannot pass multiple parameters because it behaves like it’s a function call, which considers it as a single argument.
The print statement, on the other hand, always takes only one parameter, regardless of whether you use parentheses. For example:
<?php
// echo with multiple parameters with no parentheses.
echo "Hello", " ", "World!"; // Outputs: Hello World!
// echo with parentheses for single argument only).
// This is incorrect and will cause an error:
// echo("Hello", " ", "World!");
// Correct usage with a single argument.
echo("Hello World!");
// print with a single parameter.
print "Hello, World!"; // Outputs: Hello, World!
print("Hello, World!"); // Outputs: Hello, World!
?>
2. Language Construct:
The echo is a language construct, not a function. However, the print is also a language construct, but it behaves as a function because it returns a value 1.
3. Return Value:
The print returns a value 1. On the other hand, the echo does not return a value. For example:
<?php
$result = print("This will return 1");
echo $result; // Outputs 1
?>
4. Performance:
The echo is faster than print because it does not return a value.
5. Usage:
In echo, you can pass multiple arguments separated by commas, whereas you can pass only one argument in the case of print. For example:
<?php
echo "Hello", " World!";
// print "Hello", " World!"; // This would cause an error
?>
Key Points to Remember
Here are some key points about the print statement in PHP that you should remember.
- Used to send output data to the browser.
- General syntax is print expression; or print(expression);.
- Can only accept one argument at a time.
- Returns a value of 1, which allows it to be used in expressions.
- Automatic type conversion takes place to convert the non-string data types (e.g., integers, floats, booleans) into their string representation.
- Parentheses are optional; both print “easy” and print(“easy”) are valid.
- Unlike echo, print does not support multiple parameters. You must concatenate strings if needed.
- The print is also a language construct in PHP, like echo. Although it behaves, like a function in some respects.
These are some important points that provide a concise overview of how the print statement works in PHP.