Sie sind auf Seite 1von 6

Got it!

This site uses cookies to deliver our services and to show you relevant ads. By
using our site, you acknowledge that you have read and understood our Privacy
Policy. Your use of w3resource Services, is subject to these policies More info

w3resource
menu
Inviting useful, relevant, well-written and unique guest posts.
share
Home
PHP Home
?PHP Echo and Print Statements
Echo statement
Print
PHP echo statement
Last update on November 09 2019 06:56:39 (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

<?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 />';
?>
Copy
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']}" ;
?>
Copy
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>";
?>
Copy
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>";
?>
Copy
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>";
?>
Copy
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>";
?>
Copy
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>";
?>
Copy
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>";
?>
Copy
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>";
?>
Copy
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>";
?>
Copy
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>";
?>
Copy
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>";
?>
Copy
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> ";
?>
Copy
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>";
?>
Copy
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

<?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>";
?>
Copy
View the example in browser

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>";
?>
Copy
View the example in browser

Practice here online :

Previous: PHP Variables


Next: Print

?
Inviting useful, relevant, well-written and unique guest posts

New Content published on w3resource :


Python Numpy exercises
Python GeoPy Package exercises
Python Pandas exercises
Python nltk exercises
Python BeautifulSoup exercises
Form Template
Composer - PHP Package Manager
PHPUnit - PHP Testing
Laravel - PHP Framework
Angular - JavaScript Framework
React - JavaScript Library
Vue - JavaScript Framework
Jest - JavaScript Testing Framework
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike
3.0 Unported License.
�w3resource.com 2011-2019
PrivacyAboutContactFeedbackAdvertise?

Das könnte Ihnen auch gefallen