Monday, September 27, 2010

Beginning and Ending a Block of PHP Statements

When writing PHP, you need to inform the interpreter that you want it to execute
your commands. If you don't do this, the code you write will be mistaken for HTML
and will be output to the browser. Table 3.1 shows the four ways of enclosing PHP
code.

PHP Start and End Tags

Tag Style             Start Tag                                  End Tag
Standard tags        <?php                                        ?>
Short tags              <?                                             ?>
ASP tags                <%                                            %>
Script tags    <SCRIPTLANGUAGE="php">          </script>


only the standard and the script tags can be guaranteed to
work on any configuration. The short and ASP style tags must be explicitly enabled
in your php.ini. You examined the php.ini file in Hour 2.
To activate recognition for short tags, you must make sure that the short_open_tag
switch is set to "On" in php.ini:
short_open_tag = On;
Short tags are enabled by default, so you would only need to edit php.ini if you want
to disable these.
To activate recognition for the ASP style tags, you must enable the asp_tags
setting:
asp_tags = On;
After you have edited php.ini, you should be able to choose from any of the four
styles for use in your scripts. This is largely a matter of preference, although if you
intend to work with XML, you should disable the short tags (<? ?>) and work with
the standard tags (<?php ?>).
Let's run through some of the ways in which you can legally write the code in Listing
3.1. You could use any of the four PHP start and end tags that you have seen:
<?
print("Hello Web!");
?>
<?php
print("Hello Web!");
?>
<%
print("Hello Web!");
%>
<SCRIPT LANGUAGE="php">
print("Hello Web!");
</SCRIPT>
Single lines of code in PHP also can be presented on the same line as the PHP start
and end tags: <? print("Hello Web!"); ?>
Now that you know how to define a block of PHP code, take a closer look at the code
in Listing 3.1 itself.

No comments:

Post a Comment