Getting Started with PHP


You can embed your PHP scripts inside HTML pages if you enclose the scripts in special markup. In code, you should enclose your PHP scripts (which are stored in files with the extension .php) inside the markup <?php and ?> like this:

 <?php       .       .       .      PHP goes here       .       .       . ?>

However, in practice, you can use <? and ?>, and the server will still understand:

 <?    .    .    .   PHP goes here    .    .    . ?>

A PHP-enabled server will execute the PHP code inside the <?...?> markup. Here’s an example that runs the built-in PHP function phpinfo, phpinfo.php, which creates an HTML table that tells you about your PHP installation.

Note 

Note in particular how the HTML and PHP are interspersed in this example, phpinfo.php. Also note that as in JavaScript, you end each PHP statement with a semicolon (;).

 <html>     <head>         <title>            A first PHP example         </title>     </head>     <body>         <h1>             A first PHP example         </h1>         <?             phpinfo();         ?>     </body> </html>

What does this look like in a browser? You can see the results in Figure 12.1. The details will be different depending on your PHP installation, but the idea is the same: the phpinfo function displays the table you see in the figure.

image from book
Figure 12.1: The phpinfo.php application

That gets the ball rolling with PHP. How about sending some text of your own choosing back to the browser?

Sending text back to the browser

To send text back to the browser, use the PHP echo statement, which involves passing to the echo statement the text you want to send back to the browser.

Here’s an example, echo.php. Start by including a PHP section among the HTML of a Web page like this:

 <html>     <head>         <title>             Using the echo statement         </title>     </head>     <body>         <h1>             Using the echo statement         </h1>                  <?         .         .         .         ?>     </body> </html>

Use the PHP echo statement to display some custom text:

 <html>     <head>         <title>             Using the echo statement         </title>     </head>     <body>         <h1>            Using the echo statement         </h1>         <?             echo "Greetings from PHP.";         ?>     </body> </html>

You can see echo.php at work in Figure 12.2, which shows the echo statement sending text back to the browser.

image from book
Figure 12.2: Echoing text back to the browser

That sends text to the browser; how about sending XML data back to the browser?

Sending XML back to the browser

The echo statement sends text back to the browser, but sometimes in Ajax you don’t just want to send text-you want to send XML data. After all, Ajax means Asynchronous JavaScript and XML. How can you send XML back to the browser?

This example sends the following XML data back to the browser:

 <?xml version="1.0" ?> <document>   <data>Here</data>   <data>is</data>     <data>some</data>   <data>XML.</data> </document>

To be sure that the text sent back to the browser is interpreted as XML by the browser, you should use the PHP header function to set the HTTP Content-Type header to text/xml.

Here’s an example, xml.php. Start off with the PHP <?...?> markup and the header function:

 <? header('Content-Type: text/xml');         .         .         . ?>

then echo the XML declaration, which is needed at the start of every XML document:

 <? header('Content-Type: text/xml'); echo '<?xml version="1.0" ?>';         .         .         . ?>

and then start the document element, which is just <document> here, by simply echoing <document> to the browser:

 <? header('Content-Type: text/xml'); echo '<?xml version="1.0" ?>'; echo '<document>';         .         .         . ?>

Next, echo the <data> elements to the browser:

 <? header('Content-Type: text/xml'); echo '<?xml version="1.0" ?>'; echo '<document>'; echo '<data>'; echo 'Here'; echo '</data>'; echo '<data>'; echo 'is'; echo '</data>'; echo '<data>'; echo 'some'; echo '</data>'; echo '<data>'; echo 'XML.'; echo '</data>';         .         .         . ?>

Finally, end the example by closing the <document> element:

 <? header('Content-Type: text/xml'); echo '<?xml version="1.0" ?>'; echo '<document>'; echo '<data>'; echo 'Here'; echo '</data>'; echo '<data>'; echo 'is'; echo '</data>'; echo '<data>'; echo 'some'; echo '</data>'; echo '<data>'; echo 'XML.'; echo '</data>'; echo '</document>'; ?>

So what does this look like in a browser? You can see this example, xml.php, in Figure 12.3, where the XML it sends back to the browser is displayed. (Note that Internet Explorer is treating the output of xml.php as true XML, as it should.)

image from book
Figure 12.3: Echoing XML back to the browser

This gives you basics of echoing text and XML back to the browser using PHP. Now it’s time to take a look at the kind of PHP programming you can do. We’ll start with something simple: commenting your code.



Ajax Bible
Ajax Bible
ISBN: 0470102632
EAN: 2147483647
Year: 2004
Pages: 169

Similar book on Amazon
HTML, XHTML, and CSS Bible
HTML, XHTML, and CSS Bible
JavaScript Bible
JavaScript Bible
JavaScript Bible
JavaScript Bible
PHP and MySQL Web Development (4th Edition)
PHP and MySQL Web Development (4th Edition)

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net