PHP Echo and Print

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP echo statement

Last update on September 09 2017 06:08:52 (UTC/GMT +8 hours)

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
  • PHP echo and HTML paragraph element
  • PHP echo and HTML table element
  • PHP echo and html anchor element
  • PHP echo and HTML header element
  • PHP echo and HTML list element

Display string, variable with echo

Syntax

 echo (arg1, arg2... )

Example : Simple string display

  1. <?php   
  2. echo 'One line simple string.<br />';   
  3. echo 'Two line simple string example<br />';   
  4. echo 'Tomorrow I 'll learn PHP global variables.<br />';   
  5. echo 'This is a bad command : del c:\*.* <br />';   
  6. ?>  

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  cannot generate a line break in browser.

Example : Variable inside echo statement

  1. <?php  
  2. // Variables inside an echo statement.  
  3. $abc='We are learning PHP';  
  4. $xyz='w3resource.com';  
  5. echo "$abc at $xyz <br />";  
  6. // Simple variable display  
  7. echo $abc;  
  8. echo "<br />"// creating a new line  
  9. echo $xyz;  
  10. echo "<br />"// creating a new line  
  11. // Displaying arrays  
  12. $fruits=array('fruit1'=>'Apple','fruit2'=>'Banana');  
  13. echo "Fruits are : {$fruits['fruit1']} and   
  14. {$fruits['fruit2']}" ;  
  15. ?>  

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 :

  1. <?php  
  2. // simple html statement.  
  3. echo 'One line simple string.<br />';  
  4. // display strings within paragraph with different color.  
  5. echo "<p> <font color=blue>One line simple string in   
  6. blue color</font> </p>";  
  7. echo "<p> <font color=red>One line simple string in red   
  8. color</font> </p>";  
  9. echo "<p> <font color=green> One line simple string in   
  10. green color</font> </p>";  
  11. ?>  

View the example in the browser

Example of PHP echo and html paragraph element with font color

  1. <?php  
  2. // simple html statement.  
  3. echo 'One line simple string.<br />';  
  4. // display strings within paragraph with different color.  
  5. 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>";  
  6. 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>";  
  7. 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>";  
  8. ?>  

View the example in the browser

Example of PHP echo and html paragraph element with font color, size

  1. <?php  
  2. echo "<p align='left'> <font color=blue  size='6pt'>This is left alignment 6pt</font> </p>";  
  3. echo "<p align='center'> <font color=blue  size='6pt'>This is center alignment 6pt</font> </p>";  
  4. echo "<p align='right'> <font color=blue  size='6pt'>This is right alignment 6pt</font> </p>";  
  5. ?>  

View the example in the browser

Example of PHP echo and html paragraph element with font color, size, and PHP variable

  1. <?php  
  2. $a=1000;  
  3. $b=1200;  
  4. $c=1400;  
  5. echo "<p> <font color=blue size='4pt'> Salary of Mr. A is :</font> <font color=red size='4pt'>$a$</font></p>";  
  6. echo "<p> <font color=blue size='4pt'> Salary of Mr. B is :</font> <font color=red size='4pt'>$b$</font></p>";  
  7. echo "<p> <font color=blue size='4pt'> Salary of Mr. C is :</font> <font color=red size='4pt'>$c$</font></p>";  
  8. ?>  

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 :

  1. <?php  
  2. $a=1000;  
  3. $b=1200;  
  4. $c=1400;  
  5. echo "<table border=1 cellspacing=0 cellpading=0>  
  6. <tr> <td><font color=blue>Salary of Mr. A is</td> <td>$a$</font></td></tr>   
  7. <tr> <td><font color=blue>Salary of Mr. B is</td> <td>$b$</font></td></tr>  
  8. <tr> <td><font color=blue>Salary of Mr. C is</td> <td>$c$</font></td></tr>  
  9. </table>";  
  10. ?>  

View the example in the browser

Example of PHP echo and HTML table element, font color and PHP variable

  1. <?php  
  2. $a=1000;  
  3. $b=1200;  
  4. $c=1400;  
  5. echo "<table style='border: 1px solid red;' cellspacing=0 cellpading=0>  
  6. <tr> <td><font color=blue>Salary of Mr. A is</td> <td>$a$</font></td></tr>  
  7. <tr> <td><font color=blue>Salary of Mr. B is</td> <td>$b$</font></td></tr>  
  8. <tr> <td><font color=blue>Salary of Mr. C is</td> <td>$c$</font></td></tr>  
  9. </table>";  
  10. ?>  

View the example in the browser

Example of PHP echo and HTML table element, font color, table border and PHP variable

  1. <?php  
  2. $a=1000;  
  3. $b=1200;  
  4. $c=1400;  
  5. echo "<table border=1  cellspacing=0 cellpading=0>  
  6. Monthly Salary Statement </table>";  
  7. echo "<table border=1 cellspacing=0 cellpading=0>  
  8. <tr> <td><font color=blue>Salary of Mr. A is</td> <td>$a$</font></td></tr>  
  9. <tr> <td><font color=blue>Salary of Mr. B is</td> <td>$b$</font></td></tr>  
  10. <tr> <td><font color=blue>Salary of Mr. C is</td> <td>$c$</font></td></tr>  
  11. </table>";  
  12. ?>  

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 :

  1. <?php  
  2. echo "<a href=echo-order-list.php>  
  3. Click here to see the current month salary   
  4. statement.</a>";  
  5. ?>  

View the example in the browser

Example of PHP echo and HTML anchor element with font color

  1. <?php  
  2. echo "<a href=echo-order-list.php>  
  3. <font color=blue size=4pt> Click here to  
  4. see the current month salary statement.  
  5. </font></a>";  
  6. ?>  

View the example in the browser

Example of PHP echo and HTML anchor element with font color, size

  1. <?php  
  2. echo "<a href='echo-order-list.php'   
  3. style='color: red; font-size: 10pt'>  
  4. Click here to see the current month salary statement.  
  5. </a>";  
  6. ?>  

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 :

  1. <?php  
  2. echo "<h1> This is header1 </h1> ";  
  3. echo "<h2> This is header2 </h2> ";  
  4. echo "<h3> This is header3 </h2> ";  
  5. echo "<h4> This is header4 </h2> ";  
  6. echo "<h5> This is header5 </h2> ";  
  7. echo "<h6> This is header6 </h2> ";  
  8. ?>  

View the example in the browser

Example of PHP echo and HTML header element and variable

  1. <?php  
  2. $a=1000;  
  3. $b=1200;  
  4. $c=1400;  
  5. echo "<h1> Salary of Mr. A is : $a$ </h1>";  
  6. echo "<h2> Salary of Mr. B is : $b$ </h2>";  
  7. echo "<h3> Salary of Mr. C is : $c$ </h3>";  
  8. ?>  

View the example in browser

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

  1. <?php  
  2. $a=1000;  
  3. $b=1200;  
  4. $c=1400;  
  5. echo "<ol align='left'> <font color=red size='4pt'> Salary statement for the Current month</font><br><li> <font color=blue>Salary of Mr. A is : $a$</font></li>  
  6. <li> <font color=blue>Salary of Mr. B is : $b$</font></li><br><li> <font color=blue>Salary of Mr. C is : $c$</font></li>  
  7. </ol>";  
  8. ?>  

View the example in browser

Example of PHP echo and HTML unordered list

  1. <?php  
  2. $a=1000;  
  3. $b=1200;  
  4. $c=1400;  
  5. echo "<ul align='left'> <font color=red   
  6. size='4pt'> Salary statement for the Current month  
  7. </font> <li> <font color=blue>Salary of Mr. A is :   
  8. $a$</font></li>  
  9. <li> <font color=blue>Salary of Mr. B is :   
  10. $b$</font></li>  
  11. <li> <font color=blue>Salary of Mr. C is :  
  12. $c$</font></li>  
  13. </ul>";  
  14. ?>