Ways for PHP to write in HTML

Note that the interleaving of commands in PHP language is possible, alternating them within a page written in HTML language, as many times as necessary (PHP tags can be opened and closed as many times as we want.

Ways for PHP to write in HTML | Learn PHP & MySQL | Note that the interleaving of commands in PHP language is possible, alternating them within a page written in HTML language, as many times as necessary (PHP tags can be opened and closed as many times as we want

Note that the interleaving of commands in PHP language is possible, alternating them within a page written in HTML language, as many times as necessary (PHP tags can be opened and closed as many times as we want.

For example:

<h1>This was written statically, in HTML </h1>
print (“<h2>Hello world! This was written by the PHP interpreter </h2>”);
<p>This was already written in HTML code</p>
print (“<p>This was also written by the PHP interpreter software</p>”);
<p><a href=”index.php”><?php print (“Back to Home Site, written by PHP”); ?></a></p>

Opening and closing PHP tags

Note that the PHP tag:

  1. It can be opened and closed on the same line it opened, or it can be closed on a different line. It is indistinct
  2. It can be interspersed within pre-existing HTML tags
  3. You can generate new HTML tags using an echo or print
  4. And it can open and close many times within a single page

The different types of opening and closing tags of a block written in the PHP language that we can find, are the following:

1. Standard opening and closing:

<?php xxxx ?>

Or also


This is the only universal syntax: it always works. It is the only recommended way and the one we are going to use:

2. Opening and closing short:

<? xxxx ?>

Or also


This syntax is known as short tags. It was widely used in the early years of PHP, but it is not standard. Not all configurations of the PHP interpreter enable its use, so a code that uses this syntax may stop working when it is placed on a server with another, more stringent configuration. For that reason, we do not recommend it.

3. Opening and closing by script tag:

<script language=”php”>xxxx</script>

Or also

<script language=”php”>

This syntax, although still supported, is unnecessarily long and it is very rare to find any code that uses it. Therefore, having no other window added, its use is not recommended.

4. ASP style tags:

<% xxx %>

Or also


Syntax in the style of the Microsoft ASP programming language: it is not standard, the possibility of using it depends on the configuration of the interpreter; therefore, its use is not recommended either.

In the previous examples, we used the print function, which "writes" in the source code of the page that is about to be sent moments later from the server to the user's browser, and that, as we can see, can not only write text, but also also HTML tags like the "<p>" or "<h1>" of the example, tags that are then interpreted by the browser like any other HTML code that had been written there originally.

Writing in the code with the print() function

The PHP language has a function that is one of the most used of all. We talk about the function print (), which tells the PHP interpreter software to "write" in the source code of the page that it will return to the user's browser-that which we put between its parentheses.

We have already intuitively used this function in the previous examples. If what we want is that a text be written in the code of the page, literally, we must write it in quotes within its parentheses.



If we only had to write text and never HTML code, we would not have problems but, as we must enclose the text to be displayed in quotes, we will have a problem when writing HTML code, which in turn has quotation marks inside.

In the following example, we will see why:

print(“<h1 class=”portada”>Welcome</h1>”);

This example will generate an error, because the quote located after the sign = is fulfilling, unintentionally, the function of closing the first of the quotes - the one that was opened at the beginning of the print after the initial parenthesis - and, therefore, the section of text is considered closed and the rest that follows that quote the PHP interpreter software does not know how to treat it, and warns it by displaying an error message on the screen.

A possible solution to the problem of quotation marks is to deactivate (this is called "escape") all intermediate double quotes, one by one, so that they do not terminate the text string before we get to the last double quote. indicates the term of the print function.

The escape character is the intertidal bar \ and serves not to execute the character that immediately follows it as if it were part of an order of the PHP language, but considers it as one more letter that must be written literally.

For this reason, the previous example will look like this:

print (“<h1 class=\”portada\”>Welcome</h1>”);

This works very well in short sentences, but the biggest inconvenience or annoyance that can cause us arises when we have to print long blocks of HTML code, since it is very likely that those blocks (maybe whole pages) we already have previously written, generated by our HTML code editor, and it is almost certain that they will possess numerous double quotes.

In these cases, we would be forced to the tedious task of finding the quotes one by one, and "escape" by putting an inverted bar or, failing that, we could use the search and character replacement tools of one of the HTML editors to search a quote and replace it with the escape bar plus the quote. But, both cases, it would be a long and boring task.

Much better than this, it would be to use simple quotes to delimit the beginning and end of the block of text to be printed:

print(‘<h1 class=”portada”>Welcome</h1>’);

And problem solved!!

How the "echo" command works

This command (it is not a function) can also optionally use single or double quotes to delimit what will be printed, in the same way as print. But, unlike print, it is not usual to wrap in parentheses what you will write


echo “Hello World in double quotes!”;
echo '<html>
<title>I enclose in single quotes</title>
<body>”This has double quotes," many quotes ", and it does not matter”

Note in passing that the code you write can be divided into multiple lines (PHP ignores both line breaks and blank spaces), and also point out another detail that we had not yet paid attention to: to terminate a sentence or order , a semicolon is added to the end of the line.

Big blocks: heredoc

When we need to write long blocks of HTML code, even with interleaved variables, we can use the heredoc construction that allows us to write large amounts of text, without having to escape characters inside.

It's easy to use. At the beginning of the block of text, we must place three times the sign "less than", like this: <<<followed by several alphanumeric characters (in the example that follows we have chosen EOT, but it could be any other combination of letters); then, we paste the block of text and HTML code that we will write and, to finish, we repeat the same three characters that indicated the beginning of the block.

Echo <<< EOT
<p>This text can have "quotes" inside without needing to escape them.</p>
<p>It also processes (replaces by its value) the $ variables that would fit within the code </p>
<p>This construction of the language called heredoc is ideal for including long blocks of HTML code.</p>

We can also store it within a variable:

$codigo = EOT
<p>This text can have "quotes" inside without needing to escape them.</p>
<p>It also processes (replaces by its value) the $ variables within the code </p>
echo $codigo;

Although the identifier characters can be formed with any alphanumeric combination, by convention, the EOT characters are usually used (end of text or end of text).

The important thing is that those same characters are not included in the text, since if that happens, the PHP interpreter software will consider that the block ends there, and will cause an error message when not knowing what to do with the rest of the text.

Another consideration is that these indicator characters of the start and end of the block, must be included at the beginning of the line (without leaving a single blank space, or tabulations, code indentations, or comments), and should not contain a line break).

For this reason, if we write this code in a text editor under Windows, we may have problems when including a line break (Enter) at the end of the line, since the line break in Windows is not the new line character \ n that this construction hopes to find, but it is \r \n

We can do the test and, if we see that at the end of the order does not interrupt the writing of the text, we should verify that the final line is completely empty of any strange character. Once confirmed that our editor is generating the jump correctly, we should upload to the server by FTP the file used in Ascii mode (and not in binary), and disable the "auto-detect" option that most FTP programs contain.

In addition to the ease of reading and sticking in place of long blocks of code, this syntax increases the speed of interpretation of the code in the server if we compare it with the use of several echo or print followed.


ChatGPT Free
Ask questions on any topic


For homework, research, thesis, books, magazines, blogs or academic articles

APA Format Reference:

Delgado, Hugo. (2019).
Ways for PHP to write in HTML.
Retrieved Jan 12, 2024, from


Share it with your friends in Social Networks!

Professor at the University of Guadalajara

Hugo Delgado Desarrollador y Diseñador Web en Puerto Vallarta

Professional in Web Development and SEO Positioning for more than 10 continuous years.
We have more than 200 certificates and recognitions in the Academic and Professional trajectory, including diploma certificates certified by Google.



Not finding what you need?

Use our internal search to discover more information
Related content:

Would you like to learn more about Web Design?

Meet all the courses and tutorials that we have for you completely free
Learn Web Design

Leave your Comment


Your business can also appear here. More information

Your browser has blocked advertising.
Please 🙏 allow to visualize the announcements to be able to access, thank you.