05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

XML Style<br />

Because of the advent of the eXtensible Markup Language (XML) and the migration<br />

of HTML to an XML language (XHTML), the currently preferred technique for<br />

embedding <strong>PHP</strong> uses XML-compliant tags to denote <strong>PHP</strong> instructions.<br />

Coming up with tags to demark <strong>PHP</strong> commands in XML was easy, because XML<br />

allows the definition of new tags. To use this style, surround your <strong>PHP</strong> code with<br />

. Everything between these markers is interpreted as <strong>PHP</strong>, and everything<br />

outside the markers is not. Although it is not necessary to include spaces between the<br />

markers and the enclosed text, doing so improves readability. For example, to get<br />

<strong>PHP</strong> to print “Hello, world”, you can insert the following line in a web page:<br />

<br />

The trailing semicolon on the statement is optional, because the end of the block<br />

also forces the end of the expression. Embedded in a complete HTML file, this looks<br />

like:<br />

<br />

<br />

<br />

This is my first <strong>PHP</strong> program!<br />

<br />

<br />

<br />

Look, ma! It's my first <strong>PHP</strong> program:<br />

<br />

How cool is that?<br />

<br />

<br />

<br />

Of course, this isn’t very exciting—we could have done it without <strong>PHP</strong>. The real<br />

value of <strong>PHP</strong> comes when we put dynamic information from sources such as databases<br />

and form values into the web page. That’s for a later chapter, though. Let’s get<br />

back to our “Hello, world” example. When a user visits this page and views its<br />

source, it looks like this:<br />

<br />

<br />

<br />

This is my first <strong>PHP</strong> program!<br />

<br />

<br />

<br />

Look, ma! It's my first <strong>PHP</strong> program:<br />

Hello, world!<br />

How cool is that?<br />

<br />

<br />

<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Embedding <strong>PHP</strong> in Web Pages | 57

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!