[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

php main
1. what is php
2. http basics
3. php basics
* a. embedding php
b. including php
c. php syntax
d. php data types
e. php variables
4. php expressions
5. php client side
6. php flow control
7. php manual


print version

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.

Directions for surfing with audio.

Embedding PHP

These tutorials are about PHP and its use for server-side Web programming.

Embedding PHP

PHP 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 .php suffix. The server, seeing the suffix, assumes that there is PHP code embedded in that document that needs to be processed and passes it to a PHP parser for processing before returning the document to the requesting client. (If there is no PHP code, nothing happens, and the document gets sent on as is.) Thus, PHP can modify, overwrite, or even replace the document, depending on circumstance.

As a for instance, I could have a file called myfile.php that would be a script that evaluates what type of browser the client is using and uses that information to substitute a document appropriate to that browser for itself. This would allow me to have one address for the page, even though there may be many different pages to address issues of browser incompatibilities.

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 Directive

For 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 (php.ini) has a variable called short_open_tag. By default, it is set to On.

When short_open_tag is turned on, you can abbreviate the opening tag in a PHP statement to <?. This is to allow for backwards compatability with some older SGML applications. However, using this shortcut is strongly discouraged unless you have a really good reason to use it. Saving yourself a few characters of code is not a good reason.

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 short_open_tag is set to Off. If you are planning on porting code to unknown platforms, then you definitely do not want to use short tags.

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 <script> tags.

ASP tags are not enabled by default. To turn them on you need to find the asp_tags flag in the php.ini file, or, if working from the source, build PHP with the --enable-asp-tags option.

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> tags. This is very useful if you want to ensure that you are writing scrictly formatted HTML code. It does, however, sharply delimit what you can do with PHP, since that tags can only exists in places where <script> tags can legally exist inside an HTML document. Thus it cannot be used to set attribute values or set the contents of tags that cannot have scripts nested inside them. Of course, you can resolve this by just using PHP to write out larger blocks of the document, including tags, but this is not necessarily an optimal solution.

<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 echo command, which writes information back to the document to be sent to the client. If you are only using a given PHP statement to echo content, you can use an abbreviated tag to do this. This works for both short tags and ASP tags.

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.

[top]