PHP echo statement
Description
In PHP the two basic constructs to get outputs are echo and print. Actually, echo() is not a function, it is a language construct, therefore, you can use it without parentheses.
Contents:
Display string, variable with echo
Syntax
echo (arg1, arg2... )
Example : Simple string display
<?php
echo 'One line simple string.<br />';
echo 'Two line simple string example<br />';
echo 'Tomorrow I \'ll learn PHP global variables.<br />';
echo 'This is a bad command : del c:\\*.* <br />';
?>
All the above echo commands simply display the corresponding string, here we have used an additional html command <br /> at the end of each echo statement to generate a line break as \n cannot generate a line break in browser.
Example : Variable inside echo statement
<?php
// Variables inside an echo statement.
$abc='We are learning PHP';
$xyz='w3resource.com';
echo "$abc at $xyz <br />";
// Simple variable display
echo $abc;
echo "<br />"; // creating a new line
echo $xyz;
echo "<br />"; // creating a new line
// Displaying arrays
$fruits=array('fruit1'=>'Apple','fruit2'=>'Banana');
echo "Fruits are : {$fruits['fruit1']} and
{$fruits['fruit2']}" ;
?>
Output:
We are learning PHP at w3resource.com We are learning PHP w3resource.com Fruits are : Apple and Banana
View the example in the browser
PHP echo and HTML paragraph element
We can display string, variables with echo function, additionally, we can embedded html commands into echo command. Here we have attached html paragraph element in various form into echo.
Example:
<?php
// simple html statement.
echo 'One line simple string.<br />';
// display strings within paragraph with different color.
echo "<p> <font color=blue>One line simple string in
blue color</font> </p>";
echo "<p> <font color=red>One line simple string in red
color</font> </p>";
echo "<p> <font color=green> One line simple string in
green color</font> </p>";
?>
View the example in the browser
Example of PHP echo and html paragraph element with font color
<?php
// simple html statement.
echo 'One line simple string.<br />';
// display strings within paragraph with different color.
echo "<p> <font color=blue font face='arial' size='2pt'>One line simple string in blue color, arial font and font size 2pt</font> </p>";
echo "<p> <font color=red font face='verdana' size='5pt'>One line simple string in red color, verdana font and font size 5pt</font> </p>";
echo "<p> <font color=green font face='courier' size='6pt'>One line simple string in green color, courier font and font size 6pt</font> </p>";
?>
View the example in the browser
Example of PHP echo and html paragraph element with font color, size
<?php
echo "<p align='left'> <font color=blue size='6pt'>This is left alignment 6pt</font> </p>";
echo "<p align='center'> <font color=blue size='6pt'>This is center alignment 6pt</font> </p>";
echo "<p align='right'> <font color=blue size='6pt'>This is right alignment 6pt</font> </p>";
?>
View the example in the browser
Example of PHP echo and html paragraph element with font color, size, and PHP variable
<?php
$a=1000;
$b=1200;
$c=1400;
echo "<p> <font color=blue size='4pt'> Salary of Mr. A is :</font> <font color=red size='4pt'>$a$</font></p>";
echo "<p> <font color=blue size='4pt'> Salary of Mr. B is :</font> <font color=red size='4pt'>$b$</font></p>";
echo "<p> <font color=blue size='4pt'> Salary of Mr. C is :</font> <font color=red size='4pt'>$c$</font></p>";
?>
View the example in the browser
PHP echo and HTML table element
We can display string, variables with echo function, additionally, we can embedded html elements into echo command. Here we have attached html table elements into echo.
Example:
<?php
$a=1000;
$b=1200;
$c=1400;
echo "<table border=1 cellspacing=0 cellpading=0>
<tr> <td><font color=blue>Salary of Mr. A is</td> <td>$a$</font></td></tr>
<tr> <td><font color=blue>Salary of Mr. B is</td> <td>$b$</font></td></tr>
<tr> <td><font color=blue>Salary of Mr. C is</td> <td>$c$</font></td></tr>
</table>";
?>
View the example in the browser
Example of PHP echo and HTML table element, font color and PHP variable
<?php
$a=1000;
$b=1200;
$c=1400;
echo "<table style='border: 1px solid red;' cellspacing=0 cellpading=0>
<tr> <td><font color=blue>Salary of Mr. A is</td> <td>$a$</font></td></tr>
<tr> <td><font color=blue>Salary of Mr. B is</td> <td>$b$</font></td></tr>
<tr> <td><font color=blue>Salary of Mr. C is</td> <td>$c$</font></td></tr>
</table>";
?>
View the example in the browser
Example of PHP echo and HTML table element, font color, table border and PHP variable
<?php
$a=1000;
$b=1200;
$c=1400;
echo "<table border=1 cellspacing=0 cellpading=0>
Monthly Salary Statement </table>";
echo "<table border=1 cellspacing=0 cellpading=0>
<tr> <td><font color=blue>Salary of Mr. A is</td> <td>$a$</font></td></tr>
<tr> <td><font color=blue>Salary of Mr. B is</td> <td>$b$</font></td></tr>
<tr> <td><font color=blue>Salary of Mr. C is</td> <td>$c$</font></td></tr>
</table>";
?>
View the example in the browser
PHP echo html anchor element
We can display string, variables with echo function, additionally, we can embedded html elements into echo command. Here we have attached html anchor element into echo.
Example:
<?php
echo "<a href=echo-order-list.php>
Click here to see the current month salary
statement.</a>";
?>
View the example in the browser
Example of PHP echo and HTML anchor element with font color
<?php
echo "<a href=echo-order-list.php>
<font color=blue size=4pt> Click here to
see the current month salary statement.
</font></a>";
?>
View the example in the browser
Example of PHP echo and HTML anchor element with font color, size
<?php
echo "<a href='echo-order-list.php'
style='color: red; font-size: 10pt'>
Click here to see the current month salary statement.
</a>";
?>
View the example in the browser
PHP echo and HTML header element
We can display string, variables with echo function, additionally, we can embedded html elements into echo command. Here we have attached html header element into echo.
Example :
<?php
echo "<h1> This is header1 </h1> ";
echo "<h2> This is header2 </h2> ";
echo "<h3> This is header3 </h2> ";
echo "<h4> This is header4 </h2> ";
echo "<h5> This is header5 </h2> ";
echo "<h6> This is header6 </h2> ";
?>
View the example in the browser
Example of PHP echo and HTML header element and variable
<?php
$a=1000;
$b=1200;
$c=1400;
echo "<h1> Salary of Mr. A is : $a$ </h1>";
echo "<h2> Salary of Mr. B is : $b$ </h2>";
echo "<h3> Salary of Mr. C is : $c$ </h3>";
?>
PHP echo and HTML list element
We can display string, variables with echo function, additionally, we can embedded html elements into echo command.
Here we have attached html ordered and unordered list elements into echo.
Example of PHP echo and HTML ordered list
<?php
$a=1000;
$b=1200;
$c=1400;
echo "<ol align='left'> <font color=red size='4pt'> Salary statement for the Current month</font>
<li> <font color=blue>Salary of Mr. A is : $a$</font></li>
<li> <font color=blue>Salary of Mr. B is : $b$</font></li>
<li> <font color=blue>Salary of Mr. C is : $c$</font></li>
</ol>";
?>
Example of PHP echo and HTML unordered list
<?php
$a=1000;
$b=1200;
$c=1400;
echo "<ul align='left'> <font color=red
size='4pt'> Salary statement for the Current month
</font> <li> <font color=blue>Salary of Mr. A is :
$a$</font></li>
<li> <font color=blue>Salary of Mr. B is :
$b$</font></li>
<li> <font color=blue>Salary of Mr. C is :
$c$</font></li>
</ul>";
?>
Practice here online :
Previous: PHP Variables
Next: Print
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/php/echo-print/echo.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics