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?
?>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!
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
All right, let us now get into the basics and examples of PHP delimiters.
EXAMPLE 1) BASIC PHP TAGS
<?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
Foo <?php $string = " Hello World "; echo $string; ?> Bar
Ever wonder what happens when we put text outside the
?> 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,
?> 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
Foo <?php $string = " Hello World "; ?> Bar <?php echo $string;
Next, it’s time to completely smash the myths.
- False – PHP scripts MUST start with
<?phpand end with
- False – PHP scripts can only have one pair of
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
<?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
<?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.
<?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>
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
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.
EXAMPLE CODE DOWNLOAD
Click here for the 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.
EXTRA 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 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
- PHP Tags – php.net
- Short Open Tags – php.net
- Escape From HTML – php.net
- PHP Syntax and semantics – Wikipedia
INFOGRAPHIC CHEAT SHEET
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!