How to Identify PHP Protocol
When a Web server runs a PHP script, or serves an HTML Web page with snippets of PHP code interspersed throughout the page, the server must be able to identify the parts of the script or page that use the PHP protocol. When the server encounters the tags that identify a piece of PHP code, it will parse the code and output the results to the browser or system memory, as instructed by the script.
Instructions
-
-
1
Identify the PHP protocol within your script or Web page by enclosing the code in the proper opening and closing tags.
The standard, most widely used opening tag consists of a "less than" sign, a question mark and the letters "php." The corresponding closing tag consists of a question mark followed by a "greater than" sign. Thus, a simple echo statement contained in standard opening and closing PHP tags would look like this:
<?php
echo "Hello, World!";
?> -
2
When authoring dynamic Web pages with server-side code interspersed with HTML, you can alternately enclose your PHP code within a <script> element, in much the same way that you would for a piece of JavaScript code. Set the "language" attribute of your <script> element to "php." It is not necessary to specify the "type" attribute. This method involves several more keystrokes than the standard tag, but may be necessary to avoid conflicts with some Web page creation software:
<script language="php">
echo "I hope you are well.";
</script> -
-
3
In some server environments, the php.ini file may be configured to allow short tags:
<?
echo "Good morning.";
?>
In special cases, the server may be configured to parse code as PHP when it is demarcated by ASP-style tags:<%
echo "This block of PHP code is contained within ASP-style tags.";
%>However, since short tags and ASP-style tags are not supported by all servers, the standard tags are recommended to ensure maximum portability. Additionally, PHP short tags look just like the tags used for XML declaration, which can lead to incompatibility, parse errors and general confusion.
-
4
When you know that your script will only ever be used as an included file within a calling script, you may omit the closing PHP tag. However, be judicious in your omission of closing tags, because if you have HTML and PHP on the same page and omit the closing tag, this will result in a parse error.
In general, it is best practice to always identify PHP code by wrapping it in the <?php ?> opening and closing tags.
-
1
Tips & Warnings
Global settings for how PHP is parsed can be changed by modifying php.ini; however, in many shared hosting environments, system administrators do not allow access to this file.
If your server has PHP short tags enabled, you will need to convert any XML declarations in your Web pages into PHP echo statements to avoid parse errors. For example:
<?php echo '<?xml version="1.0" encoding="utf-8" ?>'; ?>