How many ways are there to output HTML using PHP

The con, the cat and t’e nation!

Have you heard of concatenation?

No?

Stay with me if you haven’t!

According to wikipedia:

String concatenation is the operation of joining character strings end-to-end. For example, the concatenation of “snow” and “ball” is “snowball”.

Concatenation is an import concept in PHP for outputting HTML. Also used in many other programming languages.

In this tutorial you will learn the many ways PHP can generate and output HTML to the browser.

So your users can view your pages.

There are many ways to output HTML to the browser.

As once you want to output variables and data from your database in well formatted HTML, single quotes and double quotes can cause all kinds of strange problems in rendering the HTML page in the browser if they aren’t coded correctly.

HTML output examples:

1) Straight HTML

Outside of your PHP tags, you simply output straight HTML like this

<?php // Some PHP Code ?>
<p>Hello World</p>
<?php// PHP Code continued... ?>

2) Straight HTML echoed out as string from PHP

Inside your PHP tags, you can output straight HTML like this

<?php
echo "<p>Hello World</p>";
?>

3) Straight HTML echoed out as string from PHP (single quotes within double quotes)

If your HTML needs attribute value pairs, as in the example giving the font a color of red with a style attribute. Then you will need quotes within quotes. In the first line of code, I’m using single quotes within the double quotes of the string.

An alternative is to escape the quotes with a backslash as show in the second line of code. Then you can use double quotes within double quotes. There is a good reason for needing to do this which we will come to in later example.

The first line is definitely easier to read in my opinion.

<?php
echo "<p style='color: red;'>Hello World</p>";
// Alternatively escaping the quotes
echo "<p style=\"color: red;\">Hello World</p>";
?>

4) Straight HTML echoed out as string from PHP (double quotes within single quotes)

As in example 3 above you can use quotes within quotes, but this time the quotes are reversed with the double quotes on the inside.

An alternative is to escape the quotes with a backslash as show in the second line of code. Then you can use single quotes within single quotes.

<?php
echo '<p style="color: red;">Hello World</p>';
// Alternatively escaping the quotes
echo '<p style=\'color: red;\'>Hello World</p>';
?>

5) Concatenating a variable with two strings

Here we take a string variable and concatenate with two other strings of the opening and closing paragraph tags.

<?php
// Create a string variable
$my_message = "Hello World";
// Echo out the paragraph tags, concatenating the string in between them
echo "<p>". $my_message ."</p>";
?>

6) Using an array to output a string within a foreach loop

Inside your PHP tags, you can output straight HTML like this

<?php
// Create a string variable
$test = "Hello World";
// Split the string into an array, each letter will be an item in the array
$arr = str_split($test);
// Echo the opening paragraph tag
echo "<p>";
// Loop through the array
foreach ($arr as $a)
{
// Echo each item
echo $a;
}
// Echo the closing paragraph tag
echo "</p>";
?>

7) Using a function to output HTML

Inside your PHP tags, you can output straight HTML like this by calling a function.

<?php
// Function to return a string
function fnOutput()
{
return "Hello World";
}
// Echo the opening paragraph tag
echo "<p>";
// Call the function
echo fnOutput();
// Echo the closing paragraph tag
echo "</p>";
?>

So there are 7 different ways of outputting HTML to the browser.

Are there more? For sure and I will add more to this list.

Please take the above examples and use them to practice with.

If you think you’ve got some better examples, let me know.

Leave a Reply