PHP Delimiters (Very Simple Examples)

Welcome to a quick tutorial on PHP delimiters, and you may have stumbled on the term “PHP delimiter” from somewhere. Just what does it mean and what does it do?

Delimiters are used to set boundaries, denote where the start and end are. In PHP, a pair of <?php and ?> tags are used as delimiters to mark where the PHP code is in a document; Whatever is not enclosed within these delimiters will be output as it is.

Yes, this is the very first thing beginners learn – Start a PHP script with <?php and end it with ?>. But there’s quite a bit more to that, read on for more!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP DELIMITERS

All right, let us now get into the basics and examples of PHP delimiters.

 

EXAMPLE 1) BASIC PHP TAGS

1-basic.php
<?php
$string = "Hello World!";
echo $string;
?>

Captain Obvious to the rescue! You guys should already know this, the very first thing every beginner learns – Start a PHP script with <?php and end it with  ?>. But here is a common confusion and “deadly assumption” that some have – Every PHP script must start with <?php and end with ?>, there cannot be more than one pair of PHP tag. Well, that’s plain wrong.

 

EXAMPLE 2) PHP DELIMITERS

2-delimiters.php
Foo
<?php
$string = " Hello World ";
echo $string;
?>
Bar

Ever wonder what happens when we put text outside the <?php and ?> tags? Go ahead, run this snippet in the browser or command line and see for yourself.

Yes, it simply outputs Foo Hello World Bar. As in the introduction, <?php and ?> are delimiters. Whatever is enclosed within the PHP tags will be processed as PHP code, whatever is outside the tags will be output as it is. Easy?

 

 

EXAMPLE 3) MULTIPLE PHP TAGS

3-multiple.php
Foo
<?php
$string = " Hello World ";
?>
Bar
<?php
echo $string;

Next, it’s time to completely smash the myths.

  • False – PHP scripts MUST start with <?php and end with ?>.
  • False – PHP scripts can only have one pair of <?php and ?>.

I am going to repeat it. Whatever is enclosed within <?php ?> will be processed as PHP code, and whatever is outside will output as it is. This snippet will output Foo Bar Hello World.

P.S. It is all right to omit the closing ?> for the last <?php, when you have nothing more to process or output below.

 

EXAMPLE 4) DELIMITERS IN PHP-HTML

4-html.php
<?php
$title = "Test Page";
$content = "<strong>Hello World!</strong>";
?>
<!DOCTYPE html>
<html>
  <head>
    <title><?php echo $title; ?></title>
  </head>
  <body><?php echo $content; ?></body>
</html>

Let us now get into a more practical example in this one – Weaving between HTML and PHP code.

P.S. HTML is literally plain text. There’s nothing “illegal” when we define HTML in a PHP string. There’s also nothing wrong when we output HTML code as it is.

 

 

EXAMPLE 5) SHORT ECHO TAGS

5-short.php
<?php
$title = "Test Page";
$content = "<strong>Hello World!</strong>";
?>
<!DOCTYPE html>
<html>
  <head>
    <title><?=$title?></title>
  </head>
  <body><?=$content?></body>
</html>

Now, the above <?php echo $VARIABLE; ?> is quite a hassle, and this is a convenience that PHP provides – The short echo tags. Simply use <?=$VARIABLE?> to quickly output a PHP variable.

 

EXAMPLE 6) DELIMITERS IN PHP-JAVASCRIPT

6-js.php
<?php
$title = "Test Page";
$content = "<strong>Hello World!</strong>";
$number = 123456;
?>
<!DOCTYPE html>
<html>
  <head>
    <title><?=$title?></title>
    <script>
    var num = <?=$number?>;
    alert(num);
    </script>
  </head>
  <body><?=$content?></body>
</html>

Lastly, for you guys who are wondering – Yes, we can also weave between PHP and Javascript. There really isn’t any limitation to what PHP can weave into… HTML, Javascript, CSS, JSON, XML are literally just text and are interpreted by browsers in the first place.

 

 

EXTRAS

That’s all for this guide, and here is a small section on some extras and links that may be useful to you.

 

EXTRA) DEPRECATED SHORT OPEN TAGS

If you dig around the Internet, you may find some older PHP scripts and tutorials using <? instead of the “full” <?php. These are called short open tags. But because they clash with the XML tags <?xml, they have been deprecated in PHP 7. Just don’t use it anymore.

P.S. Short echo tags <?=$VAR?> are still accepted, but short tags <? echo $content ?> are totally phased out in PHP 8.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

2 thoughts on “PHP Delimiters (Very Simple Examples)”

  1. David in Mississippi

    Thank you for these absolutely EXCELLENT tutorials! I wish I had found them 15 years ago, when I was first digging deep into PHP. I know, they weren’t around then, but I can wish.

    In the “Extra” above, you said PHP short tags have been deprecated in PHP 7, and “Just don’t it (sic) anymore.” Does this mean we CANNOT use the short tags in HTML any more? I guess I’ll have to test it to find out. Or maybe when writing new code, we should always avoid the short open tags so as to maintain compatibility with PHP 8.0 (when it’s released)?

    Thanks again for some GREAT tutorials.

    1. Short echo tags are still accepted – <?=$VARIABLE?>

      Short open tags are totally phased out in PHP 8. <? $VARIABLE = "xyz"; ?> will not work.

Comments are closed.