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 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|
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
QUICK NOTESIf you spot a bug, feel free to comment below. I try to answer short 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.
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.
All right, let us now get into the basics and examples of PHP delimiters.
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 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
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 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
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
<?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
<?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>
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.
REFERENCES & LINKS
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!