In PHP, echo and print are language constructs used to display output. They are commonly used to display text, variables, HTML, and the results of expressions.
echo and print can display output. The main difference is that echo can accept multiple arguments, while print accepts one argument and returns the value 1.
The echo construct displays one or more strings. It is frequently used to display messages and HTML content.
<?php
echo "Hello World!";
?>
The echo construct displays the text written inside the quotation marks.
You can use multiple echo statements to display different messages.
<?php
echo "My Name is Rahul";
echo "<br>";
echo "I am learning PHP";
?>
The HTML <br> tag creates a line break in the browser.
You can display the value of a variable using echo.
<?php
$name = "Rahul";
$age = 20;
echo $name;
echo "<br>";
echo $age;
?>
PHP replaces each variable with its current value when executing the statements.
The echo construct can output HTML markup. The browser then interprets that markup and displays the corresponding elements.
<?php
echo "<h2>Welcome to PHP</h2>";
echo "<p>Learn PHP step by step.</p>";
?>
Learn PHP step by step.
The browser displays a heading and a paragraph because PHP sends the HTML markup to the browser.
The print construct also displays output. Unlike echo, it accepts one argument and returns the value 1.
<?php
print "Hello from print!";
?>
For ordinary output, echo and print often serve the same purpose.
You can also use print to display the values of variables.
<?php
$name = "Amit";
$course = "PHP Programming";
print $name;
print "<br>";
print $course;
?>
Each print statement displays its argument. The HTML line break separates the output into two lines.
| Feature | echo | |
|---|---|---|
| Purpose | Displays output | Displays output |
| Arguments | Can accept multiple arguments | Accepts one argument |
| Return Value | No return value | Returns 1 |
| Parentheses | Usually optional | Usually optional |
| Common Usage | Displaying text and HTML | Displaying text and HTML |
<?php
echo "Hello ", "Students!";
?>
The multiple arguments are passed to echo separated by commas. This is not valid syntax for print.
Parentheses are not required when using echo or print with a single expression.
<?php
echo "Hello PHP";
print "Welcome Students";
echo("Learning PHP");
print("PHP Tutorial");
?>
All four statements are valid. Notice that no spaces or line breaks appear between the messages unless you output them explicitly.
echo and print display output in PHP.echo can accept multiple arguments.print accepts one argument.print returns the value 1.<br> to create a line break in HTML output.Complete the following tasks:
echo.print.<br> to display messages on separate lines.echo to display an HTML heading and paragraph.<?php
echo "<h2>Soopro Pathshala</h2>";
print "Welcome to PHP Tutorials";
echo "<br>";
echo "Keep Learning!";
?>
1. Which PHP construct can accept multiple arguments?
2. What value does print return?
3. Which HTML tag creates a line break?