How to Close PHP Tags

How to Close PHP Tags thumbnail
Vary HTML at runtime with the PHP scripting language.

A PHP file consists of PHP and HTML code. A Web server interprets PHP program logic and outputs HTML to a browser according to that logic. The Web server distinguishes between PHP and HTML code through the use of the PHP opening tag, "<?php," and the PHP closing tag, "?>." PHP closing tags should not be followed with any spaces or the "Enter" key, because those characters will be interpreted by the Web server as HTML characters. Some PHP functions require the function to be executed before any HTML output, and unintended extra characters can cause program warnings and errors.

Instructions

    • 1

      Close a PHP tag after a block of PHP code. Place the opening tag at the beginning of the block and the closing tag at the end of the block. For example, type:

      <?php

      $name = "Steve";

      echo "Your name is <strong>" . $name . "</strong>";

      ?>

    • 2

      Close a PHP tag in the middle of an HTML directive just as you would a block of code. For example, type:

      <a href="<?php echo $filename; ?>" target="_blank">Click Here</a>

    • 3

      Close a PHP tag to output HTML code and then open a new PHP tag and close that tag after the section completes. For example, type:

      <?php

      $age = 15;

      if ($age > 12 && $age < 20) { ?>

      Teenager!

      <?php } else { ?>

      Not a teenager!

      <?php } ?>

    • 4

      Omit the closing tag at the end of a file in a large, multi-file PHP application if you want to ensure that no extra spaces or carriage-return characters are inadvertently inserted as HTML code between PHP functions. Always include the opening PHP tag at the beginning of each PHP file as usual.

Tips & Warnings

  • Web servers universally support the use of the HTML script tag as a substitute for the PHP opening and closing tags, for example:

  • <script language="php">

  • echo "Running my PHP code...";

  • </script>

  • Some Web servers allow "<?" and "?>" or "<?%" and "%>" as PHP opening and closing tag pairs. These tags are not recommended because they are not supported by all Web servers.

Related Searches:

References

Resources

  • Photo Credit Stockbyte/Stockbyte/Getty Images

Comments

You May Also Like

  • How to Add Photos on Facebook

    Facebook is a great website to keep connected with your friends, family and co-workers. One of the great features of Facebook is...

  • How to Merge PHP with HTML

    PHP is a handy web programming language that you can use with HTML. You either can add a section of PHP code...

Related Ads

Featured