In this PHP syntax guide, you will learn how to write PHP tags. PHP can be embedded in the document with HTML markups.

Before getting started, we should know that the file of the PHP document is ending with “.PHP” as an extension and how the PHP interprets the source code.

Actually, there are many types of PHP tags.

  • Opening and Closing PHP Syntax.
  • PHP Short Echo.
  • PHP Short Tags.
  • And some other tags already working before PHP 7.0.

Let’s explain each one in-depth.

Escaping To PHP: Opening and Closing PHP Syntax Guide

The opening and closing PHP syntax would start with this tag – “<?” And besides it, we have to define the PHP word – “php”.

So, the below code is accepted PHP tag by only opening tags.

<?php 
   echo "Hello CodedTag Students";

If you saw the previous code, it has no closing PHP tag, but it should have a semicolon at the end. And that prevents syntax errors.

So all PHP statements should have a semicolon at the end of the line.

In the next example, you will see the closing PHP tags, which means the PHP document should end with – “?>”.

<?php 
   echo "Hello CodedTag Students";
?>

And here we have an important note, the last line of the PHP document can have no semicolon. But it should have a closing tag. Let’s see that in the following example.

<?php 
   echo "line 1" . "<br />";
   echo "line 2" . "<br />";
   echo "line 3" . "<br />";
   echo "line 4" . "<br />"
?>

PHP Shorthand Echo

The shorthand echo would look like the below PHP syntax.

<?= "Hello World" ?>

The PHP shorthand is a normal tag, like in the opening and closing syntax. So there is no option to disable or enable it.

PHP Short Tags

The short tag is similar to the below PHP syntax.

<? /* Code Here */ ?>

To use the PHP short tags, you have to enable the short_open_tag option in the PHP ini file. To access the PHP ini file, you have to know what is the name of PHP server you are using.

I am using the XAMPP server in my operating system. So the root of the PHP ini file is located inside the following path.

C:\xampp\php\php.ini

In the next section, I will discuss more about the deleted PHP tags.

Removed PHP Tags

In the following list, you will some old syntaxes, but they aren’t work after PHP 7.0.

1- HTML Script Tags

This syntax is removed in PHP7.0. The syntax like the below

<script language="php">
  echo "This is a script syntax";
</script>

2- ASP Tags

This syntax is also deprecated from all versions after PHP.7.0. The syntax of ASP tags should be enabled from the PHP ini file using this option – “asp_tags”.

<%  echo "This is a ASP syntax"; %>

Anyway, let’s see some examples for PHP tags

Examples for PHP Syntax Guide and Tags

The PHP short tags.

<? 
   function codedtag_do_action() {
      return "PHP short tags";
   }

   echo "Hello world, " . codedtag_do_action();
?>

PHP shorthand echo.

<?="This is a PHP string" ?>

PHP normal tags ( Opening and Closing Syntax ).

<?="This is a PHP string" ?>