PHP Delimiters

by Vincy. Last modified on August 25th, 2022.

PHP delimiters are nothing but open-close tags to enclose PHP scripts. PHP code can be parsed if and only if it is enclosed with these delimiters.

There are various sets of delimiters to recognize PHP code. These are as listed below.

  1. <?php … ?> – It is most probably used delimiters and also preferable, since, PHP code enclosed with in <?php .. ?> could be recognized with other PHP servers independent of the tag related configuration directives.
  2. <script language=”php”></script> – This will also work anywhere, like <?php … ?>.
  3. <? … ?> – This is called as PHP short tags which will work based on the value set with the short_open_tag directive of the PHP configuration file.
  4. <?=..;?> – This is also a short form of denoting PHP code, but is used as an alternative representation of PHP print statement.
  5. <% … %> or <%=;%>   – This is the delimiters for writing Active Server Pages(ASP) code. But it will be used for PHP code if and only if the asp_tags directive is enabled.

Let us look into the php.ini directive settings that affect the PHP delimiters to be used.

short_open_tag in PHP Language Configuration

This directive is used to specify whether the short form for PHP delimiters, that is, <?…?>, is allowed or not. This PHP language option will be set with ON/OFF values and the default value is ON. When it is set as ON, then it is allowed to use the short form (<?…?>) to write PHP code which will be recognized by the PHP parser. If it is set to OFF, then the PHP code can not be parsed.

This method of writing a PHP program by enclosing it with <? and ?> is not recommended programming practice. Because, if we launch our code that contains <? and ?> open, close tags, into another PHP server, where the short_open_tag directive is not enabled, then the code will not work on that server.

Though PHP short tags are not portable among different servers, still they are allowed for providing backward compatibility.

This form is used to print something to the browser. For example, a PHP echo statement can be written as follows.

<?php
$statement = "PHP Open and Close Tags";
?>

<?=$statement;?>

From the above list of ways to represent PHP tags, the fourth possible form, that is, <?=;>was also included with this constraint depending on the value of the short_open_tag directive as of earlier PHP versions. But, as of PHP version 5.4, this form will also be portable like the first two delimiters, for launching our code having this notation, into another server.

asp_tags Option

This option is used to allow to recognize of PHP code enclosed with the open-close tags used in Active Server Pages (ASP) scripting, that is like, <%…%>.

We can search for this tag in the php.ini file. If it is enabled by having the value ON, then the PHP scripts inside the open-close tags, <%,%> will be executed, and also, the alternative ASP print statement representation, like, <%=;%> also be allowed. Otherwise, the script including asp tags will be displayed in the browser.

As like PHP short tags <? and ?>, asp style tags also depend on PHP configuration settings. So, this form of tags is also not preferable to develop PHP projects to be launched on another server, because of the lack of portability.

Note:

  • From the above list of PHP delimiters, short tags and the ASP style delimiters are not recommended as a method to represent PHP code, since they are depending on configuration settings.
  • Even though, <script language=”php”></script> is portable, it is also not widely used for avoiding confusion in separating PHP code embedding it while embedding it HTML or XML content.
Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

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

↑ Back to Top

Share this page