| [main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql] | |
Note that all external links will open up in a separate window. This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser. |
Embedding PHPThese tutorials are about PHP and its use for server-side Web programming. Embedding PHPPHP is designed to be a flexible language, therefore, there are many ways to implement it, depending on your needs. We are going to cover the standard method of including it first, then look at the other options. embedded language: a coding language that is embedded in the data file which it is acting upon Regardless of how you implement PHP, it is still an embedded language, which is to say the PHP code gets embedded directly into the data file it is supposed to act upon. Unlike some embedded scripting languages (JavaScript for instance), PHP can also be run as a script external to the data it is acting on, and can in fact pull data from multiple sources. However, it is technically a part of the document it produces as a result of the processing.
You invoke PHP by making a call to an HTTP server for a document that ends in a
As a for instance, I could have a file called On of the most common uses of PHP is to generate dynamic content that can adapt itself to different browsers. Another common use is assembling data from diverse data sources into a single document for return to the user. The XML Processing DirectiveFor those who aren't familiar with XML, here is the quick genealogy: SGML, or the Standard Generalized Markup Language, can be seen as the grandpappy of the family of markup languages that includes HTML and XML and which are used for marking up online content for display. HTML was derived from SGML to function as a markup language specifically for Web documents. Realizing HTML was a dead-end, a new Web language called XML was derived from SGML. XML is stricter than HTML but infinitely more flexible. The relationship between the two is close enough that an HTML document, for our purposes, can be treated as a poorly written XML document. The way in which you normally embed PHP in a document is through what is commonly called a PHP statement, but is actually an XML directive. PHP is designed to be compatible with many standards and commonly used scripting approaches, making it easier to code. Here we are going to code to standards. After we look at the standards-defined method, we will look at the other methods. An XML processing directive is a command embedded in an XML document for some application to process. All XML processing directives take the form of: <?appname information for application ?> Notice that the statement opens and closes with nested brackets and question marks, and that the name of the application being referenced must come right after the first question mark, with no space before it. The information for the application can be a short statement, or, in the case of PHP, can go on for pages and pages. The PHP statement normally uses this form of inclusion in HTML documents. It looks like this: <?php some php statements ?> Line breaks are allowed within the XML processing directive, so the following is also legal. <?php some php statements and some more statements ?> If you want to write standards compliant code, or embed PHP in XML documents, you need to use this format. If you are not working with XML or don't need to adhere to standards, there are other approaches. Each may be appropriate within certain server situations. Since PHP is processed by the server, it can literally go anywhere in an HMTL document in question. The code is processed before the Web browser sees the code, so the Web browser never sees the PHP. If working with XML, you need to make sure that any server-side XML processing is done after the PHP processing, or code the document with the knowledge that the PHP code cannot violate the syntax rules of the application you are working with. This means that XML documents containing PHP should still be well-formed and valid. Let's stick with HTML for now. Since you can put it anywhere, PHP can be used to write content, or tag attributes, or even tags themselves. Any of the following might be valid PHP / HTML code combinations.
<p><?php echo "Hello World!"; ?></p>
<?php echo "<h2>" . $item1 . "</h2>"; ?>
<table width="<?php echo $twidth; ?>">
<img src="linkimag.gif" height="10" width="100"
<?php if (!$strict) { echo border=\"0\"; } ?> \>
If you want to code to current standards, be aware that your HTML or XHMTL documents should still be well-formed in spite of the included PHP. This takes a little more advance planning, but is not an impossible task. The other ways of doing this are not guaranteed to work on every server. The Short Tag
The PHP configuration file (
When In use short tags look like this: <? some php statements ?> If you want to code to XML standards, short tags are a problem, since they will cause the PHP processor to try to treat all XML processing directives as PHP statements, whether they are or not. For instance, if you are writing XML documents, they should begin with an XML prologue that identified the document type. It takes the form of: <?xml version="1.0" encoding="UTF-8" ?> If short tags are turned in, then the PHP processor will try to process this and generate a string of errors. You end up having to write it like this:
<?php
if ($stricttest) {
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"\x3f>";
echo "\n";
}
?>
Which can cause its own problems with any XML applications trying to process the document.
If you want to work with XML, make sure that Dealing with Old Editors
Realizing that some old code editors might have problems with PHP, the authors of PHP included two additional ways to include PHP code in a document. One mimicks ASP tags. The other uses standard HTML
ASP tags are not enabled by default. To turn them on you need to find the It is not necessary for the server to actually process ASP tags as PHP tags, since the point of the compatibility with ASP tags is to allows users to work with editors that understand ASP tags but not PHP tags. When you are done with the document, you can just do a global search and replace to replace all ASP tags with PHP tags before moving the documents to production. ASP tags take the following form: <% code goes here %> Like PHP tags, ASP tags can go anywhere in the document.
For editors that don't understand anything that isn't HTML, you can also use <script language="php"> code goes here </script> These two approaches are disabled by default in PHP. In order to use them on the server, you have to turn them on. Echoing Content
Perhaps the most common use of PHP is to generate content, and the most common means of doing so is the It works by appending an equal sign immediately after the opening tag. <?= "Rasputin" ?> // is the same as <?php echo "Rasputin" ?> This approach does tend to reduce readability and cannot be used with the XML compliant version of the opening tag, so it is not necessarily recommended.
These pages can be found at:
[http://academ.hvcc.edu/~kantopet/]
|