Both echo and print are used to show output data in PHP.
Let's discuss one by one.
echo
echo helps us to print one or more strings. It is simple, fast, and does not return any value.
Syntax of echo:
echo "Your text or variable here";
Example: single string
<?php
echo "Hello world";
?>
Output:
Hello world
Example: Multiple strings
To separate multiple strings, use a comma.
<?php
echo "Hello, ", "World!", " This is PHP.";
?>
Output:
Hello, World! This is PHP.
What is print?
The print statement also outputs text but is slightly slower than echo. It behaves like a function and always returns 1.
<?php
print "Hello, World!";
?>
Output:
Hello, World!
Note : print function does not work for printing multiple statements at once.
print is designed to return 1 every time it executes successfully. This return value indicates that the output operation was completed.
Example:
<?php
$result = print "Hello, World!";
echo "<br>Return value: " . $result;
?>
Output:
Hello, World!
Return value: 1
Differences Between echo and print:
Feature |
echo |
|
---|---|---|
Arguments |
Multiple (comma-separated) |
Single argument only |
Return Value |
None |
Returns 1 |
Speed |
Slightly faster |
Slightly slower |
20+ PHP Interview Questions on Echo and Print
Question 1. What is the echo statement in PHP?
Answer: echo is a PHP language construct used to output one or more strings to the browser.
Example:
<?php
echo "Hello, World!";
?>
Output:
Hello, World!
Question 2. What is the print statement in PHP?
Answer: print is a PHP language construct that outputs a single string to the browser and always returns 1.
Example:
<?php
print "Hello, PHP!";
?>
Output:
Hello, PHP!
Question 3. What is the main difference between echo and print?
Answer: echo can take multiple arguments and has no return value, while print takes a single argument and returns 1.
Example:
<?php
echo "Hello, ", "World!"; // Multiple arguments
print "Hello, World!"; // Single argument
?>
Output:
Hello, World!
Hello, World!
Question 4. Does echo return a value?
Answer: No, echo does not return a value; it simply outputs the string.
Example:
<?php
$result = echo "Test"; // This will cause an error
?>
Output: Syntax error (because echo cannot be used in assignments).
Question 5. Does print return a value?
Answer: Yes, print always returns 1, indicating successful execution.
Example:
<?php
$result = print "Test";
echo "<br>Return value: " . $result;
?>
Output:
Test
Return value: 1
Question 6. Can echo handle multiple arguments?
Answer: Yes, echo can output multiple strings separated by commas.
Example:
<?php
echo "PHP ", "is ", "awesome!";
?>
Output:
PHP is awesome!
Question 7. Can print handle multiple arguments?
Answer: No, print accepts only one argument.
Example:
<?php
print "PHP ", "is ", "awesome!"; // This will cause an error
?>
Output: Syntax error (only one argument allowed).
Question 8. Which is faster: echo or print?
Answer: echo is slightly faster because it’s simpler and doesn’t return a value.
Question 9. Are echo and print functions in PHP?
Answer: No, both are language constructs, not functions, so they don’t require parentheses.
Example:
<?php
echo "No parentheses needed";
print "No parentheses needed";
?>
Output:
No parentheses needed
No parentheses needed
Question 10. Can you use parentheses with echo and print?
Answer: Yes, parentheses can be used, but they’re optional since they are constructs, not functions.
Example:
<?php
echo("Hello!");
print("Hello!");
?>
Output:
Hello!
Hello!
Question 11. Can print be used in expressions?
Answer: Yes, because print returns 1, it can be used in expressions.
Example:
<?php
$condition = true;
$condition && print "Printed!";
?>
Output: Printed!
Question 12. Can echo be used in expressions?
Answer: No, echo cannot be used in expressions because it doesn’t return a value.
Example:
<?php
$condition = true;
$condition && echo "Printed!"; // This will cause an error
?>
Output: Syntax error.
Question 13. How do you output HTML with echo or print?
Answer: You can output HTML by including HTML tags in the string.
Example:
<?php
echo "<h1>Welcome to PHP!</h1>";
?>
Output:
<h1>Welcome to PHP!</h1>
Question 14. Can echo and print output numbers?
Answer: Yes, both can output numbers by converting them to strings.
Example:
<?php
echo 42;
print 100;
?>
Output:
42
100
Question 15. How do you concatenate strings with echo?
Answer: Use the dot (.) operator or multiple arguments with commas.
Example:
<?php
$name = "Alice";
echo "Hello, " . $name; // Using dot
echo "Hello, ", $name; // Using comma
?>
Output:
Hello, Alice
Hello, Alice
Question 16. What happens if you use echo or print without arguments?
Answer: It will cause a syntax error because both require at least one string to output.
Example:
<?php
echo; // Error
print; // Error
?>
Output: Syntax error.
Question 17. Can you use echo or print to output arrays directly?
Answer: No, directly outputting an array will cause an error. You need to access array elements or use functions like print_r.
Example:
<?php
$array = [1, 2, 3];
echo $array[0]; // Works
print_r($array); // Better for arrays
?>
Output:
1
Array ( [0] => 1 [1] => 2 [2] => 3 )
Question 18. What is the shorthand syntax for echo?
Answer: The shorthand <?= is used for echo in templates, but it requires short_open_tag to be enabled.
Example:
<?php $name = "Bob"; ?>
<p>Hello, <?= $name ?>!</p>
Output:
<p>Hello, Bob!</p>
Question 19. Can print be used in a ternary operator?
Answer: Yes, because print returns 1, it can be used in a ternary operator.
Example:
<?php
$age = 20;
print ($age >= 18) ? "Adult" : "Minor";
?>
Output: Adult
Question 20. How do echo and print handle variables?
Answer: Both can output variables by including them in the string or as arguments.
Example:
<?php
$name = "John";
echo "Hello, $name!";
print "Hello, $name!";
?>
Output:
Hello, John!
Hello, John!
Question 21. What happens if you mix echo and print in the same script?
Answer: They work together fine; echo outputs directly, and print outputs with a return value of 1.
Example:
<?php
echo "First ";
$result = print "Second";
echo " Return: $result";
?>
Output:
First Second Return: 1
Leave a comment
You must be logged in to post a comment.