Opening And Closing PHP Tags

The PHP syntax is a set of rules that define how a program should be written. PHP is, in that sense, no different from any other language. If I wrote this sentence backward and upside down, it would not be following the rules, and it is doubtful that anyone would understand it. The PHP interpreter expects certain rules to be followed, and will spit errors at you if they are not followed.

All code written in PHP must be identified as PHP code. A set of tags are used to mark the beginning and end of a block of code, in between which any amount of code can be written.

The standard opening tag is:

<?php

The standard closing tag is:

?>

These tags can be used to jump in and out of "PHP mode" any number of times in a PHP file, including a PHP file containing HTML elements.

<?php /* Code Can Go Here */ ?>
<html>
<head>
  <?php /* Code Can Go Here */ ?>
</head>
<body>
  <?php /* Code Can Go Here */ ?>
</body>
</html>
<?php /* Code Can Go Here */ ?>

One other set of tags is readily available, but not practical when there is the shorter alternative. This set is:

<script language="php"> </script>

Also, several sets of tags are available if PHP's configuration is set to allow their usage, which make them less portable and not desirable for general use:

<?   ?>
<?   ?>
<%   %>
<%=   %>

All examples in this tutorial will use <?php and ?>, as it is doubtful that you will ever need an alternative.