How to echo HTML in PHP ?

31 Jul, 2021
While making a web application with PHP, we often need to print or echo few results in form of HTML. We can do this task in many different ways. Some of methods are described here:
Using echo or print: PHP echo or print can be used to display HTML markup, javascript, text or variables.Example 1: This example uses PHP echo to display the result.<?php     $name = "GeeksforGeeks";    echo "<h1>Hello User, </h1> <p>Welcome to {$name}</p>";?>Output:Example 2: This example uses PHP print to display the result.<?php     $name = "GeeksforGeeks";    print "<h1>Hello User, </h1> <p>Welcome to {$name}</p>";?>Output :Using echo shorthand or separating HTML: PHP echo shorthand can be used to display the result of any expression, value of any variable or HTML markup.Example 1: This example uses PHP echo shorthand to display the result.<?php     $name = "GeeksforGeeks";?>   <?= "<h1>Hello User,</h1>    <h1>{$name} welcomes you</h1>" ?>Output:Example 2: Separating HTML from PHP<?php    $num = 2;    for ($i = 1; $i <= 10; $i++) { ?>       <p><?= $num ?> * <?= $i ?>        = <?= $num * $i ?></p>   <?php    }?>Output:Using heredoc: We can use <<< heredoc to print the html. <<< must be followed by an identifier and line break. The same identifier is used to close the body of heredoc.Syntax:<<<GFG
// HTML Markup
GFG;
Note: The ending identifier must not be indented.Example:<?php  echo <<<GFG    <h1>GeeksforGeeks</h1>    <p>I am in heredoc with identifier 'GFG' .</p>GFG;  ?>Output:
Using echo or print: PHP echo or print can be used to display HTML markup, javascript, text or variables.Example 1: This example uses PHP echo to display the result.<?php     $name = "GeeksforGeeks";    echo "<h1>Hello User, </h1> <p>Welcome to {$name}</p>";?>Output:Example 2: This example uses PHP print to display the result.<?php     $name = "GeeksforGeeks";    print "<h1>Hello User, </h1> <p>Welcome to {$name}</p>";?>Output :
Example 1: This example uses PHP echo to display the result.
<?php     $name = "GeeksforGeeks";    echo "<h1>Hello User, </h1> <p>Welcome to {$name}</p>";?>
Output:
Example 2: This example uses PHP print to display the result.
<?php     $name = "GeeksforGeeks";    print "<h1>Hello User, </h1> <p>Welcome to {$name}</p>";?>
Output :
Using echo shorthand or separating HTML: PHP echo shorthand can be used to display the result of any expression, value of any variable or HTML markup.Example 1: This example uses PHP echo shorthand to display the result.<?php     $name = "GeeksforGeeks";?>   <?= "<h1>Hello User,</h1>    <h1>{$name} welcomes you</h1>" ?>Output:Example 2: Separating HTML from PHP<?php    $num = 2;    for ($i = 1; $i <= 10; $i++) { ?>       <p><?= $num ?> * <?= $i ?>        = <?= $num * $i ?></p>   <?php    }?>Output:
Example 1: This example uses PHP echo shorthand to display the result.
<?php     $name = "GeeksforGeeks";?>   <?= "<h1>Hello User,</h1>    <h1>{$name} welcomes you</h1>" ?>
Output:
Example 2: Separating HTML from PHP
<?php    $num = 2;    for ($i = 1; $i <= 10; $i++) { ?>       <p><?= $num ?> * <?= $i ?>        = <?= $num * $i ?></p>   <?php    }?>
Output:
Using heredoc: We can use <<< heredoc to print the html. <<< must be followed by an identifier and line break. The same identifier is used to close the body of heredoc.Syntax:<<<GFG
// HTML Markup
GFG;
Note: The ending identifier must not be indented.Example:<?php  echo <<<GFG    <h1>GeeksforGeeks</h1>    <p>I am in heredoc with identifier 'GFG' .</p>GFG;  ?>Output:
Syntax:
<<<GFG
// HTML Markup
GFG;

Note: The ending identifier must not be indented.
Example:
<?php  echo <<<GFG    <h1>GeeksforGeeks</h1>    <p>I am in heredoc with identifier 'GFG' .</p>GFG;  ?>
Output:
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
shubham_singh
PHP-basics
Picked
PHP
PHP Programs
Web Technologies
Web technologies Questions
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.