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 the case of PHP, a pair of <?php and ?> tags are used to mark where the PHP code is in a document. Everything that is not enclosed within the delimiters will be treated as text and output as-it-is.

 

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

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TABLE OF CONTENTS

Download & Notes Delimiters Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

PHP DELIMITERS

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

 

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 we learn as a beginner. Start a PHP script with <?php and end it with  ?>. But here comes the common confusion and “deadly assumption” that some beginners have – “Every PHP file must start with <?php and end with ?>. There cannot be more than one pair of PHP tags”. Well, that’s wrong.

 

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 more accurately delimiters. Whatever enclosed within the PHP tags will be processed as PHP code, whatever outside the tags will be output as-it-is. Easy?

 

 

3) MULTIPLE PHP TAGS

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

Next, it’s time to smash that “only one pair of PHP tags are allowed” myth – There’s no such limit. I am just going to repeat – Whatever is enclosed in <?php ?> will be processed as PHP code, whatever 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 to output below.

 

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.

 

 

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.

 

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 interpreted by browsers in the first place.

 

 

USEFUL BITS & LINKS

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 it anymore.

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

 

INFOGRAPHIC CHEAT SHEET

PHP Delimiters (Click To Enlarge)

 

REFERENCES & LINKS

 

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.

Leave a Comment

Your email address will not be published. Required fields are marked *