Understanding PHP Tags & Code Blocks

Published: November 29th, 2008 by:

PHP was originally designed as a tool to make a website's HTML more dynamic, so it's no wonder that it mixes well with existing HTML in your website. There are several options for defining your PHP blocks, and we will cover them all here.


Most Common Tag and How It’s Used

In a previous PHP article, I had a sample script. Here it is again:

<?php
    /* output our intro message */
    echo 'It works!';
?>

This is the most common PHP tag, and the most supported. Generally, this will be the only tag you need to worry about. Here would be another example in the context of a typical HTML page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Sample PHP Script w/ HTML</title>
<meta name="generator" content="Bluefish 1.0.7">
<meta name="author" content="Andrew Wells">
<meta name="date" content="2008-11-29T13:23:32-0600">
<meta name="copyright" content="2008 PHPStarter">
<meta name="keywords" content="A sample script showing how to mix PHP & HTML.">
<meta name="description" content="php, html, phpstarter">
<meta name="ROBOTS" content="INDEX, FOLLOW">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8">
</head>
<body>

<?php
    /* output our intro message */
    echo 'It works!';
?>

</body>
</html>

PHP only parses the code in the PHP tags, so the other HTML is left alone, and we result in an HTML page like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Sample PHP Script w/ HTML</title>
<meta name="generator" content="Bluefish 1.0.7">
<meta name="author" content="Andrew Wells">
<meta name="date" content="2008-11-29T15:55:40-0600">
<meta name="copyright" content="2008 PHPStarter">
<meta name="keywords" content="A sample script showing how to mix PHP & HTML.">
<meta name="description" content="php, html, phpstarter">
<meta name="ROBOTS" content="INDEX, FOLLOW">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8">
</head>
<body>

It works!
</body>

Variables with Multiple Blocks & Short Tags

PHP function and variable declarations do stay intact between blocks of code, so this example would output ‘Hello, Andrew!’.

<?php
    $username = 'Andrew';
?>
<p>Hello, <?php echo $username; ?>!</p>

This can get pretty confusing with several blocks of PHP mixed in with HTML. To help keep code neater, there is a shorthand way to output variables in HTML:

<?php
    $username = 'Andrew';
?>
<p>Hello, <?=$username?>!</p>

In the above example, the code to output the username is quite condensed using “short tags”. Although this method is convenient, it is generally not recommended if you plan to redistribute your code to other servers, as some might have this option disabled.

ASP-Style Tags

I don’t know why PHP provides this as an option. I strongly discourage its use and I have it disabled on all of my servers. Nevertheless, it’s a working option for you to use.

<%
    /* output our intro message */
    echo 'It works!';
%>

One Last Alternate PHP Opening/Closing Tag

Many developers don’t know this, but you can also use the HTML script tag to define your PHP code. This is the non-standard way to use this tag because this tag is normally used for client-side scripting, or scripts that are ran by the client’s browser. PHP is a server-side language, or a scripting language that is parsed by the server, and the clients are only served the results. I’m guessing that this method was implemented because WYSIWYG editors like MS FrontPage gets confused when it sees regular PHP tags, but recognize this script tag. Here is an example:

<script language="php">
    /* output our intro message */
    echo 'It works!';
</script>

So there you have it. Inserting PHP code into your HTML pages is now a no-brainer. 🙂


Leave a Reply





Wordpress doesn't like it when you post PHP code. Go save your code at pastebin, and post the link here.

About the Author

Andrew has been coding PHP applications since 2006, and has plenty of experience with PHP, MySQL, and Apache. He prefers Ubuntu Linux on his desktop and has plenty of experience at managing CentOS web servers. He is the owner of Wells IT Solutions LLC, and develops PHP applications full time for anyone that needs it as well as does desktop computer support locally in the local area. He spends most of his free time exploring new programming concepts and posting on The Webmaster Forums.