Open Source Web Development with LAMP Using Linux, Apache, MySQL, Perl, and PHP [Electronic resources]

James Lee, Brent Ware

نسخه متنی -صفحه : 136/ 127
نمايش فراداده

12.2 Embedding PHP Into HTML

There are several ways to embed PHP code into HTML documents. One way is to put the PHP code within the tags <? ... ?>:

<? echo "hello, world!"; ?> 

Most PHP programmers choose this syntax. If PHP is combined with XML, however, there are conflicts with this syntax. In XML the <? ... ?> construct is a special thing called a processing instruction. If PHP and XML are needed in the same file, one can use the alternative PHP tag <?php ... ?>:

<?php echo "hello, world!"; ?> 

Some HTML editors (such as FrontPage) don't like processing instructions, so another alternative is to use the <SCRIPT> tag. This is the syntax:

<SCRIPT LANGUAGE="php"> echo "hello, world!"; </SCRIPT> 

And last but not least, the final way is to use a style similar to one in Mason: the <% ... %> tag:

<% echo "hello, world!"; %> 

We are not using XML in these examples, so we don't need to use the <?php ... ?> syntax. Also, because our favorite HTML editors are vi and emacs, we don't need the <script> syntax (we don't use FrontPage or other HTML editorsif you do, plan accordingly). We use the first style in our examples.