How to use HTML tags in PHP echo?
Example 1: This example demonstrates that how HTML tags and variable can be used in PHP echo to display the result.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!DOCTYPE html> <html> <body> <?php $name = "Code4Example"; echo "<h1>Hello User:</h1> <p>Welcome to {$name}</p>"; ?> </body> </html> |
Output:
Example 2: This example demonstrates that how HTML tags can be used in PHP echo to display the result by Separating HTML from PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!DOCTYPE html> <html> <body> <?php $number = 5; for ($i = 1; $i <= 10; $i++) { ?> <p> <?=$number."*".$i?> = <?=$number * $i ?> </p> <?php } ?> </body> </html> |
Output:
Example 3: We can use <<< heredoc to print the html. <<< must be followed by an identifier and line break.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!DOCTYPE html> <html> <body> <?php echo <<<END <h1>Code4Example</h1> <p>I am in heredoc with identifier 'END' .</p> \n END; ?> </body> </html> |
Output: