PHP 04: PHP Tags

PHP codes must be enclosed by PHP tags. When PHP parser pareses a file , it looks for opening and closing PHP tags. It tells the parser when to start interpreting and when to stop. Reason for parsing in this manner is allowing to embed in different types of documents like HTML.

Example of PHP standard tag

Look at the code below

<?php
echo "Hello world";
?>

It is recommended to use PHP standard tag for compatibility an this tag can not be disabled by php.ini settings.

Example of PHP short tag

<?
echo "Hello world";
?>

To use PHP short tag short_open_tag must be set to On in php.ini settings. But use of of PHP short open tag is not recommended.

If the file contains only PHP code, you can omit the end tag to avoid error due to whitespace added after the end tag. See the code below.

<?php
echo "Hello world";
// code ends here with no PHP closing tag

Note

The ASP tags <%, %>, <%= and the script tag <script language="php"> are removed from PHP 7.

Related posts

Md. Monirul Alom

Md. Monirul Alom

I am a Full Stack Web developer. I love to code, travel, do some volunteer work. Whenever I get time I write for this blog