Monday, September 27, 2010

Combining HTML and PHP

The script in Listing 3.1 is pure PHP. You can incorporate this into an HTML
document simply by adding HTML outside the PHP start and end tags, as shown in

1: <html>
2: <head>
3: <title>Listing 3.2 A PHP script including HTML</title>
4: </head>
5: <body>
6: <b>
7: <?php
8: print "hello world";
9: ?>
10: </b>
11: </body>
12: </html>
As you can see, incorporating HTML into a PHP document is simply a matter of
typing in the code. The PHP interpreter ignores everything outside PHP open and
close tags. If you were to view Listing 3.2 with a browser, as shown in Figure 3.4,
you would see the string "hello world" in bold. If you were to view the document
source, as shown in Figure 3.5, the listing would look exactly like a normal HTML
document.
You can include as many blocks of PHP code as you need in a single document,
interspersing them with HTML as required. Although you can have multiple blocks of
code in a single document, they combine to form a single script. Anything defined in
the first block (variables, functions, or classes, for example) usually will be available
to subsequent blocks.

No comments:

Post a Comment