PHP Tags

PHP Tags

Every line of PHP code is written inside the PHP tags: <?php //PHP parsable code goes here ?>. If your server (either local or remote) support PHP or properly installed then the PHP interpreter always finds the “<?php” starting tag. When the interpreter finds the tag, it starts parsing the PHP code written inside it and the process ends when the interpreter finds the closing PHP tags: ?>.

Example:

<?php
echo "Hello, progracoding!";
// This is a comment
?>

PHP code can be embedded directly into an HTML page. You don’t need additional configuration to work PHP in your HTML page. But one thing you should keep in your mind that, pages that use PHP code must have the file extension *.php instead of *.html. Otherwise, the PHP code can not be parsed by the PHP interpreter.

But, you can also execute PHP code with a .html extension based HTML page. But to do that you have to configure your server configuration file in order to create the environment for executing PHP in .html page. To do that Check out:

  • How to run PHP code in .html page with Apache Server
  • How to Run PHP code in .html page with Nginx Server

Additional PHP tags to use

Everything outside of PHP tag <?php  ?> are ignored by PHP parser. But PHP supports several other tags. Some of them has been discontinued and disabled by the PHP developer team, and some of them still can be used. But to use them you have to configure first in the php.ini configuration file. See below for more information.

1. <? … ?> Short open tag [Still in use but not recommended]

You can also use PHP short ta to write your PHP code inside an HTML or PHP file.

PHP Short Tag syntax <? //php code goes here ?>

By default, the short open tag is disabled in the php.ini configuration file. If you want to enable this feature, follow the steps below:

  • Go to your apache installation directory and find out the php.ini configuration file.
  • Open the php.ini file with a text editor (e:g: Notepad, gedit, Notepad++, etc)
  • Search for short_open_tag, Now change the value of short_open_tag=Off to short_open_tag=On
  • Now save and close the file. Then restart the apache server.

Short open tag example:

<?
echo "Hello, progracoding!";
// This is a comment
?>
Warning: PHP short open tag is not recommended as of PHP 7.x.x. Because It can create conflict with other domain (e:g: <?xml //xml code ?> ). Means, when you will use XML code along with your PHP code then in case of short open tag, the PHP interpreter may think XML tag as the PHP tag.

2. Other discontinued tags

Two other discontinued tags are the script based tag (<script language=”php”></script>) and the ASP style PHP tag (<% //old php code %>). The both features were removed from PHP 7.0.0. So you can not use these features anymore if your server runs PHP 7.x.x.

PHP script without closing tag

When a PHP file contains only PHP script or when you write purely PHP file with a .php file extension, then it is recommended not to use the PHP closing tag. It helps to prevent unwanted white spacing and buffering even after the end of the code. Omitting the closing tag in a pure PHP file is a good practice for developers.

Example:

<?php
echo "Hello, progracoding!";
// This is a comment
//this is a pure file with no closing tags