How to Write HTML Inside PHP
This tutorial will introduce a few methods to write HTML in a PHP file.
Use the echo
Function to Write HTML in PHP
In the first method, we will discuss how to write HTML inside the echo
function in a PHP file. The PHP echo
function is used to output the expressions. The expressions are the string expressions. It is not mandatory to write the parenthesis while using the echo
function, as echo
is not really a function. It is a language construct and does not have any return types. The function can take multiple parameters.
We can use the echo
function to display the HTML elements. For example, create a PHP file and create two variables, $song
, and $artist
. Write some values as shown in the example below in the variables. Then, use the echo
function to create a table
tag. Use the echo
function on each line to write all the table tags like th
, tr
, and td
. Inside the td
tags, concatenate the $song
and $artist
variables as "<td>" .$songs.'"</td>"
. Finally, close the table
tag inside the echo
function.
The example below will create an HTML table with a row and two columns in a PHP file.
Example Code:
<?php
$song = "Echoes";
$artist = "Pink Floyd";
echo "<table>";
echo "<tr>";
echo "<th> Songs </th>";
echo "<th> Artists </th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$song."</td>";
echo "<td>".$artist."</td>";
echo "</tr>";
echo "</table>";
?>
Output:
Songs Artists
Echoes Pink Floyd
We can also enclose all the HTML inside one echo
function to achieve the same result.
Example Code:
<?php
$song = "Echoes";
$artist = "Pink Floyd";
echo"<table>
<tr>
<th> Songs </th>
<th> Artists </th>
</tr>
<tr>
<td>".$song."</td>
<td>".$artist."</td>
</tr>
</table>";
Output:
Songs Artists
Echoes Pink Floyd
In this way, we can write HTML inside a PHP file.
Write PHP Inside HTML in PHP File
We can also write HTML inside a PHP file without using the echo
function. The HTML can be rendered in a .php
file. In the first method, HTML was written inside the PHP tags. Therefore, we used the echo
functions to display the HTML. Here, we will write HTML outside the PHP tags. But we should open the PHP tags if we have to use the PHP variables in HTML. We can use the <?=$someVar?>
syntax to display the PHP variables inside HTML. The syntax is the shortcut for the echo
function.
We will use the same variables created in the method above for the demonstration in this method. First, create the variables inside the PHP tags. After closing the tag, create a table
tag. Create the table structure just as in the method above. In the td
tags, open the PHP tag and display the variables as <?=$song?>
and <?=$artist?>
.
As a result, we can see an HTML table on the webpage. In this way, we can write HTML inside a PHP file.
Example Code:
<?php
$song = "Echoes";
$artist = "Pink Floyd";
?>
<table>
<tr>
<th> Songs </th>
<th> Artists </th>
</tr>
<tr>
<td> <?=$song?> </td>
<td> <?=$artist?> </td>
</tr>
</table>
Output:
Songs Artists
Echoes Pink Floyd
Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.
LinkedIn