Web Designing Education

Web Designing Education

Windows Shortcut Keys

100 Useful Windows Shortcut Keys

Here’s a list of about 100 shortcut keys for Windows. This list of shortcut keys applies for almost all Windows platform.
If you know any new shortcuts which I have not include in this list, kindly comment below. Thank you and enjoy!
Common Shortcut Keys:

  • CTRL+C (Copy)
  • CTRL+X (Cut)
  • CTRL+V (Paste)
  • CTRL+Z (Undo)
  • CTRL+A (Select)
  • DELETE (Delete)
  • SHIFT+DELETE (Delete the selected item permanently without placing the item in the Recycle Bin)
  • CTRL while dragging an item (Copy the selected item) CTRL+SHIFT while dragging an item (Create a shortcut to the selected item)
  • F2 key (Rename the selected item)
  • CTRL+RIGHT ARROW (Move the insertion point to the beginning of the next word)
  • CTRL+LEFT ARROW (Move the insertion point to the beginning of the previous word)
  • CTRL+DOWN ARROW (Move the insertion point to the beginning of the next paragraph)
  • CTRL+UP ARROW (Move the insertion point to the beginning of the previous paragraph)
  • CTRL+SHIFT with any of the arrow keys (Highlight a block of text)
  • SHIFT with any of the arrow keys (Select more than one item in a window or on the desktop, or select text in a document)
  • CTRL+A (Select all)
  • F3 key (Search for a file or a folder)
  • ALT+ENTER (View the properties for the selected item)
  • ALT+F4 (Close the active item, or quit the active program)
  • ALT+ENTER (Display the properties of the selected object)
  • ALT+SPACEBAR (Open the shortcut menu for the active window)
  • CTRL+F4 (Close the active document in programs that enable you to have multiple documents open simultaneously)
  • ALT+TAB (Switch between the open items)
  • ALT+ESC (Cycle through items in the order that they had been opened)
  • F6 key (Cycle through the screen elements in a window or on the desktop)
  • F4 key (Display the Address bar list in My Computer or Windows Explorer)
  • SHIFT+F10 (Display the shortcut menu for the selected item)
  • ALT+SPACEBAR (Display the System menu for the active window)
  • CTRL+ESC (Display the Start menu)
  • ALT+Underlined letter in a menu name (Display the corresponding menu)
    Underlined letter in a command name on an open menu (Perform the corresponding command)
  • F10 key (Activate the menu bar in the active program)
  • RIGHT ARROW (Open the next menu to the right, or open a submenu)
  • LEFT ARROW (Open the next menu to the left, or close a submenu)
  • F5 key (Update the active window)
  • BACKSPACE (View the folder one level up in My Computer or Windows Explorer)
  • ESC(Cancel the current task)
  • SHIFT when you insert a CD-ROM into the CD-ROM drive (Prevent the CD-ROM from automatically playing)

Dialog Box Keyboard Shortcuts:

  • CTRL+TAB (Move forward through the tabs)
  • CTRL+SHIFT+TAB (Move backward through the tabs)
  • TAB (Move forward through the options)
  • SHIFT+TAB (Move backward through the options)
  • ALT+Underlined letter (Perform the corresponding command or select the corresponding option)
  • ENTER (Perform the command for the active option or button)
  • SPACEBAR (Select or clear the check box if the active option is a check box)
  • Arrow keys (Select a button if the active option is a group of option buttons)
  • F1 key (Display Help)
  • F4 key (Display the items in the active list)
  • BACKSPACE (Open a folder one level up if a folder is selected in the Save As or Open dialog box)

Microsoft Natural Keyboard Shortcuts:

  • Windows Logo (Display or hide the Start menu)
  • Windows Logo+BREAK (Display the System Properties dialog box)
  • Windows Logo+D (Display the desktop)
  • Windows Logo+M (Minimize all of the windows)
  • Windows Logo+SHIFT+M (Restore the minimized windows)
  • Windows Logo+E (Open My Computer)
  • Windows Logo+F (Search for a file or a folder)
  • CTRL+Windows Logo+F (Search for computers)
  • Windows Logo+F1 (Display Windows Help)
  • Windows Logo+ L (Lock the keyboard)
  • Windows Logo+R (Open the Run dialog box)
  • Windows Logo+U (Open Utility Manager)
  • Accessibility Keyboard Shortcuts
  • Right SHIFT for eight seconds (Switch FilterKeys either on or off)
  • Left ALT+left SHIFT+PRINT SCREEN (Switch High Contrast either on or off)
  • Left ALT+left SHIFT+NUM LOCK (Switch the MouseKeys either on or off)
  • SHIFT five times (Switch the StickyKeys either on or off)
  • NUM LOCK for five seconds (Switch the ToggleKeys either on or off)
  • Windows Logo +U (Open Utility Manager)

Microsoft Natural Keyboard with IntelliType Software Installed

  • Windows Logo+L (Log off Windows)
  • Windows Logo+P (Starts Print Manager)
  • Windows Logo+C (Opens Control Panel)
  • Windows Logo+V (Starts Clipboard)
  • Windows Logo+K (Opens Keyboard Properties dialog box)
  • Windows Logo+I (Opens Mouse Properties dialog box)
  • Windows Logo+A (Starts Accessibility Options (if installed)
  • Windows Logo+SPACEBAR (Displays the list of Microsoft IntelliType shortcut keys)
  • Windows Logo+S (Toggles CAPS LOCK on and off)

Internet Explorer Shortcut Keys:

  • Alt+Left Arrow (Back a page)
  • Backspace (Back a page)
  • Alt+Right Arrow (Forward a page)
  • F5 (Refresh current page, frame or tab)
  • F11 (Display the current website in full screen mode. Pressing F11 again will exit this mode.)
  • ESC (Stop page or download from loading.)
  • CTRL-or+ (Increase or decrease the font size, pressing ‘-‘ will decrease and ‘+’ will increase)
  • CTRL+Enter (Quickly complete an address. For example, type Google in the address bar and press CTRL + ENTER to get http://www.Google.com)
  • CTRL+D (Add a favorite for the page currently opened)
  • CTRL+I (Display available bookmarks)
  • CTRL+N (Open new browser Window)
  • CTRL+P (Print current page/frame)
  • CTRL+T (Opens a new tab)
  • CTRL+F4 (Close the currently selected tab)
  • CTRL+Tab (Moves through each of the open tabs)
  • Spacebar (Moves down a page at a time)
  • Shift+Spacebar (Moves up a page at a time)
  • ALT+Down arrow (Display all previous text entered in a text box and/or available options on drop down menu)

Windows Tree Control:

  • Numeric Keypad * (Expands everything under the current selection)
  • Numeric Keypad + (Expands the current selection)
  • Numeric Keypad – (Collapses the current selection)
  • RIGHT ARROW (Expands the current selection if it is not expanded, otherwise goes to the first child)
  • LEFT ARROW (Collapses the current selection if it is expanded, otherwise goes to the parent)

May 8, 2009 Posted by | Uncategorized | Leave a comment

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP PART-03

Validating a drop-down menu with Spry

Applying a select validation widget is very simple. Highlight the menu object in Design

view, and click the Spry Validation Select button in the Insert bar. The following instructions

use the same form as throughout the rest of the chapter.

Use feedback_spry.php from the preceding exercise, or copy feedback_spry_start.php

from examples/ch09 to workfiles/ch09 and save it as feedback_spry.php.

1. In Design view, select the existing drop-down menu, and then click the List Values

button in the Property inspector. The first item (– Select one –) is an invalid choice,

so you need to take a note of its Value (0). Close the List Values dialog box.

2. With the menu still selected, click the Spry Validation Select button in the Insert bar.

3. In the Property inspector, select the Invalid value checkbox, and replace the default

-1 with 0 in the field alongside. This is the invalid value you confirmed in step 1.

Also select Validate on Change.

4. Save the page, and test it in a browser. An alert message should be displayed if you

select nothing or an invalid value.

As with the checkbox group, you can convert <span> tags to <div> tags. Spry isn’t concerned

with the type of element used, but with the class and id attributes.

Styling the alert messages follows the same principles as for a text field in the various validation

widgets. Study the style sheets in the Spry assets folder, or click the Customize this

widget link in the Property inspector to display Dreamweaver help, which explains which

style rules to change.

Next, let’s move to the server side . . .

This has been a long chapter, crammed with detail, but it’s an important one. You’ll use

forms time and again when building dynamic sites, and making sure that user input is in

the right format saves endless headaches later on. Spry does a lot to help with validation

and is fairly easy to use, but the Dreamweaver interface could still do with some improvement.

However, it’s important to remember that client-side validation is only half the story.

Applying a validation widget to an existing drop-down menu

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

292

Because JavaScript can be turned off in the browser, you also need to check user input on

the server side with PHP.

Moreover, forms are useless without a script capable of processing the data. The next

chapter serves as a crash course in PHP basics for readers new to PHP. Then in Chapter 11,

we get down to the nitty-gritty of server-side programming, using PHP to validate user

input and then send it to your mail inbox.

BUILDING ONLINE FORMS AND VALIDATING INPUT

293

9

10 INTRODUCING THE BASICS OF PHP

This chapter is a cross between a crash course in PHP and a handy reference. It’s aimed at

readers who are completely new to PHP or who may have dabbled without really getting

to grips with the language. The intention is not to teach you all there is to know but to arm

you with sufficient knowledge to dig into Code view to customize Dreamweaver code with

confidence. Dreamweaver’s automatic code generation does a lot of the hard work for

you, but you need to tweak the code to get the best out of it, and when it comes to sending

an email from an online form, you have to do everything yourself.

By the end of this chapter, you’ll learn about

Writing and understanding PHP scripts

Using variables to represent changing values

Understanding the difference between single and double quotes

Organizing related information with arrays

Creating pages that make decisions for themselves

Using loops and functions for repetitive work

If you’re already comfortable with PHP, just glance at the section headings to see what’s

covered, as you might find it useful to refer to this chapter if you need to refresh your

memory about a particular subject. Then move straight to the next chapter and start

coding.

If you’re new to PHP, don’t try to learn everything at one sitting, or your brain is likely to

explode from information overload. On the first reading, look at the headings and maybe

the first paragraph or two under each one to get a general overview. Also read the section

“Understanding PHP error messages.”

Introducing the basics of PHP

PHP is a server-side language. This means that the web server processes your PHP code and

sends only the results—usually as XHTML—to the browser. Because all the action is on the

server, you need to tell it that your pages contain PHP code. This involves two simple steps,

namely:

Give every page a PHP file name extension—the default is .php. Do not use anything

other than .php unless you are told to specifically by your hosting company.

Enclose all PHP code within PHP tags.

The opening tag is <?php and the closing tag is ?>. You may come across <? as a short version

of the opening tag. However, <? doesn’t work on all servers. Stick with <?php, which is

guaranteed to work.

Embedding PHP in a web page

When somebody visits your site and requests a PHP page, the server sends it to the PHP

engine, which reads the page from top to bottom looking for PHP tags. XHTML passes

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

296

through untouched, but whenever the PHP engine encounters a <?php tag, it starts

processing your code and continues until it reaches the closing ?> tag. If the PHP code

produces any output, it’s inserted at that point. Then, any remaining XHTML passes

through until another <?php tag is encountered.

PHP doesn’t always produce direct output for the browser. It may, for instance, check the

contents of form input before sending an email message or inserting information into a

database. So some code blocks are placed above or below the main XHTML code. Code

that produces direct output, however, always goes where you want the output to be

displayed.

A typical PHP page uses some or all of the following elements:

Variables to act as placeholders for unknown or changing values

Arrays to hold multiple values

Conditional statements to make decisions

Loops to perform repetitive tasks

Functions to perform preset tasks

Ending commands with a semicolon

PHP is written as a series of commands or statements. Each statement normally tells the

PHP engine to perform a particular action, and it must always be followed by a semicolon,

like this:

<?php

do this;

now do something else;

finally, do that;

?>

PHP is not like JavaScript or ActionScript. It won’t automatically assume there should be a

semicolon at the end of a line if you leave it out. This has a nice side effect: you can spread

long statements over several lines and lay out your code for ease of reading. PHP, like

XHTML, ignores whitespace in code. Instead, it relies on semicolons to indicate where one

command ends and the next one begins.

To save space, I won’t always surround code samples with PHP tags.

You can have as many PHP code blocks as you like on a page, but they

cannot be nested inside each other.

INTRODUCING THE BASICS OF PHP

297

10

Using variables to represent changing values

A variable is simply a name that you give to something that may change or that you don’t

know in advance. The name that you give to a variable remains constant, but the value

stored in the variable can be changed at any time.

Although this concept sounds abstract, we use variables all the time in everyday life. When

you meet somebody for the first time, one of the first things you ask is, “What’s your

name?” It doesn’t matter whether the person you’ve just met is Tom, Dick, or Harry, the

word “name” remains constant, but the value we store in it varies for different people.

Similarly, with your bank account, money goes in and out all of the time (mostly out, it

seems), but it doesn’t matter whether you’re scraping the bottom of the barrel or as rich

as Croesus, the amount of money in your account is always referred to as the balance.

Naming variables

You can choose just about anything you like as the name for a variable, as long as you keep

the following rules in mind:

Variables always begin with $ (a dollar sign).

The first character after the dollar sign cannot be a number.

No spaces or punctuation are allowed, except for the underscore (_).

Variable names are case sensitive: $name and $Name are not the same.

A variable’s name should give some indication of what it represents: $name, $email, and

$totalPrice are good examples. Because you can’t use spaces in variable names, it’s a

good idea to capitalize the first letter of the second or subsequent words when combining

them (sometimes called camel case). Alternatively, you can use an underscore (e.g.,

$total_price).

Don’t try to save time by using really short variables. Using $n, $e, and $tp instead of

descriptive ones makes code harder to understand. More important, it makes errors more

difficult to spot.

Assigning values to variables

Variables get their values from a variety of sources, including the following:

User input through online forms

A database

An external source, such as a news feed or XML file

The result of a calculation

Direct inclusion in the PHP code

Although you have considerable freedom in the choice of variable names, you

can’t use $this, because it has a special meaning in PHP object-oriented programming.

It’s also advisable to avoid using any of the keywords listed at

http://www.php.net/manual/en/reserved.php.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

298

Wherever the value comes from, it’s always assigned in the same way with an equal sign

(=), like this:

$variable = value;

Because it assigns a value, the equal sign is called the assignment operator. Although it’s an

equal sign, get into the habit of thinking of it as meaning “is set to” rather than “equals.”

This is because PHP uses two equal signs (==) to mean “equals”—something that catches

out a lot of beginners (experienced PHP programmers are not immune to the occasional

lapse, either).

Use the following rules when assigning a value to a variable:

Strings must be enclosed in single or double quotes (the distinction between the

different types of quotes is explained later in the chapter).

Numbers should not be in quotes—enclosing a number in quotes turns it into a

string.

You can also use a variable to assign a value to another variable, for example:

$name = ‘David Powers';

$author = $name; // both $author and $name are now ‘David Powers’

If the value of $name changes subsequently, it doesn’t affect the value of $author. As this

example shows, you don’t use quotes around a variable when assigning its value to

another. However, as long as you use double quotes, you can embed a variable in a string

like this:

$blurb = “$author has written several best-selling books on PHP.”;

The value of $blurb is now “David Powers has written several best-selling books on PHP.”

There’s a more detailed description on the use of variables with double quotes in

“Choosing single or double quotation marks” later in the chapter.

Displaying PHP output

The most common ways of displaying dynamic output in the browser are to use echo or

print. The differences between the two are so subtle you can regard them as identical. I

prefer echo, because it’s one fewer letter to type. It’s also the style used by Dreamweaver.

Put echo (or print) in front of a variable, number, or string like this to output it to the

browser:

$name = ‘David';

echo $name; // displays David

echo 5; // displays 5

echo ‘David'; // displays David

You may see scripts that use parentheses with echo and print, like this:

echo(‘David’); // displays David

print(‘David’); // displays David

INTRODUCING THE BASICS OF PHP

299

10

The parentheses make no difference. Unless you enjoy typing purely for the sake of it,

leave them out.

Commenting scripts for clarity and debugging

Even if you’re an expert programmer, code is not always as immediately understandable as

something written in your own human language. That’s where comments can be a lifesaver.

You may understand what the code does five minutes after creating it, but when you

come back to maintain it in six months’ time—or if you have to maintain someone else’s

code—you’ll be grateful for well-commented code.

In PHP, there are three ways to add comments. The first will be familiar to you if you write

JavaScript. Anything on a line following a double slash is regarded as a comment and will

not be processed.

// Display the name

echo $name;

You can also use the hash sign (#) in place of the double slash.

# Display the name

echo $name;

Either type of comment can go to the side of the code, as long as it doesn’t go onto the

next line.

echo $name; // This is a comment

echo $name; # This is another comment

The third style allows you to stretch comments over several lines by sandwiching them

between /* and */ (just like CSS comments).

/* You might want to use this sort of comment to explain

the whole purpose of a script. Alternatively, it’s a

convenient way to disable part of a script temporarily.

*/

As the previous example explains, comments serve a dual purpose: they not only allow you

to sprinkle your scripts with helpful reminders of what each section of code is for; they can

also be used to disable a part of a script temporarily. This is extremely useful when you are

trying to trace the cause of an error.

The important thing to remember about echo and print is that they work only with

variables that contain a single value. You cannot use them to display more complex

structures that are capable of storing multiple values.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

300

Choosing single or double quotation marks

As I mentioned earlier, strings must always be enclosed in single or double quotes. If all

you’re concerned about is what ends up on the screen, most of the time it doesn’t matter

which quotes you use, but behind the scenes, PHP uses single and double quotes in very

different ways.

Anything between single quotation marks is treated as plain text.

Anything between double quotation marks is processed.

Quotation marks need to be in matching pairs. If a string begins with a single quote, PHP

looks for the next single quote and regards that as the end of the string. Since an apostrophe

uses the same character as a single quote, this presents a problem. A similar problem

arises when a string in double quotes contains double quotes. The best way to explain this

is with a practical example.

This simple exercise demonstrates the difference between single and double quotes and

what happens when a conflict arises with an apostrophe or double quotes inside a string.

1. Create a new PHP page called quotes.php in workfiles/ch10. If you just want to

look at the finished code, use quotes.php in examples/ch10.

2. Switch to Code view, and type the following code between the <body> tags:

<?php

$name = ‘David Powers';

echo ‘Single quotes: The author is $name<br />';

echo “Double quotes: The author is $name”;

?>

3. Save the page, and load it into a browser. As you can see from the following

screenshot, $name is treated as plain text in the first line, but is processed and

replaced with its value in the second line, which uses double quotes.

To display the output on separate lines, you have to include XHTML tags, such as

<br />, because echo outputs only the values passed to it—nothing more.

Experimenting with quotes

INTRODUCING THE BASICS OF PHP

301

10

4. Change the text slightly in lines 3 and 4 of the code, as follows:

echo ‘Single quotes: The author‘s name is $name<br />';

echo “Double quotes: The author‘s name is $name”;

As you type, the change in Dreamweaver syntax coloring should alert you to a

problem, but save the page nevertheless, and view it in a browser (it’s quotes2.php

in examples/ch10). You should see something like this:

As far as PHP is concerned, an apostrophe and a single quote are the same thing,

and quotes must always be in matching pairs. What’s happened is that the apostrophe

in author’s has been regarded as the closing quote for the first line; what was

intended as the closing quote of the first line becomes a second opening quote;

and the apostrophe in the second line becomes the second closing quote. All quite

different from what was intended—and if you’re confused, is it any wonder that

PHP is unable to work out what’s meant to be going on?

5. To solve the problem, insert a backslash in front of the apostrophe in the first sentence,

like this (see quotes3.php in examples/ch10):

echo ‘Single quotes: The author\‘s name is $name<br />';

You should now see the syntax coloring revert to normal. If you view the result in a

browser, it should display correctly like this:

The meaning of parse error and other error messages is explained in “Understanding

PHP error messages” at the end of the chapter.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

302

Using escape sequences in strings

Using a backslash like this is called an escape sequence. It tells PHP to treat a character in

a special way. Double quotes within a double-quoted string? You guessed it—escape them

with a backslash.

echo “Swift’s \”Gulliver’s Travels\””; // displays the double quotes

The next line of code achieves exactly the same thing, but by using a different combination

of quotes:

echo ‘Swift\’s “Gulliver\’s Travels”‘;

So what happens when you want to include a literal backslash? You escape it with a backslash

(\\).

The backslash (\\) and the single quote (\’) are the only escape sequences that work in a

single-quoted string. Because double quotes are a signal to PHP to process any variables

contained within a string, there are many more escape sequences for double-quoted

strings. Most of them are to avoid conflicts with characters that are used with variables,

but three of them have special meanings: \n inserts a new line character, \r inserts a carriage

return (needed mainly for Windows), and \t inserts a tab. Table 10-1 lists the main

escape sequences supported by PHP.

Table 10-1. The main PHP escape sequences

Escape sequence Character represented in double-quoted string

\” Double quote

\n New line

\r Carriage return

\t Tab

\\ Backslash

\$ Dollar sign

\{ Opening curly brace

\} Closing curly brace

\[ Opening square bracket

\] Closing square bracket

When creating strings, the outside pair of quotes must match—any quotes of the

same style inside the string must be escaped with a backslash. However, putting a

backslash in front of the opposite style of quote will result in the backslash being displayed.

To see the effect, put a backslash in front of the apostrophe in the doubledquoted

string in the previous exercise.

INTRODUCING THE BASICS OF PHP

303

10

Double quotes are obviously very useful, so why not use them all the time? Many people

do, but the official recommendation is to use the quoting method that uses the least processing

power—and that’s usually single quotes. When PHP sees an opening double quote,

it tries to process any variables first. If it finds none, it goes back and treats the string as

plain text. On short scripts, such as in this book, the difference in processing time is negligible,

but it can make a difference on long, complex scripts.

Joining strings together

PHP has a rather unusual way of joining strings. Although many other computer languages

use the plus sign (+), PHP uses a period, dot, or full stop (.) like this:

$firstName = ‘David';

$lastName = ‘Powers';

echo $firstName.$lastName; // displays DavidPowers

As the comment in the final line of code indicates, when two strings are joined like this,

PHP leaves no gap between them. Don’t be fooled into thinking that adding a space after

the period will do the trick. It won’t. You can put as much space on either side of the

period as you like; the result will always be the same, because PHP ignores whitespace in

code. You must either include a space in one of the strings or insert the space as a string

in its own right, like this:

echo $firstName.’ ‘.$lastName; // displays David Powers

Adding to an existing string

Very often, you need to add more text at the end of an existing string. One way to do it is

like this:

$author = ‘David';

$author = $author.’ Powers'; // $author is now ‘David Powers’

Basically, this concatenates Powers (with a leading space) on the end of $author, and then

assigns everything back to the original variable.

Adding something to an existing variable is such a common operation, PHP offers a shorthand

way of doing it—with the combined concatenation operator. Don’t worry about the

highfalutin name, it’s just a period followed by an equal sign. It works like this:

The period—or concatenation operator, to give it its correct name—can be difficult

to spot among a lot of other code. Make sure the font size in your script editor is large

enough to read without straining to see the difference between periods and commas.

The escape sequences listed in Table 10-1, with the exception of \\, work only in double-

quoted strings. If you use them in a single-quoted string, they are treated as a literal

backslash followed by the second character.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

304

$author = ‘David';

$author .= ‘ Powers'; // $author is now ‘David Powers’

There should be no space between the period and equal sign. You’ll find this shorthand

very useful when building the string to form the body of an email message in the next

chapter.

Using quotes efficiently

Award yourself a bonus point if you spotted a better way of adding the space between

$firstName and $lastName in the last example. Yes, that’s right . . . Use double quotes,

like this:

echo “$firstName $lastName”; // displays David Powers

Choosing the most efficient combination of quotation marks isn’t easy when you first start

working with PHP, but it can make your code a lot easier to use. Many beginners stick

rigidly to double quotes for everything, and end up peppering their scripts with backslashes

to escape double quotes in the middle of strings. It not only makes scripts difficult

to read, but usually results in PHP errors or broken XHTML.

Special cases: true, false and null

Although text should be enclosed in quotes, three special cases—true, false, and null—

should never be enclosed in quotes unless you want to treat them as genuine text (or

strings). The first two mean what you would expect; the last one, null, means “nothing” or

“no value.”

PHP makes decisions on the basis of whether something evaluates to true or false.

Putting quotes around false has surprising consequences. The following code:

$OK = ‘false';

does exactly the opposite of what you might expect: it makes $OK true! Why? Because the

quotes around false turn it into a string, and PHP treats strings as true. The other thing

to note about true, false, and null is that they are case insensitive. The following examples

are all valid:

$OK = TRUE;

$OK = tRuE;

$OK = true;

How long can a string be? As far as PHP is concerned, there’s no limit. In practice, you

are likely to be constrained by other factors, such as server memory; but in theory,

you could store the whole of War and Peace in a string variable.

INTRODUCING THE BASICS OF PHP

305

10

Working with numbers

PHP can do a lot with numbers—from simple addition to complex math. Numbers can

contain a decimal point or use scientific notation, but they must contain no other punctuation.

Never use a comma as a thousands separator. The following examples show the

right and wrong ways to assign a large number to a variable:

$million = 1000000; // this is correct

$million = 1,000,000; // this generates an error

$million = 1e6; // this is correct

$million = 1e 6; // this generates an error

When using scientific notation, the letter “e” can be uppercase or lowercase and optionally

followed by a plus or minus sign. No spaces are permitted.

Performing calculations

The standard arithmetic operators all work the way you would expect, although some of

them look slightly different from those you learned at school. For instance, an asterisk (*)

is used as the multiplication sign, and a forward slash (/) is used to indicate division.

Table 10-2 shows examples of how the standard arithmetic operators work. To demonstrate

their effect, the following variables have been set:

$x = 20;

$y = 10;

$z = 3;

Table 10-2. Arithmetic operators in PHP

Operation Operator Example Result

Addition + $x + $y 30

Subtraction – $x – $y 10

Multiplication * $x * $y 200

Division / $x / $y 2

Modulo division % $x % $z 2

Increment (add 1) ++ $x++ 21

Decrement (subtract 1) — $y– 9

You may not be familiar with the modulo operator. This returns the remainder of a division,

as follows:

26 % 5 // result is 1

26 % 27 // result is 26

10 % 2 // result is 0

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

306

A quirk with the modulo operator in PHP is that it converts both numbers to integers

before performing the calculation. Consequently, if $z is 4.5 in Table 10-2, it gets rounded

up to 5, making the result 0, not 2, as you might expect. (Yes, it was a mistake in my previous

books.)

A practical use of the modulo operator is to work out whether a number is odd or even.

$number % 2 will always produce 0 or 1.

The increment (++) and decrement (–) operators can come either before or after the

variable. When they come before the variable, 1 is added to or subtracted from the value

before any further calculation is carried out. When they come after the variable, the main

calculation is carried out first, and then 1 is either added or subtracted. Since the dollar

sign is an integral part of the variable name, the increment and decrement operators go

before the dollar sign when used in front:

++$x

–$y

You can set your own values for $x, $y, and $z in calculation.php in examples/ch10 to

test the arithmetic operators in action. The page also demonstrates the difference

between putting the increment and decrement operators before and after the variable.

As noted earlier, numbers should not normally be enclosed in quotes, although PHP will

usually convert to its numeric equivalent a string that contains only a number or that

begins with a number.

Calculations in PHP follow exactly the same rules as standard arithmetic. Table 10-3 summarizes

the precedence of arithmetic operators.

Table 10-3. Precedence of arithmetic operators

Precedence Group Operators Rule

Highest Parentheses () Operations contained within

parentheses are evaluated first. If

these expressions are nested, the

innermost is evaluated foremost.

Next Multiplication * / % These operators are evaluated

and division next. If an expression contains

two or more operators, they are

evaluated from left to right.

Lowest Addition and + – These are the final operators to

subtraction be evaluated in an expression.

If an expression contains two

or more operators, they are

evaluated from left to right.

If in doubt, use parentheses all the time to group the parts of a calculation that you want

to make sure are performed as a single unit.

INTRODUCING THE BASICS OF PHP

307

10

Combining calculations and assignment

You will often want to perform a calculation on a variable and assign the result back to the

same variable. PHP offers the same convenient shorthand for arithmetic calculations as for

strings. Table 10-4 shows the main combined assignment operators and their use.

Table 10-4. Combined arithmetic assignment operators used in PHP

Operator Example Equivalent to

+= $a += $b $a = $a + $b

-= $a -= $b $a = $a – $b

*= $a *= $b $a = $a * $b

/= $a /= $b $a = $a / $b

%= $a %= $b $a = $a % $b

Don’t forget that the plus sign is used in PHP only as an arithmetic operator.

Addition: Use += as the combined assignment operator

Strings: Use .= as the combined assignment operator

Using arrays to store multiple values

Arrays are an important—and useful—part of PHP. You met one of PHP’s built-in arrays,

$_POST, in the last chapter, and you’ll work with it a lot more through the rest of this book.

Arrays are also used extensively with a database, as you fetch the results of a search in a

series of arrays.

An array is a special type of variable that stores multiple values rather like a shopping list.

Although each item might be different, you can refer to them collectively by a single

name. Figure 10-1 demonstrates this concept: the variable $shoppingList refers collectively

to all five items—wine, fish, bread, grapes, and cheese.

Individual items—or array elements—are identified by means of a number in square

brackets immediately following the variable name. PHP assigns the number automatically,

but it’s important to note that the numbering always begins at 0. So the first item in the

array, wine, is referred to as $shoppingList[0], not $shoppingList[1]. And although

there are five items, the last one (cheese) is $shoppingList[4]. The number is referred to

as the array key or index, and this type of array is called an indexed array.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

308

Figure 10-1. Arrays are variables that store multiple items, just like a shopping list.

Instead of declaring each array element individually, you can declare the variable name

once, and assign all the elements by passing them as a comma-separated list to array(),

like this:

$shoppingList = array(‘wine’, ‘fish’, ‘bread’, ‘grapes’, ‘cheese’);

PHP numbers each array element automatically, so this creates the same array as in

Figure 10-1. To add a new element to the end of the array, use a pair of empty square

brackets like this:

$shoppingList[] = ‘coffee';

PHP uses the next number available, so this becomes $shoppingList[5].

Using names to identify array elements

Numbers are fine, but it’s often more convenient to give array elements meaningful

names. For instance, an array containing details of this book might look like this:

$book['title'] = ‘Essential Guide to Dreamweaver CS3′;

$book['author'] = ‘David Powers';

$book['publisher'] = ‘friends of ED';

$book['ISBN13'] = ‘978-1-59059-859-7′;

The comma must go outside the quotes, unlike American typographic practice.

For ease of reading, I have inserted a space following each comma, but it’s not

necessary to do so.

INTRODUCING THE BASICS OF PHP

309

10

This type of array is called an associative array. Note that the array key is enclosed in

quotes (single or double, it doesn’t matter). It mustn’t contain any spaces or punctuation,

except for the underscore.

The shorthand way of creating an associative array uses the => operator (an equal sign followed

by a greater-than sign) to assign a value to each array key. The basic structure looks

like this:

$arrayName = array(‘key1‘ => ‘element1‘, ‘key2‘ => ‘element2‘);

So, this is the shorthand way to build the $book array:

$book = array(‘title’ => ‘Essential Guide to Dreamweaver CS3′,

‘author’ => ‘David Powers’,

‘publisher’ => ‘friends of ED’,

‘ISBN13′ => ‘978-1-59059-859-7′);

It’s not essential to align the => operators like this, but it makes code easier to read and

maintain.

Inspecting the contents of an array with print_r()

As you saw in the previous chapter, you can inspect the contents of an array using

print_r(). This is the code that you inserted at the bottom of feedback.php:

<pre>

<?php if ($_POST) {print_r($_POST);} ?>

</pre>

It displays the contents of the array like this:

The <pre> tags are simply to make the output more readable. What really matters here is

that print_r() displays the contents of an array. As explained earlier, echo and print

work only with variables that contain a single value. However, the only real value of

print_r() is to inspect the contents of an array for testing purposes. It’s no good in a live

web page. To display the contents of an array in normal circumstances, you need to use a

Technically speaking, all arrays in PHP are associative. This means that you can

use both numbers and strings as array keys in the same array. Don’t do it,

though, as it can produce unexpected results. It’s safer to treat indexed and

associative arrays as different types.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

310

loop. This gives you access to each array element one at a time. Once you get to an element

that contains a single value, you can use echo or print to display its contents. Loops

are covered a little later.

Making decisions

Decisions, decisions, decisions . . . Life is full of decisions. So is PHP. They give it the ability

to display different output according to circumstances. Decision making in PHP uses conditional

statements. The most common of these uses if and closely follows the structure

of normal language. In real life, you may be faced with the following decision (admittedly

not very often if you live in Britain):

If the weather’s hot, I’ll go to the beach.

In PHP pseudo-code, the same decision looks like this:

if (the weather’s hot) {

I’ll go to the beach;

}

The condition being tested goes inside parentheses, and the resulting action goes between

curly braces. This is the basic decision-making pattern:

if (condition is true) {

// code to be executed if condition is true

}

The code inside the curly braces is executed only if the condition is true. If it’s false, PHP

ignores everything between the braces and moves on to the next section of code. How

PHP determines whether a condition is true or false is described in the following section.

Sometimes, the if statement is all you need, but you often want a default action to be

invoked. To do this, use else, like this:

if (condition is true) {

// code to be executed if condition is true

}

else {

// default code to run if condition is false

}

Confusion alert: I mentioned earlier that statements must always be followed by a

semicolon. This applies only to the statements (or commands) inside the curly braces.

Although called a conditional statement, this decision-making pattern is one of PHP’s

control structures, and it shouldn’t be followed by a semicolon. Think of the semicolon

as a command that means “do it.” The curly braces surround the command

statements and keep them together as a group.

INTRODUCING THE BASICS OF PHP

311

10

What if you want more alternatives? One way is to add more if statements. PHP will test

them, and as long as you finish with else, at least one block of code will run. However, it’s

important to realize that all if statements will be tested, and the code will be run in every

single one where the condition equates to true. If you want only one code block to be

executed, use elseif like this:

if (condition is true) {

// code to be executed if first condition is true

}

elseif (second condition is true) {

// code to be executed if first condition fails

// but second condition is true

else {

// default code to run if both conditions are false

}

You can use as many elseif clauses in a conditional statement as you like. It’s important

to note that only the first one that equates to true will be executed; all others will be

ignored, even if they’re also true. This means you need to build conditional statements in

the order of priority that you want them to be evaluated. It’s strictly a first-come, firstserved

hierarchy.

The truth according to PHP

Decision making in PHP conditional statements is based on the mutually exclusive Boolean

values, true and false (the name comes from a nineteenth century mathematician,

George Boole, who devised a system of logical operations that subsequently became the

basis of much modern-day computing). If the condition equates to true, the code within

the conditional block is executed. If false, it’s ignored. Whether a condition is true or

false is determined in one of the following ways:

A variable set explicitly to true or false

A value PHP interprets implicitly as true or false

The comparison of two values

Explicit true or false values This is straightforward. If a variable is assigned the value true

or false and then used in a conditional statement, the decision is based on that value. As

explained earlier, true and false are case insensitive and must not be enclosed in quotes.

Implicit true or false values PHP regards the following as false:

The case-insensitive keywords false and null

Zero as an integer (0), a floating-point number (0.0), or a string (‘0′ or “0”)

An empty string (single or double quotes with no space between them)

An empty array

Although elseif is normally written as one word, you can use else if

as separate words.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

312

An object with no values or functions (PHP 4 only)

A SimpleXML object created from empty tags

All other values equate to true.

How comparisons equate to true or false is described in the next section.

Using comparisons to make decisions

Conditional statements often depend on the comparison of two values. Is this bigger than

that? Are they both the same? If the comparison is true, the conditional statement is executed.

If not, it’s ignored.

To test for equality, PHP uses two equal signs (==) like this:

if ($status == ‘administrator’) {

// send to admin page

}

else {

// refuse entry to admin area

}

Size comparisons are performed using the mathematical symbols for less than (<) and

greater than (>). Let’s say you’re checking the size of a file before allowing it to be

uploaded to your server. You could set a maximum size of 50KB like this:

if ($bytes > 51200) {

// display error message and abandon upload

}

else {

// continue upload

}

Comparison operators These compare two values (known as operands because they

appear on either side of an operator). If both values pass the test, the result is true (or to

use the technical expression, it returns true). Otherwise, it returns false. Table 10-5 lists

the comparison operators used in PHP.

Don’t use a single equal sign in the first line like this:

if ($status = ‘administrator’) {

Doing so will open the admin area of your website to everyone. Why? Because

this automatically sets the value of $status to administrator; it doesn’t compare

the two values. To compare values, you must use two equal signs. It’s an

easy mistake to make, but one with potentially disastrous consequences.

This definition explains why “false” (in quotes) is interpreted by PHP as

true. The value –1 is also treated as true in PHP.

INTRODUCING THE BASICS OF PHP

313

10

Table 10-5. PHP comparison operators used for decision making

Symbol Name Use

== Equality Returns true if both operands have the

same value; otherwise, returns false.

!= Inequality Returns true if both operands have

different values; otherwise, returns false.

<> Inequality This has the same meaning as !=. It’s rarely

used in PHP but has been included here for

the sake of completeness.

=== Identical Determines whether both operands are

identical. To be considered identical, they

must not only have the same value but also

be of the same datatype (for example, both

floating-point numbers).

!== Not identical Determines whether both operands are not

identical (according to the same criteria as

the previous operator).

> Greater than Determines whether the operand on the left

is greater in value than the one on the right.

>= Greater than or equal to Determines whether the operand on the left

is greater in value than or equal to the one

on the right.

< Less than Determines whether the operand on the left

is less in value than the one on the right.

<= Less than or equal to Determines whether the operand on the left

is less in value than or equal to the one on

the right.

Testing more than one condition

Frequently, comparing two values is not enough. PHP allows you to set a series of conditions

using logical operators to specify whether all, or just some, need to be fulfilled.

All the logical operators in PHP are listed in Table 10-6. Negation—testing that the opposite

of something is true—is also considered a logical operator, although it applies to

individual conditions rather than a series.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

314

Table 10-6. Logical operators used for decision making in PHP

Symbol Name Use

&& Logical AND Evaluates to true if both operands are true. If the lefthand

operand evaluates to false, the right-hand

operand is never tested.

and Logical AND Exactly the same as &&, but it takes lower precedence.

|| Logical OR Evaluates to true if either operand is true; otherwise,

returns false. If the left-hand operand returns true,

the right-hand operand is never tested.

or Logical OR Exactly the same as ||, but it takes lower precedence.

xor Exclusive OR Evaluates to true if only one of the two operands

returns true. If both are true or both are false, it

evaluates to false.

! Negation Tests whether something is not true.

Technically speaking, there is no limit to the number of conditions that can be tested. Each

condition is considered in turn from left to right, and as soon as a defining point is

reached, no further testing is carried out. When using && or and, every condition must be

fulfilled, so testing stops as soon as one turns out to be false. Similarly, when using || or

or, only one condition needs to be fulfilled, so testing stops as soon as one turns out to

be true.

$a = 10;

$b = 25;

if ($a > 5 && $b > 20) // returns true

if ($a > 5 || $b > 30) // returns true, $b never tested

The implication of this is that when you need all conditions to be met, you should design

your tests with the condition most likely to return false as the first to be evaluated. When

you need just one condition to be fulfilled, place the one most likely to return true first.

If you want a particular set of conditions considered as a group, enclose them in parentheses.

if (($a > 5 && $a < 8) || ($b > 20 && $b < 40))

Operator precedence is a tricky subject. Stick with && and ||, rather than and and or, and

use parentheses to group expressions to which you want to give priority. The xor operator

is rarely used.

INTRODUCING THE BASICS OF PHP

315

10

Using the switch statement for decision chains

The switch statement offers an alternative to if . . . else for decision making. The

basic structure looks like this:

switch(variable being tested) {

case value1:

statements to be executed

break;

case value2:

statements to be executed

break;

default:

statements to be executed

}

The case keyword indicates possible matching values for the variable passed to switch().

When a match is made, every subsequent line of code is executed until the break keyword

is encountered, at which point the switch statement comes to an end.

Dreamweaver uses a switch statement in the GetSQLValueString() function, which it

inserts into pages that insert or update records in a database.

The main points to note about switch are as follows:

The expression following the case keyword must be a number or a string.

You can’t use comparison operators with case. So case > 100: isn’t allowed.

Each block of statements should normally end with break, unless you specifically

want to continue executing code within the switch statement.

You can group several instances of the case keyword together to apply the same

block of code to them.

If no match is made, any statements following the default keyword will be executed.

If no default has been set, the switch statement will exit silently and continue

with the next block of code.

Using the conditional operator

The conditional operator (?:) is a shorthand method of representing a simple conditional

statement. The basic syntax looks like this:

condition ? value if true : value if false;

What this means is that, if the condition to the left of the question mark is true, the value

immediately to the right of the question mark is used. However, if the condition evaluates

to false, the value to the right of the colon is used instead. Here is an example of it in use:

$age = 17;

$fareType = $age > 16 ? ‘adult’ : ‘child';

The conditional operator can be quite confusing when you first encounter it, so let’s break

down this example section by section.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

316

The first line sets the value of $age to 17.

The second line sets the value of $fareType using the conditional operator. The condition

is between the equal sign and the question mark—in other words, $age > 16.

If $age is greater than 16, the condition evaluates to true, so $fareType is set to the value

between the question mark and the colon—in other words, ‘adult’. Otherwise $fareType

is set to the value to the right of the colon—or ‘child’. You can rewrite the second line

using if . . . else like this:

if ($age > 16) {

$fareType = ‘adult';

}

else {

$fareType = ‘child';

}

The if . . . else version is much easier to read, but the conditional operator is more

compact, and it’s used frequently by Dreamweaver. Most beginners hate this shorthand,

but you need to understand how it works if you want to customize Dreamweaver code.

Because the conditional operator uses three operands, it’s sometimes called the ternary

operator.

Using loops for repetitive tasks

Loops are huge time-savers, because they perform the same task over and over again, yet

involve very little code. They’re frequently used with arrays and database results. You can

step through each item one at a time looking for matches or performing a specific task.

Loops frequently contain conditional statements, so although they’re very simple in structure,

they can be used to create code that processes data in often sophisticated ways.

Loops using while and do . . . while

The simplest type of loop is called a while loop. Its basic structure looks like this:

while (condition is true) {

do something

}

The following code displays every number from 1 through 100 in a browser (you can see it

in action in while.php in examples/ch10). It begins by setting a variable ($i) to 1, and then

using the variable as a counter to control the loop, as well as display the current number

onscreen.

$i = 1; // set counter

while ($i <= 100) {

echo “$i<br />”;

$i++; // increase counter by 1

}

INTRODUCING THE BASICS OF PHP

317

10

A variation of the while loop uses the keyword do and follows this basic pattern:

do {

code to be executed

} while (condition to be tested);

The only difference between a do . . . while loop and a while loop is that the code

within the do block is executed at least once, even if the condition is never true. The following

code (in dowhile.php in examples/ch10) displays the value of $i once, even

though it’s greater than the maximum expected.

$i = 1000;

do {

echo “$i<br />”;

$i++; // increase counter by 1

} while ($i <= 100);

Dreamweaver uses a do . . . while loop in its Repeat Region server behavior to loop

through the results of a database query (what Dreamweaver calls a recordset) and display

them on your page.

The danger with creating while and do . . . while loops yourself is forgetting to set a

condition that brings the loop to an end or setting an impossible condition. When this

happens, you create an infinite loop that either freezes your computer or causes the

browser to crash.

The versatile for loop

The for loop is less prone to generating an infinite loop, because you specify in the first

line how you want the loop to work. The for loop uses the following basic pattern:

for (initialize counter; test; increase or decrease the counter) {

code to be executed

}

The three expressions inside the parentheses control the action of the loop (note that they

are separated by semicolons, not commas):

The first expression initializes the counter variable at the start of the loop. You can

use any variable you like, but the convention is to use $i. When more than one

counter is needed, $j and $k are frequently used.

The second expression is a test that determines whether the loop should continue

to run. This can be a fixed number, a variable, or an expression that calculates a

value.

The third expression shows the method of stepping through the loop. Most of the

time, you will want to go through a loop one step at a time, so using the increment

(++) or decrement (–) operator is convenient.

The following code does exactly the same as the previous while loop, displaying every

number from 1 to 100 (see forloop.php in examples/ch10):

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

318

for ($i = 1; $i <= 100; $i++) {

echo “$i<br />”;

}

There is nothing stopping you from using bigger steps. For instance, replacing $i++ with

$i+=10 in this example would display 1, 11, 21, 31, and so on.

Looping through arrays with foreach

The final type of loop in PHP is used exclusively with arrays. It takes two forms, both of

which use temporary variables to handle each array element. If you only need to do something

with the value of each array element, the foreach loop takes the following form:

foreach (array_name as temporary_variable) {

do something with temporary_variable

}

The following example loops through the $shoppingList array and displays the name of

each item (see shopping_list.php in examples/ch10):

$shoppingList = array(‘wine’, ‘fish’, ‘bread’, ‘grapes’, ‘cheese’);

foreach ($shoppingList as $item) {

echo $item.'<br />';

}

The preceding example accesses only the value of each array element. An alternative form

of the foreach loop gives access to both the key and value of each element. It takes this

slightly different form:

foreach (array_name as key_variable => value_variable) {

do something with key_variable and value_variable

}

This next example uses the $book array from “Using names to identify array elements” earlier

in the chapter and incorporates the key and value of each element into a simple string,

as shown in the screenshot (see book.php in examples/ch10):

foreach ($book as $key => $value) {

echo “The value of $key is $value<br />”;

}

INTRODUCING THE BASICS OF PHP

319

10

Breaking out of a loop

To bring a loop prematurely to an end when a certain condition is met, insert the break

keyword inside a conditional statement. As soon as the script encounters break, it exits

the loop.

To skip an iteration of the loop when a certain condition is met, use the continue keyword.

Instead of exiting, it returns to the top of the loop and executes the next iteration.

Using functions for preset tasks

Functions do things . . . lots of things, mind-bogglingly so in PHP. The last time I counted,

PHP had nearly 3,000 built-in functions, and more have been added since. Don’t worry:

you’ll only ever need to use a handful, but it’s reassuring to know that PHP is a fullfeatured

language capable of industrial-strength applications.

The functions you’ll be using in this book do really useful things, such as send email, query

a database, format dates, and much, much more. You can identify functions in PHP code,

because they’re always followed by a pair of parentheses. Sometimes the parentheses are

empty, as in the case of phpinfo(), which you used in test.php when setting up your testing

environment in Chapter 3. Often, though, the parentheses contain variables, numbers,

or strings, like this:

$thisYear = date(‘Y’);

This calculates the current year and stores it in the variable $thisYear. It works by feeding

the string ‘Y’ to the built-in PHP function date(). Placing a value between the parentheses

like this is known as passing an argument to a function. The function takes the value in

the argument and processes it to produce (or return) the result. For instance, if you pass

the string ‘M’ as an argument to date() instead of ‘Y’, it will return the current month as

a three-letter abbreviation (e.g., Mar, Apr, May). The date() function is covered in detail in

Chapter 17.

Some functions take more than one argument. When this happens, separate the arguments

with commas inside the parentheses, like this:

$mailSent = mail($to, $subject, $message);

It doesn’t take a genius to work out that this sends an email to the address stored in the

first argument, with the subject line stored in the second argument, and the message

stored in the third one. You’ll see how this function works in the next chapter.

You’ll often come across the term “parameter” in place of “argument.” There is

a technical difference between the two words, but for all practical purposes,

they are interchangeable.

The foreach keyword is one word. Inserting a space between for and

each doesn’t work.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

320

As if the 3,000-odd built-in functions weren’t enough, PHP lets you build your own custom

functions. Even if you don’t relish the idea of creating your own, throughout this book

you’ll use some that I have made. You use them in exactly the same way.

Understanding PHP error messages

There’s one final thing you need to know about before savoring the delights of PHP: error

messages. They’re an unfortunate fact of life, but it helps a great deal if you understand

what they’re trying to tell you. The following illustration shows the structure of a typical

error message.

The first thing to realize about PHP error messages is that they report the line where PHP

discovered a problem. Most newcomers—quite naturally—assume that’s where they’ve

got to look for their mistake. Wrong . . .

What PHP is telling you most of the time is that something unexpected has happened. In

other words, the mistake frequently lies before that point. The preceding error message

means that PHP discovered a foreach command where there shouldn’t have been one.

(Error messages always prefix PHP elements with T_, which stands for token. Just ignore it.)

Instead of worrying what might be wrong with the foreach command (probably nothing),

start working backward, looking for anything that might be missing. Usually, it’s a semicolon

or closing quote. In this example, the error was caused by omitting the semicolon at

the end of line 28 in book.php.

There are four main categories of error, presented here in descending order of importance:

Fatal error: Any XHTML output preceding the error will be displayed, but once the

error is encountered—as the name suggests—everything else is killed stone dead.

A fatal error is normally caused by referring to a nonexistent file or function.

Parse error: This means there’s a mistake in your code, such as mismatched quotes,

or a missing semicolon or closing brace. Like a fatal error, it stops the script in its

tracks and doesn’t even allow any XHTML output to be displayed.

Warning: This alerts you to a serious problem, such as a missing include file.

(Include files are covered in Chapter 12.) However, the error is not serious enough

to prevent the rest of the script from being executed.

Notice: This advises you about relatively minor issues, such as the use of a nondeclared

variable. Although you can turn off the display of notices, you should always

try to eliminate the cause, rather than sweep the issue under the carpet. Any error

is a threat to your output.

INTRODUCING THE BASICS OF PHP

321

10

Hosting companies have different policies about the level of error checking. If error checking

is set to a high level and the display of errors is turned off, any mistakes in your code

will result in a blank screen. Even if your hosting company has a more relaxed policy, you

still don’t want mistakes to be displayed for all to see. Test your code thoroughly, and eliminate

all errors before deploying it on a live website.

Another type of error, strict, was introduced in PHP 5.0.0, mainly for the benefit of

advanced developers. As of this writing, strict error messages are not displayed by default,

but there are plans to change this and introduce a new deprecated category as a prelude

to removing outdated parts of the language. If you see a strict or deprecated error message,

ignore it at your peril.

Now put it to work . . .

After that crash course, I hope you’re feeling not like a crash victim but invigorated and

raring to go. Although you have been bombarded with a mass of information, you’ll discover

that it’s easy to make rapid progress with PHP. In the next chapter, you’ll use most of

the techniques from this chapter to send user input from an online form to your email

inbox. To begin with, you’ll probably feel that you’re copying code without much comprehension,

but I’ll explain all the important things along the way, and you should soon find

things falling into place.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

322

11 USING PHP TO PROCESS A FORM

In Chapter 9, I showed you how to build a feedback form and validate the input on the

client side with Spry validation widgets. In this chapter, we’ll take the process to its next

stage by validating the data on the server side with PHP. If the data is OK, we’ll send the

contents by email and display an acknowledgement message. If there’s a problem with any

of the data, we’ll redisplay it in the form with messages prompting the user to correct any

errors or omissions. Figure 11-1 shows the flow of events.

Figure 11-1. The flow of events in processing the feedback form

Sending an email from an online form is just the sort of task that Dreamweaver should

automate, but unfortunately it doesn’t. Commercial extensions are available to automate

the process for you, but not everyone will have—or want to buy—a commercial extension

in addition to Dreamweaver CS3, so I think it’s important to show you how to hand-code

this vital feature. At the same time, it gives you practical experience working with PHP

code, which is essential unless you are willing to be limited to very basic tasks. The

Dreamweaver server behaviors and data objects that you will use in later chapters take a

lot of the hard work out of creating dynamic applications, but like the CSS layout that you

used in Chapter 6, they lay a solid foundation for you to build on, rather than do

absolutely everything for you.

This chapter shows you how to

Gather user input and send it by email

Use PHP conditional logic to check required fields

Display errors without losing user input

Filter out suspect material

Avoid email header injection attacks

Process multiple-choice form elements

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

326

The flow of events shown in Figure 11-1 is controlled by a series of conditional statements

(see “Making decisions” in the previous chapter). The PHP script will be in the same page

as the form, so the first thing that it needs to know is if the form has been submitted. If it

has, the contents of the $_POST array will be checked. If it’s OK, the email will be sent and

an acknowledgement displayed, else a series of error messages will be displayed. In other

words, everything is controlled by if . . . else statements.

Activating the form

As you saw in Chapter 9, data entered into the form can be retrieved by using

print_r($_POST); to inspect the contents of the $_POST array. This is one of PHP’s socalled

superglobal arrays. They’re such an important part of PHP, it’s worth pausing for a

moment to take a look at what they do.

Getting information from the server with PHP superglobals

Superglobal arrays are built-in associative arrays that are automatically populated with

really useful information. They all begin with a dollar sign followed by an underscore. The

most important superglobal arrays are as follows:

$_POST: This contains values sent through the post method.

$_GET: This contains values sent through the get method or a URL query string.

$_SERVER: This contains information stored by the web server, such as file name,

pathname, hostname, etc.

$_SESSION: This stores information that you want to preserve so that it’s available

to other pages. Sessions are covered in Chapter 15.

$_FILES: This contains details of file uploads. File uploads are not covered in this

book. See http://www.php.net/manual/en/features.file-upload.php or my book PHP

Solutions: Dynamic Web Design Made Easy (friends of ED, ISBN-13: 978-1-59059-

731-6) for details.

The keys of $_POST and $_GET are automatically derived from the names of form elements.

Let’s say you have a text input field called address in a form; PHP automatically creates

an array element called $_POST['address'] when the form is submitted by the post

method or $_GET['address'] if you use the get method. As Figure 11-2 shows,

$_POST['address'] contains whatever value a visitor enters in the text field, enabling you

to display it onscreen, insert it in a database, send it to your email inbox, or do whatever

you want with it.

Figure 11-2. The $_POST array automatically creates variables with the same name

and value as each form field.

USING PHP TO PROCESS A FORM

327

11

It’s important to realize that variables like $_POST['address'] or $_GET['address'] don’t

exist until the form has been submitted. So, before using $_POST or $_GET variables in a

script, you should always test for their existence with isset() or wrap the entire section

of script in a conditional statement that checks whether the form has been submitted.

You’ll see both of these techniques in action in this chapter and the rest of this book.

You may come across old scripts or tutorials that tell you PHP automatically creates variables

with the same name as form fields. In this example, it would be $address. This relies

on a setting called register_globals being on. The default for register_globals has

been off since 2002, but some hosting companies still switch it back on. You should never

rely on register_globals, as it leaves your site wide open to malicious attacks. Moreover,

register_globals has been removed from PHP 6, so scripts that rely on this setting will

break in the future.

Some scripts also recommend the use of $_REQUEST, which is another PHP superglobal. It’s

much less secure. Always use $_POST for data submitted using the post method and $_GET

for the get method or when values are passed through a query string at the end of a URL.

Dreamweaver code hints make it easy to type the names of superglobals. As soon as you

type the underscore after the dollar sign, it displays a list of the array names; and for arrays

such as $_SERVER with predefined elements, a second menu with the predefined elements

is also displayed, as you’ll see when you start scripting the form.

Sending email

To send an email with PHP, you use the mail() function, which takes up to five arguments,

all of them strings, as follows:

The address(es) of the recipient(s)

The subject line

The message body

A list of other email headers

Additional parameters

The first three arguments are required. Email addresses in the first argument can be in

either of the following formats:

‘user@example.com’

‘Some Guy <user2@example.com>’

To send to more than one address, use a comma-separated string like this:

‘user@example.com, another@example.com, Some Guy <user2@example.com>’

Don’t forget that PHP is case sensitive. All superglobal array names are written in

uppercase. $_Post or $_Get, for example, won’t work.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

328

The second argument is a string containing the subject line. The third argument is the message

body, which must be a single string, regardless of how long it is. I’ll come back to the

final two arguments later.

It’s important to understand that mail() isn’t an email program. It passes data to the web

server’s mail transport agent (MTA). PHP’s responsibility ends there. It has no way of knowing

if the email is delivered to its destination. It doesn’t handle attachments or HTML

email. Still, it’s efficient and easy to use.

These days, most Internet service providers (ISPs) enforce simple mail transfer protocol

(SMTP) authentication before accepting email for relay from another machine. However,

since mail() communicates directly with the MTA on the same machine, no authentication

is required. More important, since mail() doesn’t normally need to authenticate

itself, it’s not capable of doing so when you attempt to use it in your local test environment.

What happens is that mail() tries to hand the message to your local MTA. If it finds

one, and your ISP accepts the message, you’re in luck. More often than not, it can’t find

one or the ISP rejects the mail without authentication. On Windows, you can edit php.ini

(see Chapter 3) and change the SMTP command from localhost to the address of your

ISP’s outgoing mail server (it’s usually something like smtp.example.com). On a Mac, PHP

uses the MTA built into OS X, so there is no need to edit php.ini.

Scripting the feedback form

To make things simple, I’m going to break up the PHP script into several sections. To start

off, I’ll concentrate on the text input fields and sending their content by email. Then I’ll

move onto validation and the display of error messages before showing you how to handle

checkboxes, radio buttons, menus, and multiple-choice lists.

Most readers should be able to send a simple email after the following exercise, but even

if you are successful, you should implement the server-side validation described later in

the chapter. This is because, without some simple security precautions, you risk turning

your online forms into a spam relay. Your hosting company might suspend your site or

close down your account altogether. In fact, many hosting companies implement security

measures that prevent the first version of the mail script from working. However, you

should have a fully working form by the end of this chapter.

This involves a lot of hand-coding—much more than you’ll encounter in later chapters.

Even if you don’t want to do a lot of PHP programming, it’s important to get a feel for the

flow of a script, as this will help you customize the Dreamweaver code once you start

working with a database. The script uses a lot of PHP’s built-in functions. I explain the

important ones but don’t always go into the finer points of how they work. The idea is to

give you a working solution, rather than overwhelm you with detail. The finished code for

each section is in examples/ch11; and in the next chapter, I’ll show you how to put the

main part of the script in an external file so that you can reuse it with other forms without

the need to hand-code everything from scratch every time.

Local testing with mail() is very much hit and miss. The most reliable approach is to

test mail-processing scripts on your remote server. The instructions in this chapter

explain which parts of the script can be tested locally.

USING PHP TO PROCESS A FORM

329

11

The starting point is in feedback_01.php in examples/ch11. It’s the same as feedback_

fieldsets.php from Chapter 9 but with the small block of PHP code removed from the

bottom of the page. If you want to use your own form, I suggest that you remove any

client-side validation from it, as the client-side validation makes it difficult to check

whether the more important server-side validation with PHP is working correctly. You can

add the client-side validation back at the final stage.

1. Copy feedback_01.php from examples/ch11 to workfiles/ch11, and save it as

feedback.php. Also make sure you have a copy of contact.css in your styles

folder.

2. Open contact.css, and add the following style rule (it already exists in the version

in the examples/styles folder):

.warning {

font-weight:bold;

color:#FF0000;

}

This adds a class called warning, which displays text in bold red. Close

contact.css.

3. Open feedback.php in Split view, click anywhere inside the form, and use the Tag

selector to select the entire form. This should bring the opening tag of the form

into view in the Code view section of the Document window. Click inside Code view

so that your cursor is between the quotes of the action attribute. Although you

can set the action for the form through the Property inspector, doing so in Code

view greatly reduces the possibility of making a mistake.

4. Select the PHP tab on the Insert bar, and click the Echo button (the menu option is

Insert ä PHP Objects ä Echo). This will insert a pair of PHP tags followed by echo

between the quotes of the action attribute, and Dreamweaver positions your cursor

in the correct place to start typing, as shown in the following screenshot:

Processing and acknowledging the message

This is a long script. Give yourself plenty of time to absorb the details. You can

check your progress at each stage with the files in examples/ch11. The final

code is in feedback_12.php.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

330

5. To set the action attribute of the form to process itself, you need to use a variable

from the $_SERVER superglobal array. As noted before, superglobals always begin

with $_, so type just that at the current position. Dreamweaver automatically presents

you with a pop-up menu containing all the superglobals, as shown here:

You can navigate this pop-up menu in several ways: continue typing “server” in

either uppercase or lowercase until SERVER is highlighted or use your mouse or

the arrow keys to highlight it. Then double-click or press Enter/Return.

Dreamweaver will present you with another pop-up menu. Locate PHP_SELF as

shown, and either double-click or press Enter/Return:

6. Although it’s not strictly necessary for a single command, get into the habit of ending

all statements with a semicolon, and type one after the closing square bracket

(]) of the superglobal variable that’s just been entered. The code in the opening

<form> tag should look like this (new code is highlighted in bold type):

<form action=”<?php echo $_SERVER['PHP_SELF']; ?>” method=”post” å

name=”form1″ id=”form1″>

The predefined variable $_SERVER['PHP_SELF'] always contains the name of the

current page, so using echo between the quotes of the action attribute automatically

sets it to the current page, making this a self-processing form. As you saw

in Chapter 9, leaving out the value of action also results in the form attempting

to process itself. So, technically speaking, this isn’t 100 percent necessary, but

it’s common practice in PHP scripts, and it’s useful to know what

$_SERVER['PHP_SELF'] does.

7. You now need to add the mail-processing script at the top of the page. As you saw

in Chapter 9, the $_POST array contains not only the data entered into the form but

also the name and value of the submit button. You can use this information to

determine whether the submit button has been clicked. From this point onward, it

will be easier to work in Code view. Switch to Code view, and insert the following

block of PHP code immediately above the DOCTYPE declaration:

USING PHP TO PROCESS A FORM

331

11

<?php

if (array_key_exists(‘send’, $_POST)) {

// mail processing script

echo ‘You clicked the submit button';

}

?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” å

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

This uses the PHP function array_key_exists() to check whether the $_POST array

contains a key called send, the name attribute of the form submit button. If you

don’t want to type out the function name yourself, you can press Ctrl+Space to

bring up an alphabetical list of all PHP functions. Type just the first few letters, and

then use your arrow keys to select the right one. When you press Tab or

Enter/Return, Dreamweaver finishes the rest of the typing and pops up a code hint.

Alternatively, just type the function name directly, and the code hint appears as

soon as you enter the opening parenthesis after array_key_exists, as shown here:

The mixed datatype refers to the fact that array keys can be either numbers or

strings. In this case, you are using a string, so enclose send in quotes, and then after

a comma, type $_POST. Because it’s a superglobal, you are presented with the

same pop-up menu as in step 5. If you select POST, Dreamweaver assumes that you

want to add the name of an array key and will automatically add an opening square

bracket after the T. On this occasion, you want to check the whole $_POST array,

not just a single element, so remove the bracket by pressing Backspace. Make sure

that you use two closing parentheses—the first belongs to the function

array_key_exists(), and the second encloses the condition being tested for by

the if statement.

If the send array key exists, the submit button must have been clicked, so any script

between the curly braces is executed. Otherwise, it’s ignored. Don’t worry that echo

will display text above the DOCTYPE declaration. It’s being used for test purposes

only and will be removed eventually.

8. Save feedback.php, and test it in a browser. It should look no different from

before.

Remember, an if statement doesn’t always need to be followed by else or

elseif. When the condition of a solitary if statement isn’t met, PHP simply

skips to the next block of code.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

332

9. Click the Send comments button. A message should appear at the top of the page

saying “You clicked the submit button.”

10. Reload the page without using the browser’s Reload button. Click inside the

address bar, and press Enter/Return. The message should disappear. This confirms

that any code inside the curly braces runs only if the submit button has been

clicked.

11. Change the block of code that you entered in step 7 so that it looks like this:

<?php

if (array_key_exists(‘send’, $_POST)) {

//mail processing script

$to = ‘me@example.com'; // use your own email address

$subject = ‘Feedback from Essential Guide';

// process the $_POST variables

$name = $_POST['name'];

$email = $_POST['email'];

$comments = $_POST['comments'];

// build the message

$message = “Name: $name\n\n”;

$message .= “Email: $email\n\n”;

$message .= “Comments: $comments”;

// limit line length to 70 characters

$message = wordwrap($message, 70);

// send it

$mailSent = mail($to, $subject, $message);

}

?>

The code that does the processing consists of five stages. The first two lines assign

your email address to $to and the subject line of the email to $subject.

Next, $_POST['name'], $_POST['email'], and $_POST['comments'] are reassigned

to ordinary variables to make them easier to handle.

The shorter variables are then used to build the body of the email message, which

must consist of a single string. As you can see, I have used the combined concatenation

operator (.=) to build the message and escape sequences to add new line

characters between each section (see “Adding to an existing string” and “Using

escape sequences in strings” in Chapter 10).

Once the message body is complete, it’s passed to the wordwrap() function, which

takes two arguments: a string and an integer that sets the maximum length of each

line. Although most mail systems will accept longer lines, it’s recommended to limit

each line to 70 characters.

USING PHP TO PROCESS A FORM

333

11

After the message has been built and formatted, the recipient’s address, subject

line, and body of the message are passed to the mail() function. There is nothing

magical about the variable names $to, $subject, and $message. I chose them to

describe what each one contains, making much of the script self-commenting.

The mail() function returns a Boolean value (true or false) indicating whether it

succeeded. By capturing this value as $mailSent, you can use it to redirect the user

to another page or change the contents of the current one.

12. For the time being, let’s keep everything in the same page, because the rest of the

chapter will add further refinements to the basic script. Scroll down, and insert the

following code just after the page’s main heading (new code is highlighted in bold):

<h1>Contact us</h1>

<?php

if ($_POST && !$mailSent) {

?>

<p class=”warning”>Sorry, there was a problem sending your message.

Please try later.</p>

<?php

}

elseif ($_POST && $mailSent) {

?>

<p><strong>Your message has been sent. Thank you for your feedback.

</strong></p>

<?php } ?>

<p>We welcome feedback from visitors . . .</p>

Many beginners mistakenly think that you need to use echo or print to display

XHTML inside a PHP block. However, except for very short pieces of code, it’s more

efficient to switch back to XHTML, as I’ve done here. Doing so avoids the need to

worry about escaping quotes. Also, Dreamweaver code hints and automatic tag

completion speed things up for you. As soon as you type a space after <p in the

first paragraph, Dreamweaver pops up a code hint menu like this:

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

334

Select class. As soon as you do so, Dreamweaver checks the available classes in the

attached style sheet and pops up another code hint menu, as shown in the next

screenshot, so you can choose warning.

This makes coding much quicker and more accurate. Dreamweaver’s context sensitivity

means you get the full range of XHTML code hints only when you’re in a section

of XHTML code. When you’re inside a block of PHP code, you get a list of

XHTML tags when you type an opening angle bracket, but there are no attribute

hints or auto-completion. So it makes more sense to use PHP for the conditional

logic but keep the XHTML separate. The only thing you need to watch carefully is

that you balance the opening and closing curly braces correctly. I’ll show you how

to do that in “Using balance braces” a little later in the chapter.

So what does this code do? It may look odd if you’re not used to seeing scripts that

mix XHTML with PHP logic, but it can be summarized like this:

<h1>Contact us</h1>

<?php

if ($_POST && !$mailSent) {

// display a failure message

}

elseif ($_POST && $mailSent) {

// display an acknowledgment

}

?>

<p>We welcome feedback from visitors . . .</p>

Both parts of the conditional statement check the Boolean values of $_POST and

$mailSent. Although the $_POST array is always set, it doesn’t contain any values

unless the form has been submitted. Since PHP treats an empty array as false (see

“The truth according to PHP” in Chapter 10), you can use $_POST on its own to test

whether a form has been submitted. So the code in both parts of this conditional

statement is ignored when the page first loads.

However, if the form has been submitted, $_POST equates to true, so the next

condition is tested. The exclamation mark in front of $mailSent is the negative

operator, making it the equivalent of not $mailSent. So, if the email hasn’t been

sent, both parts of the test are true, and the XHTML containing the error message

is displayed. However, if $mailSent is true, the XHTML containing the acknowledgment

is displayed instead.

USING PHP TO PROCESS A FORM

335

11

13. Save feedback.php, and switch to Design view. The top of the page should now

look like this:

There are three gold shields indicating the presence of PHP code, and both the

error and acknowledgement messages are displayed. You need to get used to this

sort of thing when designing dynamic pages.

If you don’t see the gold shields, refer to “Passing information through a hidden

field” in Chapter 9 for details of how to control invisible elements in Design view.

14. To see what the page looks like when the PHP is processed, click the Live Data view

button (see alongside) to the right of the Design view button on the Document

toolbar.

If you have coded everything correctly, the error message and acknowledgement

should disappear. Click the Live Data view button to toggle it off again.

If you got a PHP error message, read “Using balance braces,” and then check your

code against feedback_02.php.

Using Balance Braces

Even if you didn’t encounter a problem in the preceding exercise, Balance Braces is a tool

that you definitely need to know about. Like quotes, curly braces must always be in matching

pairs, but sometimes the opening and closing braces can be dozens, even hundreds, of

lines apart. If one of a pair is missing, your script will collapse like a house of cards. Balance

Braces matches pairs in a highly visual way, making troubleshooting a breeze.

Let’s take a look at the code in step 12 that I suspect will trip many people up. I deliberately

removed an opening curly brace at the end of line 41 in the following screenshot.

That triggered a parse error, which reported an unexpected closing curly brace on line 45.

Now, that could mean either of the following:

The script in step 11 is theoretically all you need to send email from an online form.

Don’t be tempted to leave it at that. Without the security checks described in the rest

of the chapter, you run the risk of turning your website into a spam relay.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

336

There’s a missing opening brace to match the closing one.

There’s an extra closing brace that shouldn’t be there.

The way to resolve the problem is to place your cursor anywhere between a pair of curly

braces, and click the Balance Braces button in the Coding toolbar. This highlights the code

between the matching braces. I started by placing my cursor on line 37. As you can see, it

highlighted all the code between the braces on lines 35 and 40.

Next, I positioned my cursor on line 43. When I clicked the Balance Braces button again,

nothing was highlighted, and my computer just beeped. So there was the culprit. All I

needed to work out was where the opening brace should go. My first test showed that I

had a logical block on lines 35–40, so it was just a process of elimination tracking down the

missing brace. If the problem had been an extra curly brace that shouldn’t have been

there, the code would have been highlighted, giving me a clear indication of where the

block ended.

Although it can’t tell you whether your code logic is right or where a missing brace should

go, you’ll find this tool a great timesaver. It works not only with braces, but also with

square brackets and parentheses. Just position your cursor inside any curly brace, square

bracket, or parenthesis, and click the Balance Braces button to find the other one of the

pair. You may need to test several blocks to find the cause of a problem, but it’s an excellent

way of visualizing code blocks and the branching logic of your scripts.

You can also access Balance Braces through the Edit menu, and if you’re a keyboard shortcut

fan, the combination is Ctrl+’/Cmd+’ (single quote).

Testing the feedback form

Assuming that you now have a page that displays correctly in Live Data view, it’s time to

test it. As mentioned earlier, testing mail() in a local PHP testing environment is unreliable,

so I suggest that you upload feedback.php to a remote server for the next stage of

testing. Once you have established that the mail() function is working, you can continue

testing locally.

Upload feedback.php and contact.css to your remote server. Enter some text in the

Name, Email, and Comments fields. Make sure that your input includes at least an apostrophe

or quotation mark, and click Send comments. The form should clear, and you should

see a confirmation message, as in Figure 11-3.

USING PHP TO PROCESS A FORM

337

11

Figure 11-3. Confirmation that the mail() function has passed the message to the server’s mail

transport agent

Shortly afterward, you should receive the message in your inbox. Most of the time, it

should work, but there are several things that might go wrong. The next section should

help you resolve the problem.

Troubleshooting mail()

If you don’t receive anything, the first thing to check is your spam trap, because the email

may appear to come from an unknown or a suspicious source. For example, it may appear

to come from Apache or a mysterious nobody (the name often used for web servers).

Don’t worry about the odd name; that will be fixed soon. The main thing is to check that

the mail is being sent correctly.

If you see an error message saying that the From header wasn’t set or that sendmail_from

isn’t defined in php.ini, again that’s nothing to worry about and will be fixed shortly. Keep

building the script as described in each section, and I’ll tell you when you can test your

page on the remote server again.

Some hosting companies now make it a requirement to use the fifth argument to mail()

to ensure that it comes from an entrusted user. If you don’t receive mail or see a PHP error

message, check your hosting company’s instructions and find out the exact format

required for the fifth parameter. It normally consists of -f followed (without a space) by

your own email address, all enclosed in quotes. You’ll see later how to add it to your code.

Again, keep building the script as described in each section.

Some mail servers object to new line characters that are not accompanied by carriage

returns. If you receive a warning that includes SMTP server response: 451, change the

escape sequences in the section that builds the message like this:

$message = “Name: $name\r\n\r\n”;

$message .= “Email: $email\r\n\r\n”;

$message .= “Comments: $comments”;

Getting rid of unwanted backslashes

Some day back in the mists of time, the PHP development team had the “brilliant” idea of

creating a feature known as magic quotes . . . only it wasn’t so brilliant after all. When

inserting data into a database, it’s essential to escape single and double quotes. So the idea

of magic quotes was to make life simpler for beginners by doing this automatically for all

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

338

data passed through the $_POST and $_GET arrays, and cookies. While this seemed like a

good idea at the time, it has caused endless problems. To cut a long story short, magic

quotes are being officially phased out of PHP (they’ll be gone in PHP 6), but they’re still

enabled on a lot of shared servers. You will know if your server uses them if your test email

has backslashes in front of any apostrophes or quotes, as shown in Figure 11-4.

Figure 11-4. PHP magic quotes insert unwanted

backslashes in the email.

Dreamweaver’s server behaviors automatically handle magic quotes by stripping the backslashes,

if necessary, and preparing data for database input. However, when you’re handcoding

like this, you need to deal with the backslashes yourself.

I have created a Dreamweaver snippet, so that you can drop a ready-made script into any

page that needs to get rid of unwanted backslashes. It automatically detects whether

magic quotes are enabled, so you can use it safely on any server. If magic quotes are on, it

removes the backslashes. If magic quotes are off, it leaves your data untouched. It’s part of

a collection of snippets that I’ve created for this book and packaged as a Dreamweaver

extension so they can be installed in a single operation.

1. If Dreamweaver is open, you will need to close and restart the program after

installing the snippets, so save any files that are open. Access the Extension

Manager by choosing Manage Extensions from either the Commands or Help menu.

If Dreamweaver is closed, launch Adobe Extension Manager CS3 from Start ä All

Programs (Windows) or Finder ä Applications (Mac).

2. Select Dreamweaver CS3 in the drop-down menu on the Extension Manager toolbar,

and choose File ä Install Extension, or click the Install button. Alternatively,

press Ctrl+O/Cmd+O (capital “o,” not zero).

3. In the dialog box that opens, navigate to egdwcs3_snippets.mxp in the tools

folder of the download files, and click Install.

4. After the extension has been installed, close Dreamweaver if it’s open. The snippets

will be in the PHP-DWCS3 folder of the Snippets panel when you next open the

program.

Installing the PHP snippets collection

USING PHP TO PROCESS A FORM

339

11

1. Open feedback.php in Code view. Position your

cursor on line 4, just under the mail processing

script comment, and insert a couple of blank

lines.

Move your cursor onto one of the blank lines,

and open the Snippets panel by clicking the

Snippets tab in the Files panel group or selecting

Window ä Snippets. On Windows, you can also

use the keyboard shortcut Shift+F9, but this

doesn’t work on the Mac version.

Highlight the new POST stripslashes snippet in

the PHP-DWCS3 folder, as shown alongside, and

double-click it, or click the Insert button at the

bottom of the panel.

2. This inserts the following block of code into your page:

// remove escape characters from $_POST array

if (get_magic_quotes_gpc()) {

function stripslashes_deep($value) {

$value = is_array($value) ? array_map(‘stripslashes_deep’, å

$value) : stripslashes($value);

return $value;

}

$_POST = array_map(‘stripslashes_deep’, $_POST);

}

Lying at the heart of this code is the PHP function stripslashes(), which removes

the escape backslashes from quotes and apostrophes. Normally, you just pass the

string that you want to clean up as the argument to stripslashes().

Unfortunately, that won’t work with an array. This block of code checks whether

magic quotes have been turned on; and if they have, it goes through the $_POST

array and any nested arrays, cleaning up your text for display either in an email or

in a web page.

3. Save feedback.php, and send another test email that includes apostrophes and

quotes in the message. The email that you receive should be nicely cleaned up. This

won’t work yet if you weren’t able to send the first test email.

If you have any problems, check your page against feedback_03.php.

Using the POST stripslashes snippet

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

340

Making sure required fields aren’t blank

When required fields are left blank, you don’t get the information you need, and the user

may never get a reply, particularly if contact details have been omitted. The following

instructions make use of arrays and the foreach loop, both of which are described in

Chapter 10. So, if you’re new to PHP, you might find it useful to refer to the relevant sections

in the previous chapter before continuing.

In this part of the script, you create three arrays to hold details of variables you expect to

receive from the form, those that are required, and those that are missing. This not only

helps identify any required items that haven’t been filled in; it also adds an important

security check before passing the user input to a loop that converts the names of $_POST

variables to shorter ones that are easier to handle.

1. Start by creating two arrays: one listing the name attribute of each field in the form

and the other listing all required fields. Also, initialize an empty array to store the

names of required fields that have not been completed. For the sake of this

demonstration, make the email field optional, so that only the name and comments

fields are required. Add the following code just before the section that processes

the $_POST variables:

$subject = ‘Feedback from Essential Guide';

// list expected fields

$expected = array(‘name’, ‘email’, ‘comments’);

// set required fields

$required = array(‘name’, ‘comments’);

// create empty array for any missing fields

$missing = array();

// process the $_POST variables

2. At the moment, the $_POST variables are assigned manually to variables that use

the same name as the $_POST array key. With three fields, manual assignment is

fine, but it becomes a major chore with more fields. Let’s kill two birds with one

stone by checking required fields and automating the naming of the variables at

the same time. Replace the three lines of code beneath the $_POST variables comment

as follows:

// process the $_POST variables

foreach ($_POST as $key => $value) {

// assign to temporary variable and strip whitespace if not an array

$temp = is_array($value) ? $value : trim($value);

// if empty and required, add to $missing array

if (empty($temp) && in_array($key, $required)) {

array_push($missing, $key);

}

// otherwise, assign to a variable of the same name as $key

elseif (in_array($key, $expected)) {

Checking required fields

USING PHP TO PROCESS A FORM

341

11

${$key} = $temp;

}

}

// build the message

If studying PHP code makes your brain hurt, you don’t need to worry about how

this works. As long as you create the $expected, $required, and $missing arrays in

the previous step, you can just copy and paste the code for use in any form.

So what does it do? In simple terms, this foreach loop goes through the $_POST

array, strips out any whitespace from user input, and assigns its contents to a variable

with the same name (so $_POST['email'] becomes $email, and so on). If a

required field is left blank, its name attribute is added to the $missing array.

The code uses several built-in PHP functions, all of which have intuitive names:

is_array() tests whether a variable is an array.

trim() trims whitespace from both ends of a string.

empty() tests whether a variable contains nothing or equates to false.

in_array() checks whether the first argument is part of the array specified in

the second argument.

array_push() adds a new element to the end of an array.

At this stage, you don’t need to understand how each function works, but you

can find details in the PHP online documentation at http://www.php.net/manual/en/

index.php. Type the name of the function in the search for field at the top right of

the page (see Figure 11-5), and click the right-facing arrow alongside function list.

The PHP documentation has many practical examples showing how functions and

other features are used.

Figure 11-5. Refer often to the excellent PHP online documentation, and your skills will increase

rapidly.

Why is the $expected array necessary? It’s to prevent an attacker from injecting

other variables in the $_POST array in an attempt to overwrite your default

values. By processing only those variables that you expect, your form is much

more secure. Any spurious values are ignored.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

342

3. You want to build the body of the email message and send it only if all required

fields have been filled in. Since $missing starts off as an empty array, nothing is

added to it if all required fields are completed, so empty($missing) is true. Wrap

the rest of the script in the opening PHP code block like this:

// go ahead only if all required fields OK

if (empty($missing)) {

// build the message

$message = “Name: $name\n\n”;

$message .= “Email: $email\n\n”;

$message .= “Comments: $comments”;

// limit line length to 70 characters

$message = wordwrap($message, 70);

// send it

$mailSent = mail($to, $subject, $message);

if ($mailSent) {

// $missing is no longer needed if the email is sent, so unset it

unset($missing);

}

}

}

This ensures that the mail is sent only if nothing has been added to $missing.

However, $missing will be used to control the display of error messages in the

main body of the page, so you need to get rid of it if the mail is successfully sent.

This is done by using unset(), which destroys a variable and any value it contains.

4. Let’s turn now to the main body of the page. You need to display a warning if anything

is missing. Amend the conditional statement at the top of the page content

like this:

<h1>Contact us</h1>

<?php

if ($_POST && isset($missing) && !empty($missing)) {

?>

<p class=”warning”>Please complete the missing item(s) indicated.</p>

<?php

}

elseif ($_POST && !$mailSent) {

?>

<p class=”warning”>Sorry, there was a problem sending your message.

Please try later.</p>

This adds a new condition. The isset() function checks whether a variable exists. If

$missing doesn’t exist, that means that all required fields were filled in and the

email was sent successfully, so the condition fails, and the script moves on to consider

the elseif condition. However, if all required fields were filled in, but there

was a problem sending the email, $missing still exists, so you need to make sure it’s

empty. An exclamation mark is the negative operator, so !empty means “not empty.”

USING PHP TO PROCESS A FORM

343

11

On the other hand, if $missing exists and isn’t empty, you know that at least one

required field was omitted, so the warning message is displayed.

I’ve placed this new condition first. The $mailSent variable won’t even be set if any

required fields have been omitted, so you must test for $missing first.

5. To make sure it works so far, save feedback.php, and load it in a browser. You don’t

need to upload it to your remote server, because you want to test the message

about missing items. Don’t fill in any fields. Just click Send comments. The top of

the page should look like this (check your code against feedback_04.php if

necessary):

6. To display a suitable message alongside each missing required field, add a PHP code

block to display a warning as a <span> inside the <label> tag like this:

<label for=”name”>Name: <?php

if (isset($missing) && in_array(‘name’, $missing)) { ?>

<span class=”warning”>Please enter your name</span><?php } ?>

</label>

Since the $missing array is created only after the form has been submitted, you

need to check first with isset() that it exists. If it doesn’t exist—such as when the

page first loads or if the email has been sent successfully—the <span> is never displayed.

If $missing does exist, the second condition checks if the $missing array

contains the value name. If it does, the <span> is displayed as shown in Figure 11-6.

7. Insert a similar warning for the comments field like this:

<label for=”comments”>Comments: <?php

if (isset($missing) && in_array(‘comments’, $missing)) { ?>

<span class=”warning”>Please enter your comments</span><?php } ?>

</label>

The PHP code is the same except for the value you are looking for in the $missing

array. It’s the same as the name attribute for the form element.

8. Save feedback.php, and test the page again locally by entering nothing into any

of the fields. The page should look like Figure 11-6. Check your code against

feedback_05.php if you encounter any problems.

9. Try one more test. Open Code view, and amend the line that sends the email like

this:

$mailSent = false; // mail($to, $subject, $message);

This temporarily sets the value of $mailSent to false and comments out the code

that actually sends the email.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

344

Figure 11-6. The PHP script displays alerts if required information is missing, even when

JavaScript is disabled.

10. Reload feedback.php into your browser, and type something in the Name and

Comments fields before clicking Send comments. This time you should see the message

telling you there was a problem and asking you to try later.

11. Reverse the change you made in step 9 so that the code is ready to send the email.

Preserving user input when a form is incomplete

Imagine you have just spent ten minutes filling in a form. You click the submit button, and

back comes the response that a required field is missing. It’s infuriating if you have to fill

in every field all over again. Since the content of each field is in the $_POST array, it’s easy

to redisplay it when an error occurs.

When the page first loads or the email is successfully sent, you don’t want anything to

appear in the input fields. But you do want to redisplay the content if a required field is

missing. So that’s the key: if the $missing variable exists, you want the content of each

field to be redisplayed. You can set default text for a text input field by setting the value

attribute of the <input> tag.

At the moment, the <input> tag for name looks like this:

<input name=”name” type=”text” class=”textInput” id=”name” />

To add the value attribute, all you need is a conditional statement that checks whether

$missing exists. If it does, you can use echo to display value=”” and put the value held in

$_POST['name'] between the quotes. It sounds simple enough, but this is one of those situations

where getting the right combination of quotes can drive you mad. It’s made even

worse by the fact that the user input in the text field might also contain quotes. Figure 11-7

shows what happens if you don’t give quotes in user input special treatment. The browser

finds the first matching quote and throws the rest of the input away.

USING PHP TO PROCESS A FORM

345

11

Figure 11-7. Quotes within user input need special treatment before form fields can be

redisplayed.

You might be thinking that this is a case where magic quotes would be useful.

Unfortunately, they won’t work either. If you don’t use the POST stripslashes snippet, this

is what you get instead:

Magic quotes work only with input into a database (and not very well, either, which is why

they are being phased out). The browser still sees the first matching quote as the end of

the value attribute. The solution is simple: convert the quotes to the HTML entity equivalent

(&quot;), and PHP has a function called—appropriately—htmlentities(). Passing the

$_POST array element to this function converts all characters (except space and single

quote) that have an HTML entity equivalent to that entity. As a result, the content is no

longer truncated. What’s cool is that the HTML entity &quot; is converted back to double

quotes when the form is resubmitted, so there’s no need for any further conversion.

That’s the theory—now let’s put it into practice.

1. Amend the <input> tag for the Name text field like this:

<input name=”name” type=”text” class=”textInput” id=”name”

<?php if (isset($missing)) {

echo ‘value=”‘.htmlentities($_POST['name']).'”‘;

} ?>

/>

Creating sticky form fields

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

346

This code is quite short, but the line inside the curly braces contains a tricky combination

of quotes and periods. The first thing to realize is that there’s only one

semicolon—right at the end—so the echo command applies to the whole line. You

can break down the rest of the line into three sections, as follows:

‘value=”‘.

htmlentities($_POST['name'])

.'”‘

The first section outputs value=” as text and uses the concatenation operator (a

period—see “Joining strings together” in Chapter 10) to join it to the next section,

which passes $_POST['name'] to the htmlentities() function. The final section

uses the concatenation operator again to join the next string, which consists solely

of a double quote. So, if $missing has been set, and $_POST['name'] contains Joe,

you’ll end up with this inside the <input> tag:

<input name=”name” type=”text” class=”textInput” id=”name” å

value=”Joe” />

2. Amend the email input field in the same way, using $_POST['email'].

3. The comments text area needs to be handled slightly differently, because

<textarea> tags don’t have a value attribute. You place the PHP block between

the opening and closing tags of the text area like this (new code is shown in bold):

<textarea name=”comments” id=”comments” cols=”45″ rows=”5″><?php

if (isset($missing)) {

echo htmlentities($_POST['comments']);

} ?></textarea>

It’s important to position the opening and closing PHP tags right up against the

<textarea> tags. If you don’t, you’ll get unwanted whitespace inside the text area.

4. Save feedback.php, and test the page. If the first test message earlier in the chapter

was successful, you can upload it to your remote server. If any required fields

are omitted, the form displays the original content along with any error messages.

However, if the form is correctly filled in, the email is sent, an acknowledgment is

displayed, and the input fields are cleared.

If your remote server test didn’t succeed earlier in the chapter, just test locally.

You’ll probably get a PHP error message if all required fields are filled in, but that’s

nothing to worry about. We’re almost at the stage to get your remote server

working.

You can check your code with feedback_06.php.

By default, htmlentities() leaves single quotes untouched. Since I chose to wrap the

value attribute in double quotes, this doesn’t matter. To convert a single quote to an

HTML entity as well, pass ENT_QUOTES (all uppercase) as a second argument to

htmlentities() like this: htmlentities($_POST['name'], ENT_QUOTES).

USING PHP TO PROCESS A FORM

347

11

Filtering out potential attacks

A particularly nasty exploit known as email header injection emerged in mid-2005. It seeks

to turn online forms into spam relays. A simple way of preventing this is to look for the

strings “Content-Type:”, “Cc:”, and “Bcc:”, as these are email headers that the attacker

injects into your script in an attempt to trick it into sending HTML email with copies to

many people. If you detect any of these strings in user input, it’s a pretty safe bet that

you’re the target of an attack, so you should block the message. An innocent message may

also be blocked, but the advantages of stopping an attack outweigh that small risk.

In this section, we’ll create a pattern to check for suspect phrases, and pass the form input

to a custom-built function that checks for any matches. The function is one of the snippets

that you installed earlier in the chapter, so the most complex part of the coding is already

done for you. If a match is found, a conditional statement prevents the email from being

sent.

1. PHP conditional statements rely on a true/false test to determine whether to execute

a section of code. So the way to filter out suspect phrases is to create a

Boolean variable that is switched to true as soon as one of those phrases is

detected. The detection is done using a search pattern or regular expression. Insert

the code for both of these just above the section that processes the $_POST

variables:

// create empty array for any missing fields

$missing = array();

// assume that there is nothing suspect

$suspect = false;

// create a pattern to locate suspect phrases

$pattern = ‘/Content-Type:|Bcc:|Cc:/i';

// process the $_POST variables

The string assigned to $pattern will be used to perform a case-insensitive search

for any of the following: “Content-Type:”, “Bcc:”, or “Cc:”. It’s written in a format

called Perl-compatible regular expression (PCRE). The search pattern is enclosed in

a pair of forward slashes, and the i after the final slash makes the pattern case

insensitive.

2. You can now use $pattern to filter out any suspect user input from the $_POST

array. At the moment, each element of the $_POST array contains only a string.

However, multiple-choice form elements, such as checkboxes, return an array of

results. So you need to tunnel down any subarrays and check the content of each

element separately. In the snippets collection that you installed earlier in the chapter,

you’ll find a custom-built function to do precisely that.

Insert two blank lines immediately after the $pattern variable from step 1. Then

open the Snippets panel, and double-click Suspect pattern filter in the PHP-DWCS3

folder to insert the code shown here in bold:

Blocking emails that contain specific phrases

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

348

// create a pattern to locate suspect phrases

$pattern = ‘/Content-Type:|Bcc:|Cc:/i';

// function to check for suspect phrases

function isSuspect($val, $pattern, &$suspect) {

// if the variable is an array, loop through each element

// and pass it recursively back to the same function

if (is_array($val)) {

foreach ($val as $item) {

isSuspect($item, $pattern, $suspect);

}

}

else {

// if one of the suspect phrases is found, set Boolean to true

if (preg_match($pattern, $val)) {

$suspect = true;

}

}

}

3. I won’t go into detail about how this code works. All you need to know is that calling

the isSuspect() function is very easy. You just pass it three values: the $_POST

array, the pattern, and the $suspect Boolean variable. Insert the following code

immediately after the code in the previous step:

// check the $_POST array and any subarrays for suspect content

isSuspect($_POST, $pattern, $suspect);

4. If any suspect phrases are detected, the value of $suspect changes to true, so you

need to set $mailSent to false and delete the $missing array to prevent the email

from being sent and to display an appropriate message in the form. There’s also no

point in processing the $_POST array any further. Wrap the code that processes the

$_POST variables in the second half of an if . . . else statement like this:

if ($suspect) {

$mailSent = false;

unset($missing);

}

else {

// process the $_POST variables

foreach ($_POST as $key => $value) {

// assign to temporary variable and strip whitespace if not an array

$temp = is_array($value) ? $value : trim($value);

// if empty and required, add to $missing array

if (empty($temp) && in_array($key, $required)) {

array_push($missing, $key);

}

// otherwise, assign to a variable of the same name as $key

elseif (in_array($key, $expected)) {

${$key} = $temp;

USING PHP TO PROCESS A FORM

349

11

}

}

}

Don’t forget the extra curly brace to close the else statement.

5. If suspect content is detected, you don’t want the code that builds and sends the

email to run, so amend the condition in the opening if statement like this:

// go ahead only if not suspect and all required fields OK

if (!$suspect && empty($missing)) {

// build the message

6. Save feedback.php, and check your code against feedback_07.php.

Because the if statement in step 4 sets $mailSent to false and unsets $missing if it

detects any suspect pattern, the code in the main body of the page displays the same message

that’s displayed if there’s a genuine problem with the server. A neutral message

reveals nothing that might assist an attacker. It also avoids offending anyone who may

have innocently used a suspect phrase.

You can use isSuspect() with any array or pattern, but it always requires the following

three arguments:

An array that you want to filter. If the array contains other arrays, the function burrows

down until it finds a simple value against which it can match the pattern.

A regular expression containing the pattern(s) you want to search for. There are

two types of regular expression, Perl-compatible (PCRE), and Portable Operating

System Interface (POSIX). You must use a PCRE. This function won’t work with a

POSIX regular expression. A good online source is http://regexlib.com.

A Boolean variable set to false. If the pattern is found, the value is switched to

true.

Safely including the user’s address in email headers

Up to now, I’ve avoided using one of the most useful features of the PHP mail() function:

the ability to add extra email headers with the optional fourth argument. A popular use of

extra headers is to incorporate the user’s email address into a Reply-To header, which

enables you to reply directly to incoming messages by clicking the Reply button in your

email program. It’s convenient, but it provides a wide open door for an attacker to supply

a spurious set of headers. With the isSuspect() function in place, you can block attacks

and safely use the fourth argument with the mail() function.

The most important header that you should add is From. Email sent by mail() is often

identified as coming from nobody@servername. Adding the From header not only identifies

your mail in a more user-friendly way, but it also solves the problem you might have

encountered on the first test of there being no setting for sendmail_from in php.ini.

You can find a full list of email headers at http://www.faqs.org/rfcs/rfc2076, but some of the

most well-known and useful ones enable you to send copies of an email to other

addresses (Cc and Bcc) or to change the encoding (often essential for languages other

than Western European ones).

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

350

Like the body of the email message, headers must be passed to the mail() function as a

single string. Each new header, except the final one, must be on a separate line terminated

by a carriage return and new line character. This means using the \r and \n escape

sequences in double-quoted strings.

Let’s say you want to send copies of messages to other departments, plus a copy to

another address that you don’t want the others to see. This is how you pass those additional

email headers to mail():

$headers = “From: Essential Guide<feedback@example.com>\r\n”;

$headers .= “Cc: sales@example.com, finance@example.com\r\n”;

$headers .= ‘Bcc: secretplanning@example.com';

$mailSent = mail($to, $subject, $message, $headers);

The default encoding for email is iso-8859-1 (English and Western European). If you want

to use a different encoding, set the Content-Type header. For Unicode (UTF-8), set it

like this:

$headers = “Content-Type: text/plain; charset=utf-8\r\n”;

The web page that the form is embedded in must use the same encoding (usually set in a

<meta> tag).

Hard-coded additional headers present no security risk, but anything that comes from user

input must be filtered before it’s used.

This section incorporates the user’s email address into a Reply-To header. Although

isSuspect() should sanitize user input, it’s worth subjecting the email field to a more rigorous

check to make sure that it doesn’t contain illegal characters or more than one

address.

1. At the moment, the $required array doesn’t include email, and you may be happy

to leave it that way. So, to keep the validation routine flexible, it makes more sense

to handle the email address outside the main loop that processes the $_POST array.

If email is required but has been left blank, the loop will have already added

email to the $missing array, so the message won’t get sent anyway.

If it’s not a required field, you need to check $email only if it contains something.

So you need to wrap the validation code in an if statement that uses

!empty().

Insert the code shown in bold after the loop that processes the $_POST array.

// otherwise, assign to a variable of the same name as $key

elseif (in_array($key, $expected)) {

${$key} = $temp;

}

}

}

Adding email headers and automating the reply address

USING PHP TO PROCESS A FORM

351

11

// validate the email address

if (!empty($email)) {

}

// go ahead only if not suspect and all required fields OK

if (!$suspect && empty($missing)) {

2. Position your cursor on the blank line between the curly braces of the conditional

statement you have just inserted. Open the Snippets panel, and double-click Check

email PCRE in the PHP-DWCS3 folder. This inserts the following regular expression:

$checkEmail = ‘/^[^@]+@[^\s\r\n\'";,@%]+$/';

Designing a regular expression to recognize a valid-looking email address is notoriously

difficult. So, instead of striving for perfection, $checkEmail, takes a negative

approach by rejecting characters that are illegal in an email address. However, to

make sure that the input resembles an email address in some way, it checks for an

@ mark surrounded by at least one character on either side.

3. Now add the code shown in bold to check $email against the regular expression:

// validate the email address

if (!empty($email)) {

// regex to ensure no illegal characters in email address

$checkEmail = ‘/^[^@]+@[^\s\r\n\'";,@%]+$/';

// reject the email address if it doesn’t match

if (!preg_match($checkEmail, $email)) {

$suspect = true;

$mailSent = false;

unset($missing);

}

}

The conditional statement uses the preg_match(), which takes two arguments: a

PCRE and the string that you want to check. If a match is found, the function

returns true. Since it’s preceded by the negative operator, the condition is true if

the contents of $email don’t match the PCRE.

If there’s no match, $suspect is set to true, $mailSent is set to false, and

$missing is unset. This results in the neutral alert saying that the message can’t be

sent and clears the form. This runs the risk that someone who has accidentally

mistyped the email address will be forced to enter everything again. If you don’t

want that to happen, you can omit unset($missing);. However, the PCRE detects

illegal characters that are unlikely to be used by accident, so I have left it in.

Many popular PHP scripts use pattern-matching functions that begin with ereg.

These work only with POSIX regular expressions. I recommend that you always

use the PCRE functions that begin with preg_. Not only is PCRE more efficient,

there’s a strong likelihood that support for the ereg family of functions will be

removed from a future version of PHP.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

352

4. Now add the additional headers to the email. Place them immediately above the

call to the mail() function, and add $headers as the fourth argument like this:

// limit line length to 70 characters

$message = wordwrap($message, 70);

// create additional headers

$headers = ‘From: Essential Guide<feedback@example.com>';

if (!empty($email)) {

$headers .= “\r\nReply-To: $email”;

}

// send it

$mailSent = mail($to, $subject, $message, $headers);

If you don’t want email to be a required field, there’s no point in using a nonexistent

value in the Reply-To header, so I have wrapped it in a conditional statement.

Since you have no way of telling whether the Reply-To header will be created, it

makes sense to put the carriage return and new line characters at the beginning of

the second header. It doesn’t matter whether you put them at the end of one

header or the start of the next one, as long as a carriage return and new line separates

each header. For instance, if you wanted to add a Cc header, you could do it

like this:

$headers = From: Essential Guide<feedback@example.com>\r\n”;

$headers .= ‘Cc: admin@example.com';

if (!empty($email)) {

$headers .= “\r\nReply-To: $email”;

}

Or like this:

$headers = ‘From: Essential Guide<feedback@example.com>';

$headers .= “\r\nCc: admin@example.com;

if (!empty($email)) {

$headers .= “\r\nReply-To: $email”;

}

If your hosting company requires you to supply the fifth argument to mail() for

security reasons, you should add it after the headers. Normally, it takes the form of

-f followed by your email address like this:

$mailSent = mail($to,$subject,$message,$headers,’-fdavid@example.com’);

Use this fifth argument only if instructed to do so by your hosting company.

5. Save feedback.php, upload it to your remote server, and test the form. When you

receive the email, click the Reply button in your email program, and you should see

the address that you entered in the form automatically entered in the recipient’s

address field. You can check your code against feedback_08.php.

USING PHP TO PROCESS A FORM

353

11

Handling multiple-choice form elements

You now have the basic knowledge to process text input from an online form and email it

to your inbox. The principle behind handling multiple-choice elements is exactly the same:

the name attribute is used as the key in the $_POST array. However, as you saw in Chapter

9, checkboxes and multiple-choice lists don’t appear in the $_POST array if nothing has

been selected, so they require different treatment.

The following exercises show you how to handle each type of multiple-choice element. If

you’re feeling punch drunk at this stage, come back later to study how to handle multiplechoice

elements when you need to incorporate them into a script of your own.

In Chapter 9, I showed you how to create a checkbox group, which stores all checked

values in a subarray of the $_POST array. However, the subarray isn’t even created if all

boxes are left unchecked. So you need to use isset() to check the existence of the subarray

before attempting to process it.

1. Add the name of the checkbox group to the $expected array like this:

$expected = array(‘name’, ‘email’, ‘comments’, ‘interests’);

In the form, interests is followed by square brackets like this:

<input type=”checkbox” name=”interests[]” . . .

The square brackets in the form tell the $_POST array to store all checked values in

a subarray called $_POST['interests']. However, don’t add square brackets to

interests in the $expected array. Doing so would bury the checked values in a

subarray one level deeper than you want. See “Using arrays to store multiple

values” in Chapter 10 for a reminder of how arrays are created.

2. If you want the checkboxes to be required, add the name of the checkbox group to

the $required array in the same way.

3. Because the checkbox array might never be created, you need to set a default

value before attempting to build the body of the email. The following code in bold

goes in the section that prepares the message prior to sending it:

// go ahead only if not suspect and all required fields OK

if (!$suspect && empty($missing)) {

// set default values for variables that might not exist

$interests = isset($interests) ? $interests : array(‘None selected’);

Getting data from checkboxes

When building your own forms, don’t forget to add the name of each text field to the

$expected array. Also add the name of required fields to the $required array, and add

a suitable alert as described in “Checking required fields.”

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

354

This uses the conditional operator (see “Using the conditional operator” in

Chapter 10) to check whether $interests has been set. If it has, the existing array

of checked values is reassigned to $interests. Otherwise, a single-element array

containing the string None selected is created and assigned to $interests. It

needs to be an array, even though it contains only one element, because the next

step expects an array.

4. To extract the values of the checkbox array, you can use the oddly named

implode() function, which joins array elements. It takes two arguments: a string to

be used as a separator and the array. So, implode(‘, ‘, $interests) joins the elements

of $interests as a comma-separated string. Add the following code shown

in bold to the script that builds the body of the email:

$message .= “Comments: $comments\n\n“;

$message .= ‘Interests: ‘.implode(‘, ‘, $interests);

Note that I added two new line characters at the end of the line that adds the

user’s comments to the email. On the following line, I put Interests: in single

quotes because there are no variables to be processed, and I used the concatenation

operator to join the result of implode(‘, ‘, $interests) to the end of the

email message. You cannot include a function inside a string.

5. The next listing shows the code for the first two checkboxes in the body of the

page. The code in bold preserves the user’s selections if a required field is missing.

<p>

<input name=”interests[]” type=”checkbox” id=”interests-classical” å

value=”Classical concerts”

<?php

$OK = isset($_POST['interests']) ? true : false;

if ($OK && isset($missing) && in_array(‘Classical concerts’, å

$_POST['interests'])) {

echo ‘checked=”checked”‘;

} ?>

/>

<label for=”interests-classical”>Classical concerts</label>

</p>

<p>

<input name=”interests[]” type=”checkbox” id=”interests-rock” å

value=”Rock/pop”

<?php

if ($OK && isset($missing) && in_array(‘Rock/pop’, å

$_POST['interests'])) {

echo ‘checked=”checked”‘;

} ?>

/>

<label for=”interests-rock”>Rock/pop events</label>

</p>

USING PHP TO PROCESS A FORM

355

11

The code in the first checkbox contains the following line:

$OK = isset($_POST['interests']) ? true : false;

This checks whether $_POST['interests'] exists (it won’t if the user didn’t select

any checkboxes). If it does, $OK is set to true.

The PHP code for each checkbox tests three conditions: the value of $OK, whether

the $missing variable exists, and whether the value of the checkbox is in the

$_POST['interests'] subarray. If all are true, echo inserts checked=”checked”

into the <input> tag. (If you’re using HTML instead of XHTML, use just checked.)

Although it looks like a lot of hand-coding, you can copy and paste the code after

creating the first one. Just change the first argument of in_array() to the value of

the checkbox. The complete code is in feedback_09.php.

If you want to make the checkbox group required, add an alert in the same way as

described in “Checking required fields” earlier in the chapter.

Drop-down menus created with the <select> tag normally allow the user to pick only one

option from several. One item is always selected, even if it’s only the first one inviting the

user to select one of the others. Setting the value of this first <option> to 0 has the advantage

that the empty() function, which is used to check required fields, returns true when

0 is passed to it either as a number or string.

1. Add the name of the drop-down menu to the $expected array. Also add it to the

$required array if you want a choice to be compulsory.

2. Add the value of the drop-down menu to the email message like this:

$message .= ‘Interests: ‘.implode(‘, ‘, $interests).”\n\n”;

$message .= “Visited: $visited”;

One option will always be selected, so this doesn’t need special treatment.

However, change the value of the first <option> tag in the menu to No response if

it isn’t a required field. Leave it as 0 if you want the user to make a selection.

3. The following code shows the first two items of the drop-down menu in

feedback.php. The PHP code highlighted in bold assumes that the menu has been

made a required field and resets the selected option if an incomplete form is submitted.

When the page first loads, the $_POST array contains no elements, so you

can select the first <option> by testing for !$_POST. Once the form is submitted,

the $_POST array always contains an element from a drop-down menu, so you don’t

need to test for it.

<label for=”visited”>How often have you been to London? <?php

if (isset($missing) && in_array(‘visited’, $missing)) { ?>

<span class=”warning”>Please select a value</span><?php } ?></label>

<select name=”visited” id=”visited”>

<option value=”0″

<?php

if (!$_POST || $_POST['visited'] == ‘0’) {

Getting data from a drop-down menu

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

356

echo ‘selected=”selected”‘;

} ?>

>– Select one –</option>

<option value=”Never”

<?php

if (isset($missing) && $_POST['visited'] == ‘Never’) {

echo ‘selected=”selected”‘;

} ?>

>Never been</option>

. . .

</select>

When setting the second condition for each <option>, it’s vital that you use the

same spelling and mixture of uppercase and lowercase as contained in the value

attribute. PHP is case sensitive and won’t match the two values if there are any

differences.

The finished code is in feedback_10.php.

Multiple-choice lists are similar to checkboxes: they allow the user to choose zero or more

items, so the result is stored in an array. If no items are selected, the $_POST array contains

no reference to the list, so you need to take that into consideration both in the form and

when processing the message.

1. Add the name of the multiple-choice list to the $expected array. Also add it to the

$required array if you want a choice to be compulsory.

2. In the code that processes the message, set a default value for a multiple-choice list

in the same way as for an array of checkboxes.

$interests = isset($interests) ? $interests : array(‘None selected’);

$views = isset($views) ? $views : array(‘None selected’);

3. When building the body of the message, use implode() to create a comma-separated

string, and add it to the message like this:

$message .= “Visited: $visited\n\n“;

$message .= ‘Impressions of London: ‘.implode(‘, ‘, $views);

4. The following code shows the first two items from the multiple-choice list in

feedback.php. The code works in an identical way to the checkboxes, except that

you echo ‘selected=”selected”‘ instead of ‘checked=”checked”‘. You can reuse

$OK here, because its value is reset by the code in the first <option> tag.

<select name=”views[]” size=”6″ multiple=”multiple” id=”views”>

<option value=”Vibrant/exciting”

<?php

$OK = isset($_POST['views']) ? true : false;

if ($OK && isset($missing) && in_array(‘Vibrant/exciting’, å

$_POST['views'])) {

Getting data from a multiple-choice list

USING PHP TO PROCESS A FORM

357

11

echo ‘selected=”selected”‘;

} ?>

>A vibrant, exciting city</option>

<option value=”Good food”

<?php

if ($OK && isset($missing) && in_array(‘Good food’, å

$_POST['views'])) {

echo ‘selected=”selected”‘;

} ?>

>A great place to eat</option>

. . .

</select>

The completed code is in feedback_11.php.

If you want to make the multiple-choice list required, add an alert in the same way as

described in “Checking required fields” earlier in the chapter.

Radio button groups allow you to pick only one value. This makes it easy to retrieve the

selected one. All buttons in the same group must share the same name attribute, so the

$_POST array contains the value attribute of whichever radio button is selected. However,

if you don’t set a default button in your form, the radio button group’s $_POST array element

remains unset.

1. Add the name of the radio button group to the $expected array.

2. If you haven’t set a default button and you want a choice to be compulsory, also

add it to the $required array. This isn’t necessary if a default choice is set in the

form.

3. If you haven’t set a default button, you need to set a default value before building

the body of the email message. You do this in a similar way to a checkbox group or

multiple-choice list, but since a radio button group can have only one value, you

set the default as a string, not an array, as shown in this example:

$radio = isset($radio) ? $radio : ‘Not selected';

4. Add the value of the radio button group to the body of the message like this:

$message .= ‘Impressions of London: ‘.implode(‘, ‘, $views).”\n\n”;

$message .= “Subscribe: $subscribe”;

5. Assuming a default button has been defined, amend the radio button group like

this:

<input type=”radio” name=”subscribe” id=”subscribe-yes” value=”y”

<?php

if (isset($missing) && $_POST['subscribe'] == ‘y’) {

echo ‘checked=”checked”‘;

Getting data from radio button groups

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

358

} ?>

/>

<label for=”subscribe-yes”>Yes</label>

<input name=”subscribe” type=”radio” id=”subscribe-no” value=”n”

<?php

if (!$_POST || isset($missing) && $_POST['subscribe'] == ‘n’) {

echo ‘checked=”checked”‘;

} ?>

/>

<label for=”subscribe-no”>No</label>

The conditional statement for the default radio button begins with !$_POST ||,

which means “if the $_POST array is empty or . . .” So, if the form hasn’t been submitted,

or if the user has selected No and the form is incomplete, this button will

be checked.

The completed script is in feedback_12.php.

If no default button has been defined, add the same $OK check as for a checkbox group or

multiple-choice list in the first <input> tag, as well as in the conditional statement for each

radio button. You need to add a required alert only if no default has been defined in the

original form.

Redirecting to another page

Everything has been kept within the same page, even if the message is sent successfully. To

redirect the visitor to a different page, change the code at the end of the messageprocessing

section like this:

// send it

$mailSent = mail($to, $subject, $message, $headers);

if ($mailSent) {

// redirect the page with a fully qualified URL

header(‘Location: http://www.example.com/thanks.php&#8217;);

exit;

}

}

}

The HTTP/1.1 protocol stipulates a fully qualified URL for a redirect command, although

most browsers will perform the redirect correctly with a relative pathname.

When using the header() function, you must be careful that no output is sent to the

browser before PHP attempts to call it. If, when testing your page, you see an error message

warning you that headers have already been sent, check there are no characters,

including new lines, spaces, or tabs ahead of the opening PHP tag.

USING PHP TO PROCESS A FORM

359

11

Time for a breather . . .

If that was your first encounter with PHP, your head will probably be reeling. This has been

a tough chapter. In the next chapter, you’ll adapt this script so that it can be reused as an

external file with most forms. The external file never changes, and the hand-coding is cut

down to about a dozen lines. I could, of course, have given you the external file without

explanation, but if you don’t understand the code, you can’t adapt it to your own requirements.

Even if you never write an original PHP script of your own, you should know what

the code in your page is doing. If you don’t, you’re storing up trouble for the future.

What makes PHP pages dynamic—and so powerful—is the fact that your code makes decisions,

even though you have no way of knowing in advance what is going to be input into

the form. The Dreamweaver code that you’ll encounter in subsequent chapters tries to

anticipate a lot of these unknown factors, but its beauty lies in the fact that it’s configurable.

If you know how to hand-code, you can get Dreamweaver to do a lot of the hard

work for you, and then take it beyond the basics.

However, it’s no fun spending all your time churning out code. Life becomes simpler if you

can reuse code. So that’s what the next chapter is about—saving time with includes and

Dreamweaver templates.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

360

12 WORKING WITH PHP INCLUDES

AND TEMPLATES

To give a unified look to a website, most pages have common elements, such as a header,

navigation menu, and footer. Nobody likes repeating work just for the sake of it, so the

ability to build page templates has long been one of Dreamweaver’s most popular features.

All common features can be defined and locked, but Dreamweaver propagates to all

child pages any changes that you make to the master template. The great disadvantage is

that every time you make a change all the affected pages must be uploaded again to your

remote server. On a large site, this can be a major undertaking.

Wouldn’t it be wonderful if you could make changes to just a single page and have them

reflected through the site in the same way as CSS? Well, with PHP includes, you can. As the

name suggests, the contents of an include file are included and treated as an integral part

of the page. They can contain anything that you would normally find in a PHP page: plain

text, XHTML, and PHP code. The file name extension doesn’t even need to be .php,

although for security it’s common practice to use it.

Dreamweaver makes working with includes easy thanks to its ability to display the contents

of an include in Design view (or Live Data view for dynamic content). Many people

find includes so useful that they stop using templates. Nevertheless, templates can be useful,

particularly for small sites, so this chapter covers both approaches.

In this chapter, you’ll learn how to

Use PHP includes for common page elements

Store frequently used code in the Snippets panel

Apply CSS to page fragments with Design Time Style Sheets

Export a navigation menu to an external file

Adapt the mail processing script to work with other forms

Avoid the “headers already sent” error with includes

Use Dreamweaver templates in a PHP site

To start with, let’s take a quick look at how you create a PHP include.

Including text and code from other files

The ability to include code from other files is a core part of PHP. All that’s necessary is to

use one of PHP’s include commands and tell the server where to find the file.

Introducing the PHP include commands

PHP has four separate commands for creating an include: include(), include_once(),

require(), and require_once(). Why so many? And what’s the difference?

They all do the same thing, but “require” is used in the sense of “mandatory”; everything

comes to a grinding halt if the external file is missing or can’t be opened. The “include”

pair of commands, on the other hand, soldier bravely on. The purpose of _once is to prevent

variables being accidentally overwritten. The PHP engine uses the first instance it

encounters and ignores any duplicates. If in doubt about which to use, choose

include_once() or require_once(). Using them does no harm and could avoid problems.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

364

Telling PHP where to find the external file

The include commands take a single argument: a string containing the path of the external

file. While this sounds simple enough, it confuses many Dreamweaver users. PHP looks for

the external file in what’s known as the include_path. By default, this always includes the

current directory (folder), and PHP expects either a relative or an absolute path. It won’t

work with a path relative to the site root.

If Links relative to is set to Document in the Local Info category of your site definition (see

Figure 12-1), Dreamweaver automatically uses the correct path for include files. However,

if you have selected Site root as your default style for links, includes won’t work unless you

override the default setting to change the path to a document-relative one or take alternative

measures to set the include_path.

Figure 12-1. Dreamweaver’s site definition dialog box lets you specify the default

format of internal links.

A practical exercise should clarify the situation.

In this exercise, you’ll see what happens if you use the wrong type of path for an include

file. You’ll also learn how to override the default setting, so that you can use includes successfully

even if your site definition specifies using links relative to the site root.

1. Create a new subfolder called includes in your workfiles folder, and copy

include.txt from examples/includes to the new folder.

2. Go to File ä New. Select Blank Page and PHP as the Page Type. Choose any of the

predefined layouts. The one I chose was 2 column fixed, left sidebar. This is only

going to be a test page, so you can leave Layout CSS on Add to Head. Click Create

and save the file as include_test.php in workfiles/ch12.

3. Position your cursor at the beginning of the first paragraph under the Main Content

headline. Press Enter/Return to insert a new paragraph, and then press your up

keyboard arrow to move the insertion point into the empty paragraph.

4. Select the PHP tab on the Insert bar, and click the Include button as shown in the

following screenshot (alternatively use the menu option Insert ä PHP Objects ä

Include). Dreamweaver opens Split view, inserts a PHP code block complete with an

include() command, and positions the insertion point between the parentheses,

ready for you to enter the path of the external file.

Including a text file

WORKING WITH PHP INCLUDES AND TEMPLATES

365

12

5. The path needs to be a string, so enter a quotation mark (I prefer a single quote,

but it doesn’t matter, as long as the closing quote matches). Dreamweaver’s syntax

coloring turns all the subsequent code red, but this reverts to normal once you

have finished. Position your mouse pointer over the insertion point, and right-click

to bring up a context menu. Select Code Hint Tools ä URL Browser, as shown here:

6. This places a tiny Browse icon at the insertion point like this:

7. Click the Browse icon to open the Select File dialog box. Navigate to the

workfiles/includes folder, and select include.txt. Before clicking OK, check the

setting of Relative to at the bottom of the dialog box. It displays Document or Site

Root, depending on the default in your site definition (see Chapter 4 and Figure 12-1).

If necessary, change it to Site Root, as shown here, and click OK:

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

366

8. Type a closing quote after the path that has just been entered into the include()

command. Syntax coloring turns the rest of the code back to its normal color—a

useful reminder of the importance of matching quotes. Move your cursor further

along the line to remove the &nbsp; just before the closing </p> tag.

9. Click inside Design view. The gold PHP shield should disappear and be replaced by

the content of the external text file. Magic . . . well, not quite.

10. Save include_test.php, and press F12/Opt+F12 to view it in a browser. You should

see something like Figure 12-2.

Figure 12-2. If PHP can’t find the include file, it displays ugly warning messages.

The first warning says there was no such file or directory, but of course, there is.

The second warning gives a cryptic clue as to why PHP can’t open the file. The

include_path is where PHP looks for include files. The default value on most web

servers is . (a period), which is shorthand for the current working directory, and

either the main PHP folder or pear (PEAR—the PHP Extension and Application

Repository—is a library of extensions to PHP). PHP doesn’t understand a leading

forward slash as meaning the site root, so it starts from the current folder and ends

up in a nonexistent part of the site.

11. Go back to the Dreamweaver Document window, and remove the current path;

right-click between the quotes, and use the URL Browser to navigate to

include.txt again, but this time make sure that Relative to is set to Document. Save

include_test.php, and reload it in a browser. The content of the include file

should now be correctly displayed, as shown in Figure 12-3.

WORKING WITH PHP INCLUDES AND TEMPLATES

367

12

Figure 12-3. The include file is displayed correctly when a relative path is used.

12. Go back to Dreamweaver, and change the command from include to require like

this:

<?php require(‘../includes/include/txt’); ?>

13. Save the page, and load it into a browser. It should look identical to Figure 12-3.

14. Change the path to point to a nonexistent file, such as includ.txt. When you save

the page and view it in a browser, it should look similar to Figure 12-2, but instead

of the second warning, you should see Fatal error. The other difference is that

there’s no text after the error message. As explained in “Understanding PHP error

messages” in Chapter 10, any output preceding a fatal error is displayed, but once

the error is encountered, everything else is killed stone dead.

Using site-root-relative links with includes

As you have just seen, PHP cannot find include files referenced by a site-root-relative link.

My recommendation is that, if you have selected links relative to the site root as your

default, you simply select Relative to Document in the Select File dialog box (as described in

step 10 of the preceding exercise) when creating an include.

Nevertheless, there are a couple of alternatives if you have a pressing reason for wanting

to use links relative to the site root. The problem is that they don’t work on all servers.

The virtual() function accepts both document-relative and site-root-relative paths and

can be used as a direct replacement for include() and require(). It works only when PHP

is run as an Apache module.

$_SERVER['DOCUMENT_ROOT'] is a predefined PHP variable that contains the path of the

server’s root folder, so adding it to the beginning of a site-root-relative link has the effect

of turning it into an absolute path. The following works on most servers:

<?php include($_SERVER['DOCUMENT_ROOT'].‘/workfiles/includes/ å

include/txt’); ?>

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

368

Unfortunately, $_SERVER['DOCUMENT_ROOT'] isn’t supported by IIS running PHP in CGI

mode.

To check whether your server supports either method, run server_check.php in

examples/ch12. If both are supported, you should see output similar to this.

If neither is supported and you still want to use site-root-relative links, you need to define

a constant containing the path to the site root. A constant is like a variable, except that

once defined in a script, its value cannot be changed. Constants don’t begin with a dollar

sign, and by convention, they are always in uppercase. You define a constant like this:

define(‘SITE_ROOT’, ‘C:\inetpub\wwwroot\egdwcs3′);

You could then use SITE_ROOT with a site-root-relative link like this:

<?php include(SITE_ROOT.‘/workfiles/includes/include/txt’); ?>

The disadvantage with this approach is that you need to include the definition of the constant

in every page that uses includes.

Lightening your workload with includes

So far, you have seen only a fairly trivial use of an include to insert a block of text inside a

paragraph. This might be useful in a situation where you want to change the content of

part of a page on a frequent basis without going to the bother of building a databasedriven

content management system. A much more practical use of includes is for content

that appears on many pages, for example a navigation menu or footer. Any changes made

to the include file are immediately reflected throughout the site.

Choosing the right file name extension for include files

As I explained at the beginning of the chapter, the external file doesn’t need to have a

.php file name extension. Many developers use .inc as the default file name extension to

make it clear that the file is an include. Although this a common convention, Dreamweaver

doesn’t automatically recognize an .inc file as containing PHP code, so you don’t get code

The restriction on site-root-relative links applies only to the include command. Inside

include files, all links should be site-root-relative. Document-relative links inside an

include file will be broken if the file is included at a different level of the site hierarchy.

See “Creating and editing a template-based page” later in the chapter.

WORKING WITH PHP INCLUDES AND TEMPLATES

369

12

hints or syntax coloring. More importantly, browsers don’t understand .inc files. So, if

anybody accesses an .inc file directly through a browser (as opposed to it being included

as part of a PHP page), everything is displayed as plain text.

This is a potential security risk if you put passwords or other sensitive information in external

files. One way around this problem is to store include files outside the server root

folder. Many hosting companies provide you with a private folder, which cannot be

reached by a browser. As long as the PHP script knows where to find the external file and

has permission to access it, include files can be outside the server root. However, this

creates problems for Dreamweaver site management.

A simpler, widely adopted solution is to use .inc.php as the file name extension. Browsers

and servers treat only the final .php as the file name extension and automatically pass

the file to the PHP engine if requested directly. The .inc is simply a reminder to you as the

developer that this is an include file.

As long as you store passwords and other sensitive information as PHP variables within PHP

code blocks, and use .php as the final file name extension, your data cannot be seen by

anyone accessing the page directly in a browser (of course, it will be revealed if your code

uses echo or print to display that information, but I assume that you have the sense not

to do that).

Displaying XHTML output

When PHP includes an external file, it automatically treats the contents of the external file

as plain text or XHTML. This means that you can cut a section out of an existing page built

in XHTML and convert it into an include file. In order to preserve your sanity, it’s important

to put only complete, logical elements in external files. Putting the opening part of a <div>

in one external file and the closing part in another file is a disaster waiting to happen. It

becomes impossible to keep track of opening and closing tags, and Dreamweaver is likely

to start trying to replace what it regards as missing tags.

Usually, I find the best approach is to build the complete page first, and then convert common

elements into include files.

This exercise shows you how to extract the menu from the “Stroll along the Thames” site

in Chapter 7 and convert it into an include file.

1. Copy stroll_horiz.html from examples/ch07 to workfiles/ch12. Also make

sure you have the dependent files: styles/stroll_horiz.css, SpryAssets/

SpryMenuBar.js, and SpryAssets/SpryMenuBarHorizontal_stroll.css.

2. Save stroll_horiz.html as stroll_horiz.php. You need to change the file name

extension so that the PHP engine knows to process it and include the external files

you are about to create. Test the page in a browser to make sure that it displays

correctly. It should look like Figure 12-4.

Converting a navigation menu into an include

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

370

Figure 12-4. The menu is the same on every page of the site, so it is a prime candidate for an

include file.

3. Create a new PHP file, and save it in the workfiles/includes folder as

menu.inc.php. You don’t need one of the CSS layouts, as you need a completely

blank page. Switch to Code view in menu.inc.php, and delete everything, including

the DOCTYPE declaration. There should be nothing left in the page.

4. Switch to stroll_horiz.php in the Document window. Click anywhere inside the

navigation menu, and click <div#nav> in the Tag selector to select the entire menu.

Switch to Code view, and then cut the menu to your computer clipboard

(Ctrl+X/Cmd+X or Edit ä Cut).

You must be in Code view when cutting the

menu to the clipboard. If you remain in

Design view, Dreamweaver cuts all the Spryrelated

code and pastes it into the include file.

You want to move only the XHTML code and

the Spry object initialization, but they’re in

different parts of the page, so it has to be

done in two steps. Click No, if Dreamweaver

displays the warning shown alongside at any

time during the next few steps. Once you

move the initialization script, the warning

message no longer appears.

WORKING WITH PHP INCLUDES AND TEMPLATES

371

12

5. Without moving the insertion point, click the Include button on the PHP tab of the

Insert bar (or use the menu alternative). This inserts a PHP code block and positions

your cursor between the parentheses of an include() command.

6. Type a single quote, right-click, and use Code Hint Tools ä URL Browser to navigate

to menu.inc.php in the workfiles/includes folder in the same way as in

“Including a text file” earlier in the chapter. In the Select File dialog box, make sure

that Relative to is set to Document. Click OK, and type a closing quote after the path.

Save stroll_horiz.php.

7. Switch to menu.inc.php in the Document window. Make sure you are in Code view,

and paste the menu that you cut from stroll_horiz.php. (If you are in Design

view, you won’t get all the XHTML code. Always cut and paste in the same view in

Dreamweaver—Design view to Design view or Code view to Code view.)

8. Go back to stroll_horiz.php, scroll down to the bottom of the page, and cut to

your clipboard the section of code highlighted on lines 54–58 in the following

screenshot.

This is the initialization script for the Spry menu bar. Make sure that you have the

opening and closing <script> tags.

9. Paste the Spry object initialization script into menu.inc.php after the closing

</div> tag. Save menu.inc.php, and close the file.

10. Switch to Design view in stroll_horiz.php. The menu should be visible as it was

before. If you can’t see the menu, open Preferences from the Edit menu

(Dreamweaver menu on a Mac), select the Invisible Elements category, and make

sure there’s a check mark in Server-Side includes: Show contents of included file.

11. Hover your mouse pointer over the navigation menu, and click the turquoise Spry

Menu Bar tab at the top left corner (see Figure 12-5). The Property inspector recognizes

it as a server-side include (SSI) and displays the name of the file, together

with an Edit button. Clicking the Edit button opens the include file in the Document

window ready for editing.

12. Test stroll_horiz.php in a browser. It should look like Figure 12-4, and the menu

should work as before. You can check your code against stroll_horiz_menu.php

in examples/ch12 and menu.inc.php in examples/includes.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

372

Figure 12-5. When you select the contents of an include file in Design view, the Property inspector

provides a direct link to edit it.

Putting the Spry object initialization script at the end of menu.inc.php results in it being

called earlier than it was in the original page, but it’s still in the right order and doesn’t

result in invalid code. It also prevents the warning in step 4 from being displayed every

time that you open the parent page.

An added advantage is that you can edit the Spry menu through the Property inspector in

the same way as in Chapter 7. Even though the include file has no direct link to the Spry

menu bar external JavaScript file, Dreamweaver automatically finds it because the

Spry assets folder is specified in the site definition.

However, what you put in an external file doesn’t always have such benign consequences.

Avoiding problems with include files

The server incorporates the content of an include file into the page at the point of the

include command. If you pasted all the Spry-related code into menu.inc.php, rather than

just the constructor, you would end up with the link to the external style sheet within the

<body> of stroll_horiz.php. Although some browsers might render the page correctly,

<style> blocks are invalid outside the <head> of a web page. If it doesn’t break now, it

probably will sooner or later as browsers get increasingly standards-compliant.

The most common mistake with include files is adding duplicate <head> and <body> tags.

Keep your include files free of extraneous code, and make sure that when everything fits

back together that you have a DOCTYPE declaration, a single <head> and <body>, and that

everything is in the right order.

Dreamweaver depends on the DOCTYPE declaration at the top of a page to determine

whether to use XHTML rules. Code added to an include will normally use HTML style, so

when editing an include, you need to keep a close eye on what is happening in Code view.

This is why I recommend extracting code into include files only toward the end of a project

or if the external file uses mainly dynamic code.

An annoying quirk in the way Dreamweaver handles PHP includes in Design view

is that the include command must be in its own PHP code block. If you put any

other PHP code in the same block—even a comment—Dreamweaver just displays

the gold PHP shield.

WORKING WITH PHP INCLUDES AND TEMPLATES

373

12

Another common problem is a broken link in an include file. Always use site-root-relative

links inside include files. As explained in Chapter 4, site-root-relative links provide a constant

reference to a page or an asset, such as an image. If you use document-relative links

inside an include file, the relationship—and therefore the link—is broken if the file is

included at a different level of the site hierarchy than where it was originally designed.

Applying styles with Design Time Style Sheets

Although Dreamweaver displays the menu normally in stroll_horiz.php, it looks completely

different in menu.inc.php. As Figure 12-6 shows, the menu is completely unstyled;

all you can see is the underlying series of nested unordered lists. Design Time Style Sheets

let you apply the styles in an external style sheet to a page or code fragment without the

need to attach the style sheet directly to the page. As the name suggests, the style sheet is

applied only at design time; in other words, in Design view.

Figure 12-6. The include file is completely unstyled.

To apply Design Time Style Sheets to a page or an include file, select CSS Styles ä Designtime

from either the Text menu or from the context menu when right-clicking in Design

view. This opens the Design Time Style Sheets dialog box, as shown in the following

screenshot:

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

374

The dialog box has two sections. The first one, Show only at design time, lets you apply a

style sheet without attaching it to the file. The second one, Hide at design time, works with

style sheets that are attached to a file, letting you hide the effect of selected style sheets

while working in Design view. It’s particularly useful when working with style sheets for different

media, such as print and screen.

Both sections work the same way: add a style sheet to the list by clicking the plus (+)

button and navigating to the style sheet in the site file system. The rules of the CSS

cascade apply, so add multiple style sheets in the same order as to the original page. To

remove a style sheet, highlight it, and click the minus (–) button. Figure 12-7 shows

menu.inc.php after applying workfiles/styles/stroll_horiz.css and SpryAssets/

SpryMenuBarHorizontal_stroll.css as Design Time Style Sheets. It now looks the same

as in the page it was extracted from.

Figure 12-7. After applying Design Time Style Sheets, the include file looks the same as in the

original page.

With the Design Time Style Sheets applied, you can manipulate the styles of the include

file by changing the class or ID of individual elements. You can also change the style rules

in the external style sheets through the CSS Styles panel. But—and it’s a rather large

one—you should remember that the code fragment that you’re working with is no longer

in the context of its parent page. As a result, the full effect of the CSS cascade may not be

accurately reflected. Also, changes made to the external style sheet may have unexpected

consequences on other parts of your design. Although useful, Design Time Style Sheets

have their limitations.

Another drawback is that Design Time Style Sheets can be applied to only one page at a

time. There is a commercial extension available that lets you apply Design Time Style

Sheets to an entire site. See http://www.communitymx.com/abstract.cfm?cid=61265 for details.

Dreamweaver stores details of style sheets applied to a page in this way in a subfolder

called _notes. The subfolder is hidden in the Files panel but can be inspected in Windows

Explorer or Finder.

Adding dynamic code to an include

The footer of a page frequently contains details that might change, such as company

address or telephone number, making it an ideal candidate for an include file.

The footer in stroll_horiz.php contains only a copyright notice, which normally changes

only once a year, but with a little PHP magic, you can get it to update automatically at the

stroke of midnight on New Year’s Eve every year. Continue working with the files from the

previous exercise.

Automatically updating a copyright notice

WORKING WITH PHP INCLUDES AND TEMPLATES

375

12

1. Create a PHP page, and save it in workfiles/includes as footer.inc.php. Switch

to Code view and remove all code, so the file is completely blank. Switch to Design

view.

2. Open stroll_horiz.php in Design view, and click anywhere inside the copyright

notice at the bottom of the page. Select the entire footer by clicking <div#footer>

in the Tag selector, and cut it to your clipboard.

3. Without moving the insertion point, click the Include button on the PHP tab of the

Insert bar. Dreamweaver opens Split view with the cursor placed between the

parentheses of an include() block. Type a single quote, use the URL Browser as

before to insert the path to footer.inc.php, and type a closing quote.

4. Switch to footer.inc.php, and paste the contents of your clipboard into Design

view. The footer is unstyled, but if you save footer.inc.php, switch to

stroll_horiz.php, and click in Design view, you’ll see the footer properly styled as

though you had never moved it. Click the copyright notice. The entire text is

selected, and the Property inspector displays the path of the include together with

an Edit button.

5. Click the Edit button to open footer.inc.php, and switch to Code view. It contains

the following XHTML:

<div id=”footer”>

<p>&copy; Footsore in London</p>

<!– end #footer –>

</div>

6. A copyright notice should have a year. You could just type it in, but the PHP date()

function generates the current year automatically. Add the following code like this:

<p>&copy;

<?php

ini_set(‘date.timezone’, ‘Europe/London’);

echo date(‘Y’);

?>

Footsore in London</p>

Chapter 17 explains dates in PHP and MySQL in detail, but let’s take a quick look at

what’s happening here. The core part of the code is this line:

echo date(‘Y’);

This displays the year using four digits. Make sure you use an uppercase Y. If you

use a lowercase y instead, only the final two digits of the year will be displayed.

The reason for the preceding line is because PHP 5.1.0 or higher requires a valid

time-zone setting. This should be set in php.ini, but if your hosting company forgets

to do this, you may end up with ugly error messages in your page.

What if your hosting company is using an earlier version of PHP? No problem.

Earlier versions simply ignore this line.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

376

Setting the time zone like this is not only good insurance against error messages, it

also allows you to override the hosting company setting, if your host is in a different

time zone from your own. The second argument for ini_set() must be one of

the time zones listed at http://www.php.net/manual/en/timezones.php.

7. Switch to Design view, and click the Live Data view button. You should see the current

year displayed alongside the copyright symbol, as shown here.

Click the Live Data view button again to toggle it off.

8. Copyright notices normally cover a range of years, indicating when a site was first

launched. To improve the copyright notice, you need to know two things: the start

year and the current year. Change the PHP code in the paragraph like this:

<p>&copy;

<?php

ini_set(‘date.timezone’, ‘Europe/London’);

$startYear = 2007;

$thisYear = date(‘Y’);

if ($startYear == $thisYear) {

echo $startYear;

}

else {

echo “{$startYear}-{$thisYear}”;

}

?>

Footsore in London</p>

This uses simple conditional logic (if you’re new to PHP, see “Using comparisons to

make decisions” in Chapter 10, and take particular note of the use of two equal

signs in the conditional statement). The static value of $startYear is compared to

the dynamically generated value of $thisYear. If both are the same, only the start

year is displayed; if they’re different, you need to display both with a hyphen

between them.

I’ve used curly braces around the variables in the following line:

echo “{$startYear}-{$thisYear}”;

This is because they’re in a double-quoted string that contains no whitespace. The

curly braces enable the PHP engine to identify the beginning and end of the variables.

Since hyphens aren’t permitted in variable names, you could omit the curly

braces on this occasion. However, their presence makes the code easier to read.

WORKING WITH PHP INCLUDES AND TEMPLATES

377

12

9. Switch back to Design view, and toggle Live Data view on again. Assuming that you

used the current year for $startYear, you’ll see no difference, so experiment by

changing the value of $startYear and alternating between uppercase and lowercase

y in the date() function to see the different output, as shown here:

The values of $startYear, $thisYear, and the name of the copyright owner are the

only things you need to change, and you have a fully automated copyright notice.

You can check your code against footer.inc.php in examples/includes and

stroll_horiz_footer.php in examples/ch12.

Using includes to recycle frequently used PHP code

Up to now, all the examples in this chapter have shown you how to include plain text or

XHTML. The last example makes use of PHP code but is specific to one particular site.

Includes become really useful when you create PHP code that can be used in any site. A

simple example is the POST stripslashes snippet that you used in the last chapter. Instead

of putting the code directly inside your script, you could put it in an external file and use

include() to incorporate it.

Let’s take a look at the code again:

// remove escape characters from POST array

if (get_magic_quotes_gpc()) {

function stripslashes_deep($value) {

$value = is_array($value) ? array_map(‘stripslashes_deep’, å

$value) : stripslashes($value);

return $value;

}

$_POST = array_map(‘stripslashes_deep’, $_POST);

}

It contains nothing but PHP code, and the code itself consists of a conditional statement

that removes backslashes from the $_POST array if magic quotes are enabled on the server.

To use it successfully as an include, you must do the following two things:

The code in the external file must be surrounded by PHP tags. Although include()

and its related commands are part of PHP, the PHP engine treats everything in an

include file as plain text or XHTML until it encounters an opening PHP tag.

The opening tag must be matched by a closing one at or before the end of the

include file.

The code must be included at the point in the script where you want to run it. In

this respect, it’s the same as the text and XHTML includes earlier in the chapter.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

378

PHP can be used in two main ways: as a procedural language and as an object-oriented

one. In a procedural language, everything is usually in the same page and the code is

executed from top to bottom. However, to avoid the need to retype frequently used sections

of script, you can package them up as custom-built functions. An object-oriented

language takes the concept of functions much further, and packages most of the code in

libraries called classes.

That’s a vast over-simplification, but in both approaches, unless the contents of an external

file define functions or classes, the include command must come at the point in the

code where you want to run it. The POST stripslashes snippet does include the definition

of the stripslashes_deep() function, but it’s buried inside a conditional statement. So,

the snippet itself is a chunk of procedural code that must be included at the point of the

script where it’s needed.

However, you can convert the snippet into a new function called nukeMagicQuotes() like

this:

<?php

function nukeMagicQuotes() {

// remove escape characters from POST array

if (get_magic_quotes_gpc()) {

function stripslashes_deep($value) {

$value = is_array($value) ? array_map(‘stripslashes_deep’, å

$value): stripslashes($value);

return $value;

}

$_POST = array_map(‘stripslashes_deep’, $_POST);

}

}

?>

If you save this as nukeQuotes.inc.php, you can include the external file at the beginning

of your script and run this function at any stage in your script like this (you can see the

code in feedback_nuke.php in examples/ch12 and nukeQuotes.inc.php in examples/

includes):

nukeMagicQuotes();

The difference of this approach is that the include file initializes the function, but the function

doesn’t actually run until it’s called in the main body of the script. Since this particular

piece of code runs only once, there’s no immediate advantage of doing it this way.

However, let’s say that you find a way of improving this script, the changes need to be

made only in the external file, saving you the effort of hunting through every page where

it might have been used. External files can define more than one function, so you can store

frequently used functions together. In this respect, includes are the PHP equivalent of linking

external JavaScript files or style sheets.

When functions or classes are stored in an external file, the include command must

come before you use the functions or classes in your main script.

WORKING WITH PHP INCLUDES AND TEMPLATES

379

12

Although building your own function library is an important use of includes, you shouldn’t

ignore the opportunity to recycle procedural code. The next section shows you how to

adapt the mail processing script from the last chapter and make it generic, so that it can

handle the output of any feedback form.

Adapting the mail processing script as an include

The mail processing script in the last chapter performs a series of tasks, some of them specific

to the feedback form, others more generic in nature.

Analyzing the script

To make the script reusable, you need to identify what’s specific, what’s generic, and

whether any of the specific tasks can be made generic. Once you have identified the

nature of each task, you need to concentrate the generic ones into a single unit that can

be exported to an external file.

Table 12-1 lists the tasks in the order they are currently performed and identifies their

roles. You can study the code in feedback_orig.php in examples/ch12.

Table 12-1. Analysis of the mail processing script

Step Description Type

1 Check if form has been submitted Specific

2 Remove magic quotes Generic

3 Set to address and subject Specific

4 List expected and required fields Specific

5 Initialize missing array Generic

6 Filter suspect content Generic

7 Process $_POST variables and check for missing fields Generic

8 Validate email address Generic

9 Build the message body Specific

10 Create additional headers Specific

11 Send email Generic

As you can see from Table 12-1, most tasks are generic, but they don’t form a single block.

However, step 2 can easily be moved after steps 3 and 4. That leaves just steps 9 and 10

that get in the way.

Step 9 builds the body of the message, which would appear to be something that’s always

specific to each form. Let’s take another look at that part of the script:

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

380

// set default values for variables that might not exist

$interests = isset($interests) ? $interests : array(‘None selected’);

$views = isset($views) ? $views : array(‘None selected’);

// build the message

$message = “Name: $name\n\n”;

$message .= “Email: $email\n\n”;

$message .= “Comments: $comments\n\n”;

$message .= ‘Interests: ‘.implode(‘, ‘, $interests).”\n\n”;

$message .= “Visited: $visited\n\n”;

$message .= ‘Impressions of London: ‘.implode(‘, ‘, $views).”\n\n”;

$message .= “Subscribe: $subscribe”;

It doesn’t take a genius to work out that the message is built using text labels followed by

variables with the same name as the label. Since the variable names come from the name

attributes in the form, all you need is a way of displaying the name attributes as well as the

values of each input field. That’s easily done with PHP. It’s also easy to set default values for

variables that might not exist.

That leaves just step 10, the creation of additional headers. With the exception of the

return email address, it doesn’t matter when you specify the additional headers. They simply

need to be passed to the mail() function in step 11. So you can move the creation of

most headers to the form-specific section at the beginning of the script. Table 12-2 shows

the revised order of tasks.

Table 12-2. The revised mail processing script

Step Description Type

1 Check if form has been submitted Specific

2 Set to address and subject Specific

3 Set form-specific email headers Specific

4 List expected and required fields Specific

5 Remove magic quotes Generic

6 Initialize missing array Generic

7 Filter suspect content Generic

8 Process $_POST variables and check for missing fields Generic

9 Validate email address Generic

10 Build the message body Generic

11 Add return email address to headers Generic

12 Send email Generic

WORKING WITH PHP INCLUDES AND TEMPLATES

381

12

Building the message body with a generic script

Loops and arrays take a lot of the hard work out of PHP scripts, although they can be difficult

to understand when you’re new to PHP. You may prefer just to use the completed

script, but if you’re interested in the details, take a look at the following code, and I’ll

explain how it works:

// initialize the $message variable

$message = ”;

// loop through the $expected array

foreach($expected as $item) {

// assign the value of the current item to $val

if (isset(${$item})) {

$val = ${$item};

}

// if it has no value, assign ‘Not selected’

else {

$val = ‘Not selected';

}

// if an array, expand as comma-separated string

if (is_array($val)) {

$val = implode(‘, ‘, $val);

}

// add label and value to the message body

$message .= ucfirst($item).”: $val\n\n”;

}

This replaces the code for step 9 that was listed in the preceding section. It begins by

initializing $message as an empty string. Everything else is inside a foreach loop

(see “Looping through arrays with foreach” in Chapter 10), which iterates through

the $expected array. This array consists of the name attributes of each form field (name,

email, etc.).

A foreach loop assigns each element of an array to a temporary variable. In this case, I

have used $item. So, the first time the loop runs, $item is name; the next time it’s email,

and so on. This means that you can use $item as the text label for each form field, but

before you can do that, you need to know whether the field contains any value. The code

that processes the $_POST variables (step 8 in the revised script) assigns the value of each

field to a variable based on its name attribute ($name, $email, etc.). The rather odd-looking

${$item} is what’s known as a variable variable (the repetition is deliberate, not a misprint).

Since the value of $item is name the first time the loop runs, ${$item} refers to

$name. On the next pass through the loop, it refers to $email, and so on.

In effect, what happens is that on the first iteration the following conditional statement

if (isset(${$item})) {

$val = ${$item};

}

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

382

becomes this

if (isset($name)) {

$val = $name;

}

If the variable doesn’t exist (which would happen if nothing was selected in a checkbox

group), the else clause assigns $val the string Not selected.

So you now have $item, which contains the label for the field, and $val, which contains

the field’s value.

The next conditional statement uses is_array() to check whether the field value is an

array (as in the case of checkboxes or a multiple-choice list). If it is, the values are converted

into a comma-separated string by implode().

Finally, the label and field value are added to $message using the combined concatenation

operator (.=). The label ($item) is passed to the ucfirst() function, which converts the

first character to uppercase. The concatenation operator (.) joins the label to a doublequoted

string, which contains a colon followed by the field value ($val) and two new line

characters.

This code handles all types and any number of form fields. All it needs is for the name

attributes to make suitable labels and to be added to the $expected array.

The following instructions show you how to adapt feedback.php from the previous chapter,

so that it can be recycled for use with most forms. If you don’t have a copy of the file

from the previous chapter, copy feedback_orig.php from examples/ch12 to workfiles/

ch12, and save it as feedback.php.

1. Create a new PHP file, and save it as process_mail.inc.php in workfiles/

includes. Switch to Code view, and strip out all existing code.

2. Insert the following code:

<?php

if (isset($_SERVER['SCRIPT_NAME']) && strpos($_SERVER['SCRIPT_NAME'],å

‘.inc.php’)) exit;

?>

This uses the predefined variable $_SERVER['SCRIPT_NAME'] and the strpos()

function to check the name of the current script. If it contains .inc.php, that

means somebody is trying to access the include file directly through a browser, so

the exit command brings the script to a halt. When accessed correctly as an

include file, $_SERVER['SCRIPT_NAME'] contains the name of the parent file, so

unless you also give that the .inc.php file name extension, the conditional statement

returns false and runs the rest of the script as normal.

Converting feedback.php to use the generic script

WORKING WITH PHP INCLUDES AND TEMPLATES

383

12

Calling process_mail.inc.php directly shouldn’t have any negative effect, but if

display_errors is enabled on your server, it generates error messages that might

be useful to a malicious attacker. This simple security measure prevents the script

running unless it’s accessed correctly.

3. Cut the POST stripslashes code from the top of feedback.php, and paste it on the

blank line before the closing PHP tag in process_mail.inc.php.

4. Leave $to, $subject, $expected, and $required in feedback.php. Cut the remaining

PHP code above the DOCTYPE declaration (DTD), except for the closing curly

brace and PHP tag. The following code should be left above the DTD in

feedback.php:

<?php

if (array_key_exists(‘send’, $_POST)) {

//mail processing script

$to = ‘me@example.com'; // use your own email address

$subject = ‘Feedback from Essential Guide';

// list expected fields

$expected = array(‘name’, ‘email’, ‘comments’, ‘interests’, å

‘visited’, ‘views’, ‘subscribe’);

// set required fields

$required = array(‘name’, ‘comments’, ‘visited’);

}

?>

5. Paste into process_mail.inc.php just before the closing PHP tag the code you cut

from feedback.php.

6. Cut the line that sets the From header, and paste it in feedback.php after the

$required array. Replace the code that builds the message with the generic version.

The full listing for process_mail.inc.php follows, with the new code highlighted

in bold:

<?php

if (isset($_SERVER['SCRIPT_NAME']) && strpos($_SERVER['SCRIPT_NAME'],å

‘.inc.php’)) exit;

// remove escape characters from POST array

if (get_magic_quotes_gpc()) {

function stripslashes_deep($value) {

$value = is_array($value) ? array_map(‘stripslashes_deep’, å

$value) : stripslashes($value);

return $value;

}

$_POST = array_map(‘stripslashes_deep’, $_POST);

}

// create empty array for any missing fields

$missing = array();

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

384

// assume that there is nothing suspect

$suspect = false;

// create a pattern to locate suspect phrases

$pattern = ‘/Content-Type:|Bcc:|Cc:/i';

// function to check for suspect phrases

function isSuspect($val, $pattern, &$suspect) {

// if the variable is an array, loop through each element

// and pass it recursively back to the same function

if (is_array($val)) {

foreach ($val as $item) {

isSuspect($item, $pattern, $suspect);

}

}

else {

// if one of the suspect phrases is found, set Boolean to true

if (preg_match($pattern, $val)) {

$suspect = true;

}

}

}

// check the $_POST array and any subarrays for suspect content

isSuspect($_POST, $pattern, $suspect);

if ($suspect) {

$mailSent = false;

unset($missing);

}

else {

// process the $_POST variables

foreach ($_POST as $key => $value) {

//assign to temporary variable and strip whitespace if not an array

$temp = is_array($value) ? $value : trim($value);

// if empty and required, add to $missing array

if (empty($temp) && in_array($key, $required)) {

array_push($missing, $key);

}

// otherwise, assign to a variable of the same name as $key

elseif (in_array($key, $expected)) {

${$key} = $temp;

}

}

}

// validate the email address

if (!empty($email)) {

// regex to identify illegal characters in email address

$checkEmail = ‘/^[^@]+@[^\s\r\n\'";,@%]+$/';

WORKING WITH PHP INCLUDES AND TEMPLATES

385

12

// reject the email address if it deosn’t match

if (!preg_match($checkEmail, $email)) {

$suspect = true;

$mailSent = false;

unset($missing);

}

}

// go ahead only if not suspsect and all required fields OK

if (!$suspect && empty($missing)) {

// initialize the $message variable

$message = ”;

// loop through the $expected array

foreach($expected as $item) {

// assign the value of the current item to $val

if (isset(${$item})) {

$val = ${$item};

}

// if it has no value, assign ‘Not selected’

else {

$val = ‘Not selected';

}

// if an array, expand as comma-separated string

if (is_array($val)) {

$val = implode(‘, ‘, $val);

}

// add label and value to the message body

$message .= ucfirst($item).”: $val\n\n”;

}

// limit line length to 70 characters

$message = wordwrap($message, 70);

// create Reply-To header

if (!empty($email)) {

$headers .= “\r\nReply-To: $email”;

}

// send it

$mailSent = mail($to, $subject, $message, $headers);

if ($mailSent) {

// $missing is no longer needed if the email is sent, so unset it

unset($missing);

}

}

?>

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

386

7. All that remains is to include the mail processing script. Since the form won’t work

without it, it’s a wise precaution to check that the file exists and is readable before

attempting to include it. The following is a complete listing of the amended code

above the DOCTYPE declaration in feedback.php. The new code, including the

$header pasted in the previous step, is highlighted in bold.

<?php

if (array_key_exists(‘send’, $_POST)) {

//mail processing script

$to = ‘me@example.com'; // use your own email address

$subject = ‘Feedback from Essential Guide';

// list expected fields

$expected = array(‘name’, ‘email’, ‘comments’, ‘interests’, å

‘visited’, ‘views’, ‘subscribe’);

// set required fields

$required = array(‘name’, ‘comments’, ‘visited’);

$headers = ‘From: Essential Guide<feedback@example.com>';

$process = ‘../includes/process_mail.inc.php';

if (file_exists($process) && is_readable($process)) {

include($process);

}

else {

$mailSent = false;

}

}

?>

The path to process_mail.inc.php is stored in $process. This avoids the need to

type it out three times. The conditional statement uses two functions with selfexplanatory

names: file_exists() and is_readable(). If the file is OK, it’s

included. If not, $mailSent is set to false. This displays the warning that there was

a problem sending the message. Because $missing is set inside the processing

script, the user’s input won’t be redisplayed. You could move the initialization of

$missing to feedback.php, but if the script can’t be accessed, your form is broken

anyway.

8. To be super-efficient, send yourself an email alerting you to the problem with the

include file by amending the conditional statement like this:

if (file_exists($process) && is_readable($process)) {

include($process);

}

else {

$mailSent = false;

mail($to, ‘Server problem’, “$process cannot be read”, $headers);

}

You can check the final code in feedback_process.php in examples/ch12 and

process_mail.inc.php in examples/includes.

WORKING WITH PHP INCLUDES AND TEMPLATES

387

12

Because process_mail.inc.php uses generic variables, you can slot this include file into

any page that processes a form and sends the results by email. The only proviso is that you

must use the same variables as in step 7, namely $to, $subject, $expected, $required,

$headers, and $mailSent. You also need to use $missing for the error-checking routine,

as described in the previous chapter.

Programming purists would criticize this use of procedural code, arguing that a more

robust solution should be built with object-oriented code. An object-oriented solution

would probably be better, but it would also be more difficult for a PHP beginner to adapt.

The purpose of this exercise has been to demonstrate how even procedural code can be

recycled with relatively little effort. It also prepares the ground for customizing the

PHP code automatically generated by Dreamweaver. With the exception of the

XSL Transformations server behavior (covered in Chapter 18), Dreamweaver uses procedural

code.

Avoiding the “headers already sent” error

A problem that you’re bound to encounter sooner or later is this mysterious error

message:

Warning: Cannot add header information – headers already sent

It happens when you use header() to redirect a page, as described in the previous chapter,

or with PHP sessions (covered in Chapter 15). More often than not, the cause of the

problem lies in an include file.

Using header() or starting a PHP session must be done before any output is sent to the

browser. This includes not only XHTML but also any whitespace. As far as PHP is concerned,

whitespace means any space, tab, carriage return, or new line character. Why the

error message is so mysterious—and causes so much head banging—is because the whitespace

is often at the end of an include file. Use the line numbers in Code view, as shown

in Figure 12-8, to make sure there are no blank lines at the end of an include file. Also

make sure that there is no whitespace after the closing PHP tag on the final line.

Whitespace inside the PHP tags is unimportant, but the PHP code must not generate any

XHTML output before using header() or starting a session. The same applies to the parent

page: there must be no whitespace before the opening PHP tag.

On rare occasions, the error is triggered by an invisible control character at the beginning

of the file. Use View ä Code View Options ä Hidden Characters to check, and delete the

character.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

388

Figure 12-8. Eliminate whitespace at the beginning and end of include files to avoid the “headers

already sent” error.

Using Dreamweaver templates in a PHP site

Earlier in the chapter, I showed you how to extract two sections from the “Stroll along the

Thames” site and turn them into includes. You could go further, and convert the header

and fixed parts of the document <head> into includes, so that each page consists of several

includes, with just the sidebar and main content forming the actual content of the page.

As long as you keep each include as a coherent block, it’s relatively easy to manage, and

Design view preserves the unified look of the page.

However, it’s not an approach that everybody feels comfortable with. That’s where

Dreamweaver templates can be a useful alternative. A template locks the fixed elements

of the design, but lets you designate editable regions for the content that you want to

change on each page. Dreamweaver templates allow you to control what can and can’t be

edited with a great degree of precision, right down to the individual attributes of a tag. If

you change anything in a locked region of a master template, Dreamweaver automatically

updates all child pages (as long as you accept the option to do so). Although this is convenient,

you still need to upload the changed pages manually to the live website.

I don’t intend to go into the finer details of working with templates but simply give a

broad overview of creating a template, designating editable regions, and creating child

pages. I’ll also touch on issues that apply specifically to working with PHP in a

Dreamweaver template.

WORKING WITH PHP INCLUDES AND TEMPLATES

389

12

Creating a template

The easiest way to create a Dreamweaver template is to design a page in the Document

window in the normal way. It’s then a simple matter of saving the page as a template and

designating the editable regions. Let’s do that with the “Stroll along the Thames” page

from earlier in the chapter.

This exercise combines the benefits of both approaches. The menu and footer are PHP

includes, so can be edited separately, while the rest of the page as a template locks down

the main design elements.

1. Open stroll_horiz_footer.php from examples/ch12 in the Document window.

There is no need to copy or move it, because converting it into a template takes

care of that.

2. Choose Make Template from the Common tab of the Insert bar, as shown in the following

screenshot. Alternatively, use the menu option File ä Save as Template.

3. This opens the following dialog box:

In theory, you can choose to save the template in a different site, but this is likely

to cause problems with images, so leave Site unchanged. Existing templates displays

a list of templates that you have already defined, if any. Optionally enter a description

Converting stroll_horiz_footer.php into a template

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

390

of the template in the Description field. The Save as field suggests using the current

file name. You can change this, if you like, but don’t add a file name extension, as

Dreamweaver uses a special extension for templates. Click Save.

4. Dreamweaver asks if you want to update links. You must click Yes, or your template

will have broken links and cause endless trouble.

5. Although it may appear as though nothing happens, the Dreamweaver title bar

changes to display <<Template>> in front of the file name, which now has a

.dwt.php extension, as shown here.

Dreamweaver also saves the new template in the Templates folder in the site root.

If the folder doesn’t exist, Dreamweaver creates it silently.

Adding editable regions to the master template

Everything in a template is locked, except for the <title> tag and an editable region in the

<head> of the document. This is needed so that external JavaScript files and style sheets

can be added to a child page. It’s also where Dreamweaver behaviors insert the JavaScript

functions that they require.

It goes without saying that you must unlock at least one part of the page for the template

to be of any real value. Otherwise, every child page would be identical. Deciding what to

lock and unlock depends entirely on the level of control that you want over a page. For

instance, you could create separate editable regions for each of the headings on the page.

An important exception to this basic principle is that the area above the DTD and

below the closing </html> tag is not locked in templates for server-side languages,

such as PHP. I’ll come back to this issue a little later, as it causes a lot of confusion.

The file with the .dwt.php file name extension is now the master template from which

you create child pages. Any changes to the design of this page will affect all child

pages created from it—as long as you accept the option to update them. You must

not move the template from the Templates folder. This is perhaps the single most

common mistake with templates—moving the master template to another folder will

cause you endless grief. Don’t do it.

WORKING WITH PHP INCLUDES AND TEMPLATES

391

12

If you select the entire heading, including its surrounding tags, the heading can be

replaced by anything: a table, a <div>, an <iframe>, or whatever you like. If you select just

the content of an <h2> tag and convert it into an editable region, only the content can be

changed in a child page. You can’t even change it to an <h1> tag.

Since the remaining chapters of this book are about building dynamic content with PHP,

you don’t want such rigid control. So you could make everything inside the container

<div> one big editable region. However, we’ll take a slightly different approach.

This exercise shows you how to create separate editable regions for the sidebar heading

and content, as well as for the whole main content area.

1. Open stroll_horiz_footer.dwt.php in the Templates folder if it’s not already

open.

2. Open Split view. Click immediately to the left of the heading that reads The pleasures

of London. Hold down your mouse button and drag to the end of the heading.

Alternatively, hold down the Shift key while pressing the keyboard right arrow to

select the content of the heading. Make sure you have just the text and not the surrounding

<h3> tags, as shown in the following screenshot.

3. There are several ways to make this an editable region. If you’re a fan of the Insert

bar, click the down arrow next to the Make Template button on the Common tab,

and select Editable Region. The Insert bar remembers your last selection, so the

Editable Region button remains displayed, ready for the creation of more editable

regions.

Alternatively, right-click and select Templates ä New Editable Region from the context

menu, or go to Insert ä Template Objects ä Editable Region.

4. This opens the New Editable Region dialog box. It has just one field for a name for

the editable region. It can be anything you like, but each region must have a different

name. Enter sidebarHead, and click OK.

5. This wraps the contents of the <h3> tag in two special HTML comment tags, as

shown in Figure 12-9. These tell Dreamweaver to treat this as an editable region in

child pages. Dreamweaver also displays a turquoise border around the region in

Design view, with a tab at the top left indicating the name of the editable region.

Making the sidebar and main content areas editable

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

392

Figure 12-9. Editable regions are easily identified in both Code view and Design view.

6. Select the paragraphs and <blockquote> in the sidebar. If you’re still in Split view,

you’ll see that selecting the text in Design view misses the opening tag of the first

paragraph and the closing tag of the final one. This doesn’t matter. Since more than

one paragraph is selected, Dreamweaver is normally clever enough to realize that

you want the surrounding tags and includes them when you create the editable

region.

7. Use one of the previous methods to make this an editable region named

sidebarContent. Switch to Code view to make sure that the opening and closing

<p> tags were included. If they weren’t, move them inside the template comments.

8. Select all the content in the mainContent <div>, but not the surrounding <div>

tags, and create an editable region called mainContent. Check that the template

comments are in the right place in Code view, and save stroll_horiz_footer.

dwt.php.

9. Dreamweaver should display a warning that sidebarHead is inside a block tag, and

that users of the template won’t be able to create new blocks in this region. This is

because the <h3> tags are outside the sidebarHead editable region, which prevents

anything other than a level three heading being created. That’s fine. So click OK.

Creating child pages from a template

Now that you have a template, you can build pages based on it. The editable regions can

be freely changed, but the other areas remain locked and can be changed only by editing

the master template.

Always check the position of the TemplateBeginEditable and TemplateEndEditable

comments in Code view, as you can easily move them or any of the surrounding code

while still in the template. Checking now saves a lot of frustration later, when you discover

that you didn’t select the region accurately in Design view, and your child pages

don’t work the way you expect. These comments are an integral part of the template

control mechanism and are propagated to the child pages, where they remain part of

the XHTML (see Figure 12-13).

WORKING WITH PHP INCLUDES AND TEMPLATES

393

12

This exercise uses the template from the previous exercise to create a child page and

explores the way editable regions are displayed in both Design view and Code view. It also

demonstrates the importance of using site-root-relative links in PHP includes,

1. Go to File ä New. When the New Document dialog box opens, select Page from

Template from the options on the left side. Assuming that you created the template

in the preceding exercises, the dialog box should look similar to Figure 12-10.

Figure 12-10. The New Document dialog box gives you access to all the templates you have created.

If you have created templates in several sites, select the site and the template that

you want to use as the basis for a new page (you can have as many templates as

you like in a site, using different designs for pages that serve different functions).

The New Document dialog box shows a preview of the selected template, together

with the description you entered when it was first created.

The idea of a template is that all changes to common elements are propagated

automatically to child pages when the master template is updated. Unless you want

to create a page that doesn’t automatically update, make sure that there’s a check

mark in Update page when template changes, and click Create.

2. A new page is created in the Document window. At first glance, it looks identical to

the template, but several features tell you that it’s a child page (see Figure 12-11)

and that you can make changes only to the editable regions indicated by the

turquoise borders and tabs. Whenever your mouse is over a locked part of the

page, the pointer turns into a circle with a diagonal bar to warn you that no

changes can be made.

Creating and editing a template-based page

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

394

Figure 12-11. The child page is identical to the master template, but locked areas

can no longer be edited.

3. Save the page as stroll_index.php in workfiles/ch12.

4. Repeat step 1 to create another child page from the template. Save it as stroll_

restaurants.php in a new folder called workfiles/ch12/food.

5. Make some changes to stroll_restaurants.php. Experiment to see what you can

and can’t change. For instance, the sidebarHead cannot be changed to a different

format, but the <h2> at the top of the mainContent <div> can be changed or

deleted altogether. You can also change the Title field in the Document toolbar.

Give the pages different titles by adding Home to stroll_index.php and

Restaurants to stroll_restaurants.php. Make sufficient changes to one of the

pages so that you can tell them apart, and then save both of them.

6. The navigation menu contains only dummy links at the moment, so open

menu.inc.php in workfiles/includes. Update the link for Home so that it points

to stroll_index.php and for Restaurants to point to stroll_restaurants.php.

Because the navigation menu is shared by files in different levels of the site hierarchy,

you must make the links relative to the site root, even if you have set the site

default to use links relative to the current document. Use the Browse icon (it looks

like a folder) alongside the Link field in the Property inspector to select the target

files, and set the Relative to drop-down menu to Site Root as shown in Figure 12-12.

WORKING WITH PHP INCLUDES AND TEMPLATES

395

12

Figure 12-12. Links for the navigation menu must be relative to the site root.

7. Save menu.inc.php, and switch back to stroll_index.php in the Document window.

You want to test the navigation menu links, so your Dreamweaver preferences

shouldn’t use temporary files for preview. (If you’re not sure, open Preferences

from the Edit menu or Dreamweaver menu on a Mac; then select Preview in Browser,

and make sure Preview using temporary file is deselected.) Press F12/Opt+F12 to preview

stroll_index.php in a browser.

8. Test the Restaurants link. If you have followed the instructions carefully, it won’t

work. Don’t worry; I’ve done something deliberately to demonstrate an important

feature of building pages from templates.

9. Open stroll_index.php in Code view. The first thing you’ll notice is that several

parts of code are colored light gray. All the code in gray is locked. Try editing one

of these areas. Although Dreamweaver puts the insertion point wherever you click,

you cannot type anything. The only exception is between the template comments

shown on line 25 of Figure 12-13. This is where you can add extra style sheets or

JavaScript. It’s also where you should insert any other elements that normally go in

the <head> of a web page, such as keywords and description <meta> tags.

Figure 12-13. Locked areas in a child page cannot be edited (lines 8–22 and 27–35 are hidden

using Code collapse).

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

396

Another thing to note is the codeOutsideHTMLIsLocked attribute shown on line 2

of Figure 12-13. By default, this is set to false. This is to allow you to apply server

behaviors to pages created from a template. When you start working with server

behaviors in Chapter 14, you’ll see that Dreamweaver puts most of the PHP code

outside the <html> tags, so if this were set to true, you wouldn’t be able to apply

server behaviors to your pages. I’ll explain the implications of this shortly.

Finally, look at the include command for menu.inc.php (it’s on line 36 in Figure 12-13).

It’s pointing to the version of the file in examples/includes, but the menu that you

edited in step 6 is in workfiles/includes. Because this code is in a locked area,

you can’t edit it. You need to do that in the master template.

10. Open stroll_horiz_footer.dwt.php in the Templates folder, and click the navigation

menu. The Property inspector displays the name of the include file. Click the

folder icon to the right of the Filename field, and browse to the workfiles/

includes folder. Select menu.inc.php, and make sure that Relative to is set to

Document (PHP includes need a relative path).

11. Save stroll_horiz_footer.dwt.php. Dreamweaver will remind you again that

sidebarHead is inside a block tag. Just click OK. The next dialog box asks you if you

want to update all files based on the template, and lists all of the child pages. The

whole point of a template is automatic updating, so click Update.

12. When all the files have been updated, Dreamweaver displays a report like this:

If you don’t see the list of updated files at the bottom of the dialog box, select the

Show log checkbox. If anything goes wrong, Dreamweaver reports which files it

wasn’t able to update. As you can see from the preceding screenshot, the update

process is almost instantaneous with only a couple of child pages, but the time

taken depends on the number of pages and the complexity of the updates. Click

Close.

13. Reload stroll_index.php in your browser, and click the Restaurant link. You

should be taken to stroll_restaurants.php, and the Home link should take

you back to stroll_index.php. You can check your files against the versions in

examples/ch12.

WORKING WITH PHP INCLUDES AND TEMPLATES

397

12

This is only a trivial example of how a change to the master template is propagated to all

child pages, but it should be sufficient to demonstrate how templates control the look and

shape of a site. However, the real power of this sample layout lies not so much in the template,

but in the use of an include file for the navigation menu. If you were to leave the

navigation menu in the main template, you would need to update every single child page

each time you edit the menu. With an include, the edits take place in the external file but

are immediately available in all pages that include it.

Locking code outside the <html> tags

Often, questions appear in online forums from people puzzled by the fact that the code

isn’t propagated to child pages when a server behavior is applied to a template. Although

coverage of server behaviors begins in Chapter 14, it makes sense to discuss this issue

here, while still on the subject of templates.

Dreamweaver uses the space above the DTD and below the closing </html> tag to create

the PHP scripts used for server behaviors, such as inserting or updating records in a database.

This is the same technique as you used in the last chapter to build the mail processing

script. The reason for doing this is quite simple: the PHP engine reads the page from

top to bottom and processes the dynamic code in the order that it encounters it. So, if you

have a page that displays the results of a database search, it stands to reason that

you need to conduct the search before displaying the results as XHTML. Dreamweaver

uses the area after the closing </html> tag to clean up any resources used by the script.

Templates are intended to lock common elements, but dynamic code is almost always

unique to a page. As a result, Dreamweaver doesn’t lock the code outside the <html> tags.

So, even if you apply a server behavior to a master template (or write your own custom

script above the DTD), the code outside the <html> tags will not be propagated to any

child pages.

If, for any reason, you want to create a template that propagates code outside the <html>

tags, add the following code anywhere inside the <head> of the master template:

<!– TemplateInfo codeOutsideHTMLIsLocked=”true” –>

This is an all or nothing option. The PHP code will be propagated to child pages, but you

cannot apply any other server behaviors to such child pages. The circumstances in which

this option is useful are extremely rare, so use with care—if at all.

Choosing the right tool

The considerably large space I have devoted to PHP includes in this chapter should give

you a fair indication of my personal preference for includes. However, some people find

the idea of splitting a page into its various component parts a difficult concept to come to

terms with. So templates do have an important role to play. They also offer a more secure

solution if you work in a team environment. You can generate a child page and hand it to

a less experienced developer in the knowledge that only the editable regions can be

changed. With includes, nothing is locked. But as a site gets larger, so too do the efficiency

savings offered by includes.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

398

13 SETTING UP MYSQL AND

PHPMYADMIN

Dynamic websites take on a whole new meaning in combination with a database. Drawing

content from a database allows you to present material in ways that would be impractical—

if not impossible—with a static website. Examples that spring to mind are online stores,

such as Amazon.com; news sites, such as the International Herald Tribune (www.iht.com);

and the big search engines, including Google and Yahoo! Database technology allows these

websites to present thousands, sometimes millions, of unique pages with remarkably little

underlying code. Even if your ambitions are nowhere near as grandiose, a database can

increase your website’s richness of content with relatively little effort.

Although PHP is capable of interacting with most popular databases (and some less wellknown

ones, too), Dreamweaver has made the choice for you. All the server behaviors are

designed to work with MySQL—a good choice, because it’s widely available, free, very fast,

and offers an excellent range of features.

In this chapter, you will learn how to

Install MySQL on Windows and Mac OS X

Secure access to MySQL

Set up the phpMyAdmin graphical interface

Back up and transfer data to another server

Introducing MySQL

If you have ever worked with Microsoft Access, your first encounter with MySQL might

come as something of a shock. For one thing, it doesn’t have a glossy interface. As

Figure 13-1 shows, it looks like a throwback to the old days of DOS before the friendly

interfaces of Mac and Windows. Its beauty lies, however, in its simplicity. What’s more,

most of the time you’ll never see MySQL in its raw state like this. You’ll either use

Dreamweaver or a graphic front end called phpMyAdmin. Best of all, you’ll be designing

your own personalized interface by creating PHP pages.

Figure 13-1. The unadorned interface of MySQL as seen in the Windows MySQL Command

Line Client

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

402

The other thing that comes as a surprise to Access users is that your database is not kept

in a single file that you can upload to your remote server. MySQL keeps all databases in a

central data folder, and each database table normally consists of three separate files. The

way you transfer data from one server to another is by creating a text file that contains all

the necessary commands to build the database and its contents—in other words, a backup

file. All you need to know now is that there isn’t “a database file”—there are lots of them,

and normally, you should never handle them directly.

Understanding basic MySQL terminology

If you’ve not worked with a relational database before, you may find your head spinning

with some of the names that crop up throughout the rest of this book. So here’s a quick

guide:

SQL: Structured Query Language is the international standard behind all major relational

databases. It’s used to insert and otherwise manipulate data and is based on

natural English. For instance, to get the values of first_name and family_name

from a database table called members, where username is equal to dpowers, you

would use the following command (or SQL query):

SELECT first_name, family_name

FROM members

WHERE username = ‘dpowers’

As you can see, it’s very human readable, unlike many other computer languages.

Although SQL is a standard, all of the main databases have added enhancements

on top of the basic language. If you have been using Access or Microsoft SQL

Server, be prepared for some slight differences in the use of functions. Some people

pronounce SQL “sequel,” while others say “Ess-queue-ell.” Both are right.

MySQL: This refers to the entire database system created by MySQL AB of Sweden.

It’s always spelled in uppercase, except for the “y,” and the official pronunciation is

“My-ess-queue-ell.” It’s not just a single program, but also a client/server system

with a number of related programs that perform various administrative tasks. The

two main components are mysql and mysqld, with both terms entirely in lowercase.

mysqld: This is the server (or, to give it its proper technical name, daemon) that

runs in the background listening for requests made to the database. Once it has

been started, you can ignore it.

mysql: This has three distinct meanings. The first is the client program used to feed

requests to the database. mysql is also the name of the main administrative database

that controls user accounts, and on Windows, it is the name of the Windows

service that starts and stops the database server. Once you start working with

MySQL, differentiating between the different meanings of “mysql” is not as confusing

as it first seems.

SETTING UP MYSQL AND PHPMYADMIN

403

13

Installing MySQL

So, let’s press ahead and install MySQL. There are separate instructions for Windows and

Mac OS X. If you plan to use a remote server as your testing server, and already have

MySQL and phpMyAdmin set up, you can skip ahead to the next chapter.

You can get MySQL from the downloads page at http://dev.mysql.com/downloads/.

Select the Download link for MySQL Community Server, as shown in the following screenshot.

This link takes you to the latest stable version of MySQL (currently the 5.0.x series).

If, for any reason, you want to install an older version, don’t click this link, but scroll down

the page to the link to archives of older releases.

MySQL Enterprise is the commercial version, but the technical features in the Community

Server are identical. The main difference is that MySQL Enterprise comes with technical

support. With the Community Server, you’re on your own, but you have this book to guide

you. There is also a large community of MySQL users who are able to offer help online.

The installation instructions for MySQL are different for Windows and Mac OS X, so Mac

users should skip ahead to the relevant section of the chapter.

Installing MySQL on Windows

MySQL comes in a range of versions, but the one you should choose is Windows Essentials.

It contains all the important stuff and certainly everything you need for this book. If you

have a version older than MySQL 4.1.5 already installed on your computer, you must uninstall

the old version first.

Deciding whether to enable InnoDB support

MySQL is capable of storing database tables in a variety of formats. Most of the time, you

don’t need to worry about this. The default MySQL format, MyISAM, is fast and highly reliable.

Moreover, if you’re on shared hosting, this is frequently your only choice. However, if

you have your own dedicated server, you will almost certainly also have the option of

InnoDB tables, which offer extra features including foreign key constraints (see Chapter 16

for details). Some hosting companies also offer support for InnoDB, so it’s worth checking

Because new versions are coming out all the time, I recommend that you check my

website at http://foundationphp.com/egdwcs3/updates.php before going ahead.

Any major updates to the instructions will be listed there.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

404

before installing MySQL on your local computer. The Windows Essentials version of MySQL

automatically enables support for InnoDB, but you can save 60MB of disk space if you

don’t need to use it.

You can either check with your hosting company directly, or you can do a simple test by

running a SQL query on your remote server. Most companies provide phpMyAdmin to you

to administer your database(s). Launch phpMyAdmin, and click the SQL tab. Delete any

existing query in the Run SQL query field, and type the following:

SHOW VARIABLES LIKE ‘have_inno%’

Click Go. If you see a result like the one shown in Figure 13-2, it means InnoDB tables are

supported. If your remote server is very old, the Variable_name column may read

have_innobase. This is the same as InnoDB.

Figure 13-2. Confirmation that the MySQL server supports InnoDB tables

If the Value column says NO, InnoDB is not supported. The instructions in later chapters

show you how to emulate foreign key constraints by using PHP conditional logic.

Don’t worry if you can’t find out whether your remote server supports InnoDB. You can

easily add or remove InnoDB from your local setup later.

These instructions are based on the 5.0 series of MySQL, which is installed in

C:\Program Files\MySQL\MySQL Server 5.0. I expect MySQL 5.1 to become the recommended

release shortly after publication of this book. On past experience, the

default location changes for each series of Windows Essentials, so 5.1 is likely to be

installed in C:\Program Files\MySQL\MySQL Server 5.1, and Windows treats different

series as completely separate programs. If you upgrade from one series to another, any

existing databases need to be transferred to the new version as if it were a different

server (see the section titled “Backup and data transfer” near the end of this chapter).

Installing the Windows Essentials version of MySQL

SETTING UP MYSQL AND PHPMYADMIN

405

13

1. Go to the MySQL download site, and select the link for MySQL Community Server.

2. In the page that opens, scroll down to find the section marked Windows downloads.

Choose Windows Essentials, and click the download link. (You may be invited to

Pick a mirror instead. This directs you to a mirror site closer to your location and

usually offers a faster download.)

3. Download the MySQL file to your hard disk. It will have a name like mysqlessential-

x.x.x-win32.msi, where x.x.x represents the version number.

4. Exit all other Windows programs; make sure you are logged in as an administrator

(in Windows Vista; turn off User Account Control temporarily—see Chapter 3 for

instructions), and double-click the icon of the file you have just downloaded. This

is a self-extracting Windows Installer package.

5. Windows Installer will begin the installation process and open a welcome dialog

box. If you are upgrading an existing version of the same series of Windows

Essentials to a more recent one, the dialog box will inform you that it has detected

your current installation and will remove it before installing the new one. However,

all your databases will remain intact. Click Next to continue.

6. Dialog boxes will give you the opportunity to change the installation destination

and select the type of setup. Accept the defaults, and click Next.

7. If you’re happy to go ahead with installation, click Install in the next dialog box.

8. Before launching into the actual installation, MySQL invites you to sign up for a free

MySQL.com account. I suggest that you select Skip Sign-Up and click Next. After

you finish setting up everything, visit http://www.mysql.com/register.php to see if you’re

interested in the benefits offered. The main advantage is that you get automatic

notification of new versions and links to helpful articles about new features of

MySQL.

9. The actual installation now takes place and is normally very quick. When everything’s

finished, you’re presented with a final dialog box.

If this is a new installation or if you are upgrading from one series to another,

click Finish to launch the configuration wizard, which is described in the next

section.

If you are upgrading to a later version of the same series (such as from 5.0.10 to

5.0.37), deselect the checkbox labeled Configure the MySQL Server now before

clicking Finish. MySQL should be ready to use but needs to be restarted manually

(see “Starting and stopping MySQL manually on Windows” later in the chapter).

If you have a software firewall, you might also be prompted to allow

connections to and from MySQL. You must permit connections in order to work

with the database.

There are a lot of dialog boxes to go through, although all you usually need to do is accept

the default setting. These instructions are based on version 1.0.8 of the Configuration

Wizard.

Configuring MySQL Windows Essentials

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

406

1. The Configuration Wizard opens with a welcome screen. Click Next to proceed.

2. The first dialog box asks whether you want a detailed or standard configuration.

Choose the default Detailed Configuration option, and click Next.

3. The three options on the next screen affect the amount of computer resources

devoted to MySQL. Accept the default Developer Machine, and click Next. If you

choose either of the other options, all other programs will slow down to a crawl.

4. The next dialog box asks you to select from the following three types of database:

Multifunctional Database: Allows you to use both InnoDB and MyISAM tables.

Transactional Database Only: InnoDB tables only. MyISAM is disabled.

Non-Transactional Database Only: MyISAM tables only. InnoDB is disabled.

Your choice depends on whether your remote server supports InnoDB tables (see

“Deciding whether to enable InnoDB support” earlier in the chapter). If it does,

choose Multifunctional Database. Otherwise, choose Non-Transactional Database Only.

Do not choose Transactional Database Only. You should use InnoDB tables only

when you need the extra features they provide, so you need support for MyISAM

tables as well.

If you’re not sure which to choose, and disk space is not a problem, choose

Multifunctional Database. However, you should be aware that this option requires an

extra 60MB of disk space to create the InnoDB tablespace.

5. What you see next may vary. If you chose Non-Transactional Database Only in the

preceding step, you will probably be taken directly to step 6. However, you may see

a dialog box inviting you to select a drive for the InnoDB data file. Unless you chose

Multifunctional Database, just click Next and move on to step 6.

If you plan to use InnoDB, you need to tell MySQL where to store the data.

The InnoDB engine uses a single tablespace that acts as a sort of virtual file system.

InnoDB files, once created, cannot be made smaller. The default location for the

tablespace is C:\Program Files\MySQL\MySQL Server 5.0\data. If you want to

locate the tablespace elsewhere, the drop-down menu offers some suggested

alternatives. When you have made your choice, click Next.

6. Leave the next dialog box at the default Decision Support (DSS)/OLAP, and click

Next.

7. The next dialog box sets the networking options and SQL mode. The important settings

are in the top half. Make sure Enable TCP/IP Networking is checked, and leave

Port Number on the default setting of 3306. The lower half of the dialog box lets

you choose whether to run MySQL in strict mode. In an ideal world, you should

accept this default setting, but it may cause problems with some PHP applications

written before strict mode was introduced. Deselect the Strict mode checkbox, and

click Next.

If you choose Multifunctional Database, you need to edit the MySQL configuration file

later, as described in “Changing the default table type on Windows Essentials.”

SETTING UP MYSQL AND PHPMYADMIN

407

13

8. MySQL has impressive support for most of the world’s languages. The next dialog

box invites you to choose a default character set. In spite of what you might think,

this has no bearing on the range of languages supported—all are supported by

default. The character set mainly determines the order in which data is sorted.

Since Dreamweaver CS3 now uses Unicode (UTF-8) as the default encoding for web

pages, choosing the second option, Best Support for Multilingualism, seems the obvious

choice. However, support for Unicode was not introduced to MySQL until

version 4.1. If your hosting company is still running an earlier version of MySQL,

you should stick with the default Standard Character Set. This is also a suitable

choice if you work exclusively in English or use a completely different encoding,

such as Shift_JIS for Japanese. Click Next after you have made your choice.

9. The recommended way of running MySQL is as a Windows service. If you accept

the defaults as shown in the top half of the next dialog box, MySQL will always start

automatically when you boot your computer and run silently in the background. (If

MySQL has already been installed as a Windows service, this section will be grayed

out.) If for any reason you don’t want MySQL to start automatically, uncheck the

Launch the MySQL Server automatically option. You can easily change this option

later (see the section “Starting and stopping MySQL manually on Windows” later in

this chapter).

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

408

The lower half of the dialog box gives you the option to include the bin directory

in your Windows PATH. This option enables you to interact directly with MySQL and

its related utilities at the command line without the need to change directory every

time. You won’t need to do this very often—if at all—but selecting this option

makes life a little more convenient if the occasion ever arises. Click Next.

10. A fresh installation of MySQL has no security settings, so anyone can tamper with

your data. MySQL uses the name root to signify the main database administrator

with unrestricted control over all aspects of the database. Choose a password that

you can remember, and enter it in both boxes.

Unless you access your development server from a different computer over a network,

leave the Enable root access from remote machines checkbox unchecked.

Do not check Create An Anonymous Account. It will make your database insecure.

If you are upgrading an existing version of Windows Essentials and want to keep

your current root password, deselect the Modify Security Settings checkbox. If this is

a first-time installation, you might not have this checkbox.

Click Next when you have finished.

If you get a warning message that a Windows service with the name MySQL already

exists, you will be asked if you want to use this name. You must click No and choose a

different name from the drop-down menu in the Service Name field.

SETTING UP MYSQL AND PHPMYADMIN

409

13

11. At long last, everything is ready. Click Execute. If you have installed a software firewall,

it will probably warn you that MySQL is trying to connect to a DNS server. You

must allow the connection; otherwise, MySQL will never work. If your firewall

doesn’t list MySQL specifically, make sure that it permits local connections on port

3306, the MySQL default.

12. Assuming that all was okay, you should see a screen confirming that the configuration

process is complete. MySQL should now be running—even if you selected the

option not to start automatically (the option applies only to automatic start on

bootup).

13. If you want to change the configuration at a later date—say, to add or remove support

for InnoDB—launch the Configuration Wizard from the Windows Start menu

by choosing Programs ä MySQL ä MySQL Server 5.0 ä MySQL Server Instance

Config Wizard. The dialog box that opens offers the following two options:

Reconfigure Instance: This takes you through all the dialog boxes again. If you

add support for InnoDB, change the default table type, as described in the next

section. If you remove support for InnoDB, stop the MySQL server after the

wizard has finished, and delete any files with names that begin ibdata and

ib_logfile from C:\Program Files\MySQL\MySQL Server 5.0\data. Then

restart MySQL.

Remove Instance: This does not remove MySQL from your system but removes

the Windows service that automatically starts MySQL when you boot your computer.

Unfortunately, it also removes the MySQL configuration file. See “Starting

and stopping MySQL manually on Windows” for a less radical solution.

Changing the default table type on Windows Essentials

The instructions in this section are required only if you selected Multifunctional Database in

step 4 of “Configuring MySQL Windows Essentials.”

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

410

The Windows Configuration Wizard sets InnoDB as the default table storage engine for a

multifunctional database. This is the opposite of the standard MySQL setup, so it makes

sense to switch the default to match the way your remote server works. All it requires is a

simple change to the MySQL configuration file: my.ini.

1. Use Windows Explorer to navigate to the folder in which MySQL was installed. The

default is C:\Program Files\MySQL\MySQL Server 5.0.

2. Locate the file called my.ini, and double-click it. The file will open in Notepad.

3. Approximately 80 lines from the top, you should find a line that reads as follows:

default-storage-engine=INNODB

Change it to the following (the spelling of MyISAM is case insensitive):

default-storage-engine=MyISAM

4. Save the file, and close it. To make the change effective, restart MySQL. MySQL will

now create all new tables in the default MyISAM format. To use the InnoDB format

for a database or an individual table, you can change the table type in phpMyAdmin,

the graphical interface for MySQL that you will install later in the chapter.

Starting and stopping MySQL manually on Windows

Most of the time, MySQL will be configured to start up automatically, and you can forget

about it entirely. There are times, however, when you need to know how to start or stop

MySQL manually—usually for maintenance or to conserve resources.

1. Select Control Panel from the Windows Start menu. Double-click the Administrative

Tools icon, and then double-click the Services icon in the window that opens.

2. In the Services panel, scroll down to find MySQL, and highlight it by clicking once.

You can now use the video recorder–type icons at the top of the panel (or rightclick

to bring up the context menu) to stop or start the server.

3. To change the automatic startup option, highlight MySQL in the Services panel,

right-click to reveal a context menu, and choose Properties.

4. In the dialog box that opens, activate the Startup type drop-down menu, and

choose Automatic, Manual, or Disabled. Click OK. That’s all there is to it.

Using the MySQL monitor on Windows

Although most of your interaction with MySQL will be through phpMyAdmin or your own

PHP scripts, it’s useful to know how to access MySQL through the MySQL monitor (or the

Command Line Client, as it’s called in Windows Essentials). It’s also a good way to test that

your installation went without problems.

To start a session From the Windows Start menu, select Programs ä MySQL ä MySQL

Server 5.0 ä MySQL Command Line Client. This will open the Command Line Client, which

will ask you for your password. Type the root password that you chose in step 10 of the

section “Configuring MySQL Windows Essentials,” and press Enter. As long as the server is

SETTING UP MYSQL AND PHPMYADMIN

411

13

running—and you typed your password correctly—you will see a welcome message similar

to the one shown here (on Windows XP, the title bar says MySQL Command Line Client).

If you get your password wrong, your computer will beep and close the window. If you

find this happening repeatedly, even though you’re sure you typed in your password correctly,

there are two likely explanations. The first is that your Caps Lock key is on—MySQL

passwords are case sensitive. The other is that the MySQL server isn’t running. Refer to the

previous section on how to control MySQL manually before doing too much damage by

banging your forehead on the keyboard.

Ending your session After you finish working with the MySQL monitor, type exit or quit at

the mysql> prompt, followed by Enter. The MySQL Command Line Client window closes

automatically.

Setting up MySQL on Mac OS X

MySQL is available as a Mac PKG file, so everything is taken care of for you, apart from

some minor configuration.

When upgrading an existing installation of MySQL, the Mac installer will not move

your data files. You must first create a backup, as described at the end of this chapter,

and reload them after upgrading. You must also shut down the MySQL server. If you

have never installed MySQL before, you don’t need any special preparations; just follow

these instructions.

Being unable to connect to MySQL because the server isn’t running is probably the

most common beginner’s mistake. The MySQL server runs in the background, waiting

for requests. Opening the Command Line Client does not start MySQL; it opens the

MySQL monitor, which is a channel for you to send instructions to the server. Equally,

closing the Command Line Client does not stop MySQL. The server continues running

in the background until the computer is closed down or until you stop it manually.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

412

1. Go to http://www.mysql.com/downloads, and select the link for MySQL Community Server.

2. Select the Mac OS X (package format) downloads section, and choose the Standard

version for your processor and version of OS X—there are separate packages for

PowerPC, 64-bit PowerPC, and Intel Macs. The Intel Mac version is labeled x86. As

you can see from the screenshot in the next step, the PKG file name includes not

only the MySQL version number but also the version of OS X and processor for

which it has been compiled (osx10.4-powerpc).

3. Double-click the DMG icon to mount the disk image on your desktop.

4. Double-click the mysql-standard-x.x.x.pkg icon to start the installation process. The

Mac OS X installer opens. Follow the instructions onscreen.

5. Double-click the MySQLStartupItem.pkg icon, and follow the instructions onscreen.

6. Open a Finder window, and drag the MySQL.prefPane icon onto Applications ä

System Preferences. This installs a MySQL control panel. A dialog box asks whether

you want it to be available to yourself or all users. Make your choice, and click

Install.

The MySQL preference pane should open. Click Start MySQL Server, and enter your

Mac administrator password when prompted. It may take a few seconds before the

preference pane reports that the server is running, as shown here:

The Mac files are available in two formats. Make sure you don’t select a TAR package

by mistake. These instructions are for the package format, which uses a Mac installer.

Downloading and installing MySQL

SETTING UP MYSQL AND PHPMYADMIN

413

13

To start or stop the MySQL server in future, open the preference pane by clicking

the MySQL icon in the Other section of System Preferences.

Adding MySQL to your PATH

You normally access MySQL through phpMyAdmin (introduced later in this chapter) or

your own PHP scripts, but sometimes you need to access it directly in Terminal. To avoid

having to type out the full path every time, add it to the PATH in your environmental variables.

By default, Terminal uses what is known as the “bash shell.” Open Terminal (in

Applications ä Utilities), and check the title bar. If it says Terminal—bash, as shown in the

following screenshot, use the following instructions. In the unlikely event that it says

Terminal—tcsh, follow the instructions in the section titled “Amending PATH in the tcsh

shell.”

Use this set of instructions if the Terminal title bar says Terminal—bash:

1. Open BBEdit or TextWrangler.

2. From the File menu, choose Open Hidden, and browse to your home folder. If there

is a file called .profile (with a period as the first character), as shown in the

screenshot, highlight it, and click Open.

Amending PATH in the bash shell

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

414

3. The file exists only if you have already made changes to the way Terminal operates.

If .profile doesn’t exist, click Cancel, and open a blank file.

4. If you have opened an existing version of .profile, add the following code on a

separate line at the end. Otherwise, enter it in the blank page.

export PATH=”$PATH:/usr/local/mysql/bin”

5. Select File ä Save, and save the file as .profile in your own home folder. The

period at the beginning of the file name should provoke the following warning:

6. Select Use “.” and close your text editor.

SETTING UP MYSQL AND PHPMYADMIN

415

13

Use the following, alternative instructions only if the title bar says Terminal—tcsh:

1. Open Terminal, and enter the following command at the shell prompt:

echo ‘setenv PATH /usr/local/mysql/bin:$PATH’ >> ~/.tcshrc

Make sure you copy everything exactly, including the quotes and spacing as shown.

2. Press Return, and close Terminal. The next time you open Terminal, the MySQL program

directory will have been added to your PATH.

Securing MySQL on Mac OS X

Although you have a fully functioning installation of MySQL, by default it has no security.

Even if you’re the only person working on your computer, you need to set up a similar system

of passwords and user accounts as on your hosting company’s server. There’s one

important account that exists by default on all MySQL servers. It’s called root, and it is the

main database administrator with unlimited powers over database files. When you first

install MySQL, access to the root account isn’t password protected, so you need to block

this security gap. The MySQL root user, by the way, is totally unrelated to the Mac OS X

root user, which is disabled by default. Enabling root for MySQL has no effect on the

OS X root user.

1. Open Terminal, and type the following command:

mysql -u root

The command contains three elements:

mysql: The name of the program

-u: Tells the program that you want to log in as a specified user

root: The name of the user

Setting the MySQL root password

If you have just added MySQL to your PATH, you must close and reopen Terminal

before embarking on this section. Otherwise, Terminal won’t be able to find MySQL.

Amending PATH in the tcsh shell

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

416

2. You should see a welcome message like this:

3. The most common problem is getting an error message like this instead:

It means that mysqld, the MySQL server, is not running. Use the MySQL control

panel in System Preferences to start the server.

Another common problem is for Terminal to report command not found. That

means you have either mistyped the command or that you haven’t added the

MySQL program files directory to your PATH, as described in the previous section.

4. Assuming that you have logged in successfully, as described in step 2, type the following

command at the mysql> prompt:

use mysql

This command tells MySQL that you want to use the database called mysql, which

contains all the details of authorized users and the privileges they have to work on

database files. You should see the message Database changed, which means MySQL

is ready for you to work on the files controlling administrative privileges.

5. Now enter the command to set a password for the root user. Substitute

myPassword with the actual password you want to use. Also make sure you use

quotes where indicated and finish the command with a semicolon.

UPDATE user SET password = PASSWORD(‘myPassword‘) WHERE user = ‘root';

6. Next, remove anonymous access to MySQL:

DELETE FROM user WHERE user = ”;

The quotes before the semicolon are two single quotes with no space in between.

SETTING UP MYSQL AND PHPMYADMIN

417

13

7. Tell MySQL to update the privileges table:

FLUSH PRIVILEGES;

The sequence of commands should produce a series of results like this:

8. To exit the MySQL monitor, type exit, followed by Return. This simply ends your session

with the MySQL monitor. It does not shut down the MySQL server.

9. Now try to log back in by using the same command as in step 2. MySQL won’t let

you in. Anonymous access and password-free access have been removed. To get in

this time, you need to tell MySQL that you want to use a password:

mysql -u root -p

10. When you press Return, you will be prompted for your password. Nothing will

appear onscreen as you type, but as long as you enter the correct password, MySQL

will let you back in. Congratulations, you now have a secure installation of MySQL.

Using the MySQL monitor on Windows and Mac

From this point on, 99.9 percent of everything you do is identical on both Windows and

Mac OS X. If you are used to working exclusively with a GUI like Windows or Mac OS, it can

be unsettling to work at the command line with MySQL. You won’t need to do it very

often, if at all. However, it’s not difficult, and here are a few pointers to make you feel

more at home:

When you work inside the MySQL monitor, most commands need to end with a

semicolon (;). The only exceptions are use databaseName and exit. The MySQL

monitor is quite happy if you use a semicolon after these two commands, so the

simple rule is this: if in doubt, put a semicolon on the end of each command.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

418

If you forget to put a semicolon at the end of a command that needs one, the

MySQL monitor will assume that you want to break your command over more than

one line, and that you haven’t finished typing. It will patiently wait for you to do so,

like this:

This enables you to spread long queries over a number of lines. Not only is this easier

to read onscreen, it’s also useful if you make an error. The MySQL monitor

remembers previous commands line by line, and you can retrieve them by pressing

the up and down arrow keys on your keyboard. Once a previous command has

been redisplayed, you can use your left and right arrow keys to move along the line

and edit it in the normal way. Once you have completed the command, just type a

semicolon and press Enter/Return. The MySQL monitor will then process it.

If you spot a mistake before pressing Enter/Return, use your left and right arrow

keys to edit the current line. If the mistake is on a previous line, there is no way to

go back. Abandon the command by typing \c. The MySQL monitor will ignore

everything you have entered and present you with the mysql> prompt.

Using MySQL with phpMyAdmin

Although you can do everything using MySQL monitor, it’s a lot easier to use a graphic

interface. There are several to choose from, both commercial and free. Among the free

offerings are two from MySQL itself: MySQL Administrator and MySQL Query Browser

(www.mysql.com/products/tools). Two other popular graphical front ends for MySQL are

the commercial product Navicat (www.navicat.com) and SQLyog (www.webyog.com), which

is available in both commercial and free versions.

However, the most popular graphical interface for MySQL is phpMyAdmin (www.phpmyadmin.

net). It’s a PHP-based administrative system for MySQL that has been around since 1998,

and it constantly evolves to keep pace with MySQL developments. It works on Windows,

Mac OS X, and Linux and currently supports all versions of MySQL from 3.23.32 to 5.0.

What’s more, many hosting companies provide it as the standard interface to MySQL.

Because phpMyAdmin has a very intuitive interface, I suggest that you try it first. If you

work with databases on a regular basis, you may want to explore the other graphical interfaces

later. However, since phpMyAdmin is free, you have nothing to lose—and you may

find it does everything you want.

SETTING UP MYSQL AND PHPMYADMIN

419

13

Setting up phpMyAdmin on Windows and Mac

These instructions are based on phpMyAdmin 2.10.1. Like a lot of open source applications,

phpMyAdmin is constantly evolving. Any changes of a substantial nature will be

listed on my website at http://foundationphp.com/egdwcs3/updates.php.

Since phpMyAdmin is PHP-based, all that’s needed to install it is to download the files,

unzip them to a website in your local testing environment, and create a simple configuration

file.

1. Go to http://www.phpmyadmin.net, and download the latest stable version. The files can

be downloaded in three types of compressed file: BZIP2, GZIP, and ZIP. Choose

whichever format you have the decompression software for.

2. Unzip the downloaded file. It will extract the contents to a folder called

phpMyAdmin-x.x.x, where x represents the version number.

3. Highlight the folder icon, and cut it to your clipboard. On Windows, paste it inside

the folder designated as your web server root (C:\htdocs, if you followed my

example). If you’re on a Mac and want phpMyAdmin to be available to all users,

put the folder in Macintosh HD:Library:WebServer:Documents rather than in your

own Sites folder.

4. Rename the folder you have just moved to this: phpMyAdmin.

5. Like Apache and PHP, phpMyAdmin uses a text file to store all the configuration

details. Since version 2.7.0, you no longer edit the phpMyAdmin configuration file

but store your personal details in a new file, which should be named

config.inc.php. There are two ways of doing this: using a built-in script called

setup.php or manually. I prefer the manual method, but instructions for both

methods follow.

Use these instructions if you want to use the built-in configuration script.

1. Create a new subfolder called config within the phpMyAdmin folder. Windows users

skip to step 3. Mac users continue with step 2.

2. On Mac OS X, use Finder to locate the config folder that you have just created.

Ctrl-click and select Get Info. In Ownership & Permissions, expand Details, and click

the lock icon so that you can make changes to the settings. Change the setting for

Others to Read & Write. Close the config Info panel.

3. Open a browser, and type the following into the address bar:

http://localhost/phpmyadmin/scripts/setup.php

If you created the phpMyAdmin folder inside your Sites folder on a Mac, use the

following address, substituting username with your Mac username:

http://localhost/~username/phpmyadmin/scripts/setup.php

Configuring phpMyAdmin with setup.php

Downloading and installing phpMyAdmin

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

420

4. You should see the page shown in Figure 13-3.

Figure 13-3. A built-in script automates the configuration of phpMyAdmin.

Ignore any warning about the connection not being secure. This is intended for

server administrators installing phpMyAdmin on a live Internet server. If, on the

other hand, you see the following warning, it means that you have not set up

the config folder correctly and should go back to step 1.

5. Click the Add button in the Servers section. This loads a form with most of the necessary

information already filled in. Check the following settings:

Server hostname: localhost

Server port: Leave blank unless your web server is running on a nonstandard

port, such as 8080

Server socket: Leave blank

Connection type: tcp

PHP extension to use: mysqli

SETTING UP MYSQL AND PHPMYADMIN

421

13

6. The default setting for Authentication type is config. If you don’t need to password

protect access to phpMyAdmin, check that User for config auth is set to root, and

enter your MySQL root password in the next field, Password for config auth.

If you want to restrict access to phpMyAdmin by prompting users for a password,

change Authentication type to http, and delete root from the User for config auth field.

7. Scroll down to the Actions field, and click Add. As shown here, there are two Add

buttons close to each other; click the one circled in the screenshot:

8. The next screen will probably warn you that you didn’t set up a phpMyAdmin database,

so you won’t be able to use all the phpMyAdmin features. This is not important.

You can set up one later if you decide to use the advanced features of

phpMyAdmin.

9. Scroll down to the Configuration section near the bottom of the page, and click

Save.

10. Open the config folder in Explorer or Finder. You should see a new file called

config.inc.php. Move it to the main phpMyAdmin folder. The official instructions

tell you to delete the config folder, but this isn’t necessary in a local testing environment.

Although setup.php automates the creation of config.inc.php, it duplicates some

default settings. If you strip out the unnecessary commands, you may find it quicker to

create the file manually.

1. If you don’t need to password protect access to phpMyAdmin, type the following

code into a blank document:

<?php

$i = 1;

$cfg['Servers'][$i]['extension'] = ‘mysqli';

$cfg['Servers'][$i]['password'] = ‘mysqlRootPassword‘;

?>

Use your own MySQL root password in place of mysqlRootPassword.

Configuring phpMyAdmin manually

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

422

If you need password protection for phpMyAdmin, use the following code instead:

<?php

$i = 1;

$cfg['Servers'][$i]['extension'] = ‘mysqli';

$cfg['Servers'][$i]['auth_type'] = ‘http';

?>

2. Save the file as config.inc.php in the main phpMyAdmin folder. Erm . . . that’s it.

Launching phpMyAdmin

To use phpMyAdmin, launch a browser, and enter http://localhost/phpMyAdmin/index.php in

the address bar (on a Mac, use http://localhost/~username/phpMyAdmin/index.php if

you put phpMyAdmin in your Sites folder). If you stored your root password in

config.inc.php, phpMyAdmin should load right away, as shown in Figure 13-4. If you

chose to password protect phpMyAdmin, enter root as the username and whatever you

specified as the MySQL root password when prompted.

Figure 13-4. phpMyAdmin is a very user-friendly and stable graphical interface to MySQL.

If you’re used to glossy software design, your initial impression of phpMyAdmin may not

be all that favorable, particularly if you don’t have a large monitor. The interface is sorely

in need of a facelift, but don’t let that fool you; phpMyAdmin is both powerful and easy

to use.

If you get a message saying that the server is not responding or that the socket is not

correctly configured, make sure that the MySQL server is running.

SETTING UP MYSQL AND PHPMYADMIN

423

13

Logging out of phpMyAdmin

If you opted to password protect phpMyAdmin, the Log out link is at

the bottom left of the front page, just beneath Import (as shown in the

screenshot alongside). When you click the link, you are immediately

prompted for your username and password. Click Cancel, and you will

be presented with a screen informing you that you supplied the wrong

username/password—in other words, you have been logged out. Odd, but that’s the way

it works.

Backup and data transfer

MySQL doesn’t store your database in a single file that you can simply upload to your website.

Even if you find the right files (on Windows, they’re located in C:\Program Files\

MySQL\MySQL Server 5.0\data), you’re likely to damage them unless the MySQL server is

turned off. Anyway, most hosting companies won’t permit you to upload the raw files,

because it would also involve shutting down their server, causing a great deal of inconvenience

for everyone.

Nevertheless, moving a database from one server to another is very easy. All it involves is

creating a backup dump of the data and loading it into the other database with

phpMyAdmin. The dump is a text file that contains all the necessary Structured Query

Language (SQL) commands to populate an individual table or even an entire database

elsewhere. phpMyAdmin can create backups of your entire MySQL server, individual databases,

selected tables, or individual tables. To make things simple, the following instructions

show you how to back up only a single database.

These instructions show you how to back up an entire database. You can also back up individual

tables in the same way by selecting the tables in step 4.

Creating a backup

If you have just installed MySQL for the first time, bookmark this section for

when you need to upload files to your remote server or upgrade MySQL. If

you’re on a Mac, you must always back up your data before upgrading MySQL.

Once the new version has been installed, you can transfer your data to the new

server. Windows users need to follow this procedure only when upgrading from

one series to another, such as 5.0 to 5.1.

You cannot log back in to phpMyAdmin from the wrong username/password screen.

You must enter the original URL into the browser address bar first.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

424

1. Launch phpMyAdmin, and select the database that you want to back up from the

drop-down menu in the navigation frame.

2. When the database details have loaded into the main frame, select Export from the

tabs along the top of the screen, as shown here.

3. The rather fearsome-looking screen shown in Figure 13-5 opens. In spite of all the

options, you need to concern yourself with only a few.

Figure 13-5. phpMyAdmin offers a wide range of choices when exporting data from MySQL.

SETTING UP MYSQL AND PHPMYADMIN

425

13

4. The Export section on the left of the screen lists all the tables in your database.

Click Select All, and leave the radio buttons on the default SQL.

5. If the database has never been transferred to the other server before, the only

option that you need to set on the right side of the screen is the drop-down menu

labeled SQL compatibility mode. The setting depends on the version of MySQL on

the other server (only the first two numbers, such as 3.23, 4.0, 4.1, or 5.0, are

important):

If the other server is running the same version of MySQL, choose NONE.

If you are transferring between MySQL 4.1 and MySQL 5.0 (in either direction),

choose NONE.

If the other server is running MySQL 3.23, choose MYSQL323.

If the other server is running MySQL 4.0, choose MYSQL40.

6. If the database has already been transferred on a previous occasion, select Add

DROP TABLE in the Structure section. The existing contents of each table are

dropped and are replaced with the data in the backup file.

7. Put a check mark in the box alongside Save as file at the bottom of the screen. The

default setting in File name template is __DB__, which automatically gives the

backup file the same name as your database. So, in this case, it will become

egdwcs3.sql. If you add anything after the final double underscore, phpMyAdmin

will add this to the name. For instance, you might want to indicate the date of the

backup, so you could add 20070704 for a backup made on July 4, 2007. The file

would then be named egdwcs320070704.sql.

1. If a database of the same name doesn’t already exist on the target server, create

the database, but don’t create any tables.

2. Launch the version of phpMyAdmin that is used by the target server, and select the

database that you plan to transfer the data to. Click the Import tab in the main

frame (on versions of phpMyAdmin earlier than 2.7.0, click the SQL tab instead).

3. Use the Browse button to locate the SQL file on your local computer, and click Go.

That’s it!

Because phpMyAdmin uses PHP to upload the file, the maximum size of any backup is

normally limited to 2MB, which is the default maximum size for any file upload. If you

are transferring a very large database, use the phpMyAdmin export and import tabs to

backup and transfer individual tables. Alternatively, contact your hosting company for

advice on transferring your database.

Loading data from a backup file

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

426

Looking ahead . . .

Now that you have MySQL and phpMyAdmin installed, we can finally begin to explore

Dreamweaver’s server behaviors. In the next chapter, I’ll show you how to create a database

table in MySQL and insert into it user input from the feedback form in Chapter 9.

You’ll also learn how to combine it with the mail processing script from Chapters 11 and

12 and how to retrieve and display information stored in a database.

SETTING UP MYSQL AND PHPMYADMIN

427

13

14 STORING RECORDS

IN A DATABASE

Unlike Access or FileMaker Pro, MySQL doesn’t come with predesigned forms. Instead, you

build and design everything yourself. While this presents a challenge to the first-time user,

MySQL isn’t difficult to use, and Dreamweaver takes a lot of the hard work out of integrating

MySQL with your website.

Let’s say that you decide to start accepting orders for goods and services through your

website. As well as getting the orders by email, you need to store that information in a

database. Rather than input all the data again manually, it makes much more sense to

combine the two operations. So by the end of this chapter, you will be able to input data

directly from the feedback form from Chapters 9, 11, and 12, and then send the details to

your mail inbox. In the process, you’ll learn the basics of database construction, and how

to handle different types of data. Specifically, you’ll learn how to

Create MySQL user accounts

Define a database table

Create a database connection in Dreamweaver

Insert form input into a database

Use a recordset to retrieve data and display database results

Apply a repeat region to display multiple records

Merge the mail processing script with database input

Setting up a database in MySQL

If you set up MySQL and phpMyAdmin in a local testing environment, as described in the

last chapter, launch phpMyAdmin, and open the Database drop-down menu in the left

frame (see Figure 14-1).

MySQL isn’t a single database, but a relational database management system (RDBMS). The

first database listed, information_schema, is a virtual database that contains details of

other databases within the RDBMS. The second one, mysql, contains all the user account

and security information and should never be edited directly unless you’re really sure what

you’re doing. The final database, test, contains nothing. The numbers in parentheses indicate

how many tables each database contains.

Figure 14-1.

A new installation of MySQL contains

three default databases.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

430

If you’re using a remote server, and your hosting company provides phpMyAdmin, the list

of databases will be limited to those on your account, or you may be limited to only one

database.

Creating a local database for testing

Assuming that you have set up a local testing environment, you need to create a test database

to work with the remaining chapters. I’m going to call the database egdwcs3. To make

life easier for yourself when it comes to testing pages on the Internet, use the name of a

database on your remote server.

Type the name of the database in the field labeled Create new database in the

phpMyAdmin welcome screen, and click Create, as shown in Figure 14-2. Leave Collation in

its default position. However, if you’re working in a language other than English, Swedish,

or Finnish, and your remote server runs MySQL 4.1 or later, skip ahead to the section

“Understanding collation” before going any further.

Figure 14-2. To create a new database, just type its name into

the phpMyAdmin welcome screen, and click Create.

The database should be created instantly, and phpMyAdmin will invite you to create a new

table. Before doing that, you need to create at least one user account for the database.

Leave phpMyAdmin open.

Because phpMyAdmin is a browser-based application, the precise layout of what you

see onscreen depends on the size of your monitor and browser viewport.

STORING RECORDS IN A DATABASE

431

14

Creating user accounts for MySQL

At the moment, your installation of MySQL has only one registered user—the superuser

account called “root,” which has complete control over everything. A lot of beginners

think that once they have set up a password for the root user, they can start building databases.

This is a big mistake. The root user should never be used for anything other than

administration.

MySQL stores all databases in a common directory. So, on shared hosting, your database—

with all its precious information—rubs shoulders with everyone else’s. Clearly, you need a

way to prevent unauthorized people from seeing or altering your data. The answer is to

create user accounts that have the fewest number of privileges necessary to perform

essential tasks, preferably on a single database.

Granting the necessary user privileges

You normally want visitors to your site to be able to see the information it contains but not

to change it. However, as administrator, you need to be able to insert new records, and

update or delete existing ones. This involves four types of privileges, all named after the

equivalent SQL commands:

SELECT: Retrieves records from database tables

INSERT: Inserts records into a database

UPDATE: Changes existing records

DELETE: Deletes records but not tables or databases (the command for that is DROP)

In an ideal setup, you create two separate user accounts: one for administrators, who

require all four privileges, and another one for visitors, limited to SELECT. If your hosting

company lets you set up user accounts with different privileges, I suggest that you create

two accounts like this. However, if you have no choice, set up one account and use the

same username and password as on your remote server.

These instructions show you how to set up user accounts in a local testing environment.

You can skip this section if you are using your remote server as your testing server.

1. Click the home icon at the top of the left frame in phpMyAdmin to return to the

welcome screen, and then click Privileges, as shown in the following screenshot:

Setting up MySQL user accounts

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

432

2. The User overview screen opens. Click Add a new User halfway down the page.

3. In the page that opens, enter the name of the user account that you want to create

in the User name field. Select Local from the Host drop-down menu. This automatically

enters localhost in the field alongside. This option restricts the user to connecting

to MySQL only from the same computer. Enter a password in the Password

field, and confirm it in the Re-type field. The Login Information table should look

like this:

The Privileges tab at the top of the previous screen displays a list of current user

accounts. To create a new user account, you must use the link in the welcome screen.

STORING RECORDS IN A DATABASE

433

14

4. Beneath the Login Information table is one labeled Global privileges. Granting such

extensive privileges is insecure, so scroll past the Global privileges table, and click

the Go button right at the bottom of the page.

5. The next page confirms that the user has been created and displays many options,

beginning with the Global privileges again. Scroll down to the section labeled

Database-specific privileges. Activate the drop-down menu, as shown here, to display

a list of all databases. Select the name for the database you plan to use for

testing.

6. The next screen allows you to set the user’s privileges for just this database. You

want the admin user to have all four privileges listed earlier, so click the checkboxes

next to SELECT, INSERT, UPDATE, and DELETE (if you hover your mouse

pointer over each option, phpMyAdmin displays a tooltip describing what it’s for).

After selecting the four privileges, as shown here, click the top Go button.

Dreamweaver needs these details later to make a connection to the database.

If you want to use the download files exactly as they are, use humpty as

the password for egadmin.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

434

7. phpMyAdmin presents you with the following confirmation that the privileges have

been updated for the user account:

8. The page displays the Database-specific privileges table again, in case you need to

change anything. Assuming you got it right, click the Privileges tab at the top right

of the page. You should now see the new user listed in the User overview.

If you ever need to make any changes to a user’s privileges, click the Edit Privileges

icon to the right of the listing, as shown. You can also delete users by selecting the

checkbox to the left of the User column, then clicking Go.

9. If your hosting company permits you to create multiple user accounts, click Add a

new User, and repeat steps 3–8 to create a second user account. If you want to use

the same username and password as in the download files, call the account eguser,

and give it the password dumpty. This user will have restricted privileges, so in

step 6, check only the SELECT option.

Now that you have a database and at least one user account, you can build the table to

store the feedback information. However, first, you need to understand the principles

behind table construction.

phpMyAdmin frequently offers you a variety of options on the same page, each of

which normally has its own Go button. Always click the one at the foot of or alongside

the section that relates to the options you want to set.

STORING RECORDS IN A DATABASE

435

14

How a database stores information

All data in MySQL is stored in tables, with information organized into rows and columns

very much like a spreadsheet. Figure 14-3 shows a simple database table as seen in

phpMyAdmin.

Figure 14-3. Information in a database table is stored in rows and columns, just like in a

spreadsheet.

Each column has a name (image_id, filename, and caption) indicating what it stores.

The rows aren’t labeled, but the first column (image_id) contains a unique identifier

known as a primary key, which can be used to identify the data associated with a particular

row. Each row contains an individual record of related data. The significance of primary

keys is explained in the next section.

The intersection of a row and a column, where the data is stored, is called a field. So, for

instance, the caption field for the third record in Figure 14-3 contains the value “The

Golden Pavilion in Kyoto” and the primary key for that record is 3.

How primary keys work

Although Figure 14-3 shows image_id as a consecutive sequence from 1 to 8, they’re not

row numbers. Figure 14-4 shows the same table with the captions sorted in alphabetical

order. The field highlighted in Figure 14-3 has moved to the seventh row, but it still has the

same image_id and filename.

The terms “field” and “column” are often used interchangeably. A field holds

one piece of information for a single record, whereas a column contains the

same field for all records.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

436

Figure 14-4. Even when the table is sorted in a different order, each record can be identified

by its primary key.

Although the primary key is rarely displayed, it identifies the record and all the data stored

in it. If you know the primary key, you can update a record, delete it, or use it to display

data. Don’t worry about how you find the primary key; it’s easy using Structured Query

Language (SQL), the standard means of communicating with all major databases. The

important thing is to assign a primary key to every record.

A primary key doesn’t need to be a number, but it must be unique.

Social Security, staff ID, or product numbers make good primary keys. They may

consist of a mixture of numbers, letters, and other characters but are always

different.

MySQL will generate a primary key for you automatically.

Once a primary key has been assigned, it should never—repeat, never—be changed.

Because a primary key must be unique, MySQL doesn’t normally reuse the number when a

record is deleted, leaving holes in the sequence. Don’t even think about renumbering. By

changing the numbers to close the gaps, you put the integrity of your database at serious

risk. Some people want to remove gaps to keep track of the number of records, but you

can easily get the same information with SQL.

Although Figures 14-3 and 14-4 show the similarity between a database table and a

spreadsheet, there’s an important difference. With a spreadsheet, you can enter data without

the need to specify beforehand what type of data it is or how it’s to be structured. You

can’t do that with a database.

Designing a database table

Before entering data, you need to define the table structure. This involves the following

decisions:

The name of the table

How many columns it will have

The name of each column

What type of data will be stored in each column

Whether the column must always have data in each field

Which column contains the table’s primary key

STORING RECORDS IN A DATABASE

437

14

Don’t be tempted to choose the first thing that comes into your head. Experienced database

developers often say at least half the total development time is spent deciding the

structure of a database. Although the structure of a database can be altered, some decisions

tie your hands so badly you need to redesign everything from scratch. That’s not

much fun when the database contains several thousand records. The time spent on these

early decisions can save a lot of agony and frustration later on.

Because each database is different, it’s impossible to prescribe one simple formula, but the

next few pages should help guide you in the right direction. Don’t attempt to commit

everything to memory at the first read-through. Come back later when you need to

refresh your memory or check a particular point.

Choosing the table name

The basic MySQL naming rules for databases, tables, and columns are as follows:

Names can be up to 64 characters long.

Legal characters are numbers, letters, the underscore, and $.

Names can begin with a number but cannot consist exclusively of numbers.

Some hosting companies seem blissfully ignorant of these rules and assign clients databases

that contain one or more hyphens (an illegal character) in their name. If a name contains

spaces or illegal characters, you must surround it by backticks (`) in SQL queries.

Note that this is not a single quote (‘) but a separate character. Dreamweaver and

phpMyAdmin normally do this for you automatically.

Choose names that are meaningful. Tables hold groups of records, so it’s a good strategy

to use plural nouns. For example, use products rather than product. Don’t try to save on

typing by using abbreviations, particularly when naming columns. Explicit names make it

much easier to build SQL queries to extract the information you want from a database.

SQL is designed to be as human-readable as possible, so don’t make life difficult for yourself

by using cryptic naming conventions.

When choosing column names, there is a danger that you might accidentally choose

one of MySQL’s many reserved words (http://dev.mysql.com/doc/refman/5.0/en/

reserved-words.html), such as date or time. A good technique is to use compound

words, such as arrival_date, arrival_time, and so on. These names also tell you much

more about the data held in the column.

Case sensitivity of names Windows and Mac OS X treat MySQL names as case insensitive.

However, Linux and Unix servers respect case sensitivity. To avoid problems when transferring

databases and PHP code from your local computer to a remote server, I recommend

that you use only lowercase in database, table, and column names. Using camel case (e.g.,

arrivalDate) is likely to cause your code to fail when transferring a database from your

local computer to a Linux server.

Deciding how many columns to create

How should you store each person’s name? One column? Or one each for the family and

personal names? A commercial contacts management program like Microsoft Outlook

goes even further, splitting the name into five parts. In addition to first and last name, it

stores title (Mr., Mrs., etc.), a middle name, and suffix (I, II, III, Jr., and Sr.). Addresses are

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

438

best broken down into street, town, county, state, ZIP code, etc. Think of all the possible

alternatives, and add a column for each one. Things like company name, apartment number,

and extra lines in an address can be made optional, but you need to make provision

for them. This is an important principle of a relational database: break down complex

information into its component parts, and store each part separately.

This makes searching, sorting, and filtering much easier. Breaking information into small

chunks may seem a nuisance, but you can always join them together again. It’s much easier

than trying to separate complex information stored in a single field.

Choosing the right column type in MySQL

MySQL 5.0 has 28 different column types. Rather than confuse you by listing all of them,

I’ll explain just the most commonly used. You can find full details of all column types in the

MySQL documentation at http://dev.mysql.com/doc/refman/5.0/en/data-types.html.

Storing text The difference between the main text column types boils down to the maximum

number of characters that can be stored in an individual field, and whether you can

set a default value.

CHAR: A fixed-length width text column up to a maximum of 255 characters. You

must specify the size when building the table, although this can be altered later.

Shorter strings are OK. MySQL adds trailing space to store them, and automatically

removes it on retrieval. If you attempt to store a string that exceeds the specified

size, excess characters are truncated. You can define a default value.

VARCHAR: A variable-length character string. The maximum number must be specified

when designing the table, but this can be altered later. Prior to MySQL 5.0, the

limit is 255; this has been increased to 65,535 in MySQL 5.0. Another change in

MySQL 5.0 affects the way trailing space is treated. Prior to MySQL 5.0, trailing

space is stripped at the time of storing a record. Since MySQL 5.0, trailing space is

retained for both storage and retrieval. You can define a default value.

TEXT: Stores a maximum of 65,535 characters (slightly shorter than this chapter).

You cannot define a default value.

TEXT is convenient, because you don’t need to specify a maximum size (in fact, you can’t).

Although the maximum length of VARCHAR is the same as TEXT in MySQL 5.0, other factors

such as the number of columns in a table reduce this.

Prior to MySQL 5.0, you cannot use CHAR in a table that also contains VARCHAR, TEXT, or

BLOB. When creating the table, MySQL silently converts any CHAR columns to VARCHAR.

Storing numbers The most frequently used numeric column types are as follows:

TINYINT: Any whole number (integer) between –128 and 127. If the column is

declared as UNSIGNED, the range is from 0 to 255. This is particularly suitable for

storing people’s ages, number of children, and so on.

INT: Any integer between –2,147,483,648 and 2,147,483,647. If the column is

declared as UNSIGNED, the range is from 0 to 4,294,967,295.

Keep it simple: use VARCHAR for short text items and TEXT for longer ones.

STORING RECORDS IN A DATABASE

439

14

FLOAT: A floating-point number.

DECIMAL: A floating-point number stored as a string. This column type is best

avoided.

DECIMAL is intended for currencies, but you can’t perform calculations with strings inside a

database, so it’s more practical to use INT. For dollars or euros, store currencies as cents;

for pounds, use pence. Then use PHP to divide the result by 100, and format the currency

as desired.

Storing dates and times MySQL stores dates in the format YYYY-MM-DD. This may come as a

shock, but it’s the ISO (International Organization for Standardization) standard, and

avoids the ambiguity inherent in national conventions. The most important column types

for dates and times are as follows:

DATE: A date stored as YYYY-MM-DD. The supported range is 1000-01-01 to 9999-12-31.

DATETIME: A combined date and time displayed in the format YYYY-MM-DD

HH:MM:SS.

TIMESTAMP: A timestamp (normally generated automatically by the computer).

Legal values range from the beginning of 1970 to partway through 2037.

Storing predefined lists MySQL lets you store two types of predefined list that could be

regarded as the database equivalents of radio button and checkbox states:

ENUM: This column type stores a single choice from a predefined list, such as “yes,

no, don’t know” or “male, female.” The maximum number of items that can be

stored in the predefined list is a mind-boggling 65,535—some radio-button group!

SET: This stores zero or more choices from a predefined list, up to a maximum of

64. Although this violates the principle of storing only one piece of information in

a field, it’s useful when the items form a coherent unit (e.g., optional extras on a car).

The values stored in the ENUM and SET columns are stored as a comma-separated string.

Individual values can include spaces and other characters but not commas.

MySQL timestamps are based on a human-readable date and, since MySQL 4.1,

use the same format as DATETIME. As a result, they are incompatible with Unix

and PHP timestamps, which are based on the number of seconds elapsed since

January 1, 1970. Don’t mix them.

Don’t use commas or spaces as the thousands-separator. Apart from numerals,

the only characters permitted in numbers are the negative operator (-) and the

decimal point (.). Although some countries use a comma as the decimal point,

MySQL accepts only a period.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

440

Storing binary data Binary data, such as images, bloat your tables and cannot be displayed

directly from a database. However, the following column types are designed for binary

data:

TINYBLOB: Up to 255 bytes

BLOB: Up to 64KB

MEDIUMBLOB: Up to 16MB

LONGBLOB: Up to 4GB

With such whimsical names, it’s a bit of a letdown to discover that BLOB stands for binary

large object.

Deciding whether a field can be empty

When defining a database table, specifying a column as NOT NULL is the equivalent of designating

a required field. Since the phpMyAdmin default is NOT NULL, you need to manually

override this to make a field optional. You can change a column definition from NOT

NULL to NULL and vice versa at any time.

Storing input from the feedback form

It’s time to put the knowledge from the preceding section to practical use by building a

table to store the information from the feedback form from Chapters 9, 11, and 12. The

form is available in the download files, so you can dive straight in. Don’t be put off by the

fact that it’s a feedback form; working with it shows you all the basic techniques you need

for inserting records into a database.

The name for the database table needs to give a clear indication of what it contains, so I’ll

call the table feedback. The next step is to analyze the form and decide how the feedback

table should be structured.

Analyzing the form

There are seven fields in the form (see Figure 14-5), so you need at least seven columns.

You need another column for the primary key. Anything else?

If you set a default value for a NOT NULL column, MySQL automatically uses that

value if nothing is entered in the field. Unfortunately, Dreamweaver doesn’t

support this useful feature.

STORING RECORDS IN A DATABASE

441

14

Figure 14-5. The names of the form fields often make good names for database columns.

In a real-world situation the family and given names should be stored separately, but I’m

going to skip that here, because both are text fields, so they are handled in the same way.

The one extra field that I’m going to add will store the date and time that the form was

submitted. So that makes a total of nine columns. As for column names, the name attributes

of the form fields make a good choice, and as you’ll see shortly, using them makes it

a lot easier to use the Dreamweaver server behaviors. For the two extra fields, let’s use

message_id for the primary key, and submitted for the date.

The next step is to decide the column types (refer to “Choosing the right column type in

MySQL”). By convention, the primary key column is normally the first one in a table.

MySQL has a feature called auto_increment, which automatically assigns the next available

number. This is ideal for a primary key, so we’ll make the column an INT type. We don’t

want negative numbers, so we’ll also make it UNSIGNED. This gives a range of nearly 4.3 billion,

which is probably excessive for most tables. However, the danger of choosing a

smaller number type is that you run out of numbers, particularly if records are added and

deleted frequently. It’s much better to err on the side of caution with the primary key

column.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

442

The first two form fields, name and email, contain short, variable amounts of text, so

VARCHAR is appropriate. With VARCHAR you need to specify the maximum number of characters.

For name, 50 is sufficient, but you don’t want to risk truncating an email address, so

100 is a safer choice for email.

The comments field is also text. If your remote server is running MySQL 5.0 or higher, you

can use VARCHAR and set a limit of 500 or 1000, but TEXT is appropriate for all versions, so

we’ll use that. To limit the length of comments, use the Spry Textarea Validation Widget as

described in Chapter 9.

The checkbox group interests presents a dilemma. Unless you make this a required field,

users can pick anything from none to five. Storing more than one piece of information in

a field goes against the principles of good database design, but creating five separate

columns isn’t very satisfactory either. What happens if you want to add another category

to the list, or remove one? Since interests are grouped together, and represent a series

of closely related options, this is where SET comes in handy.

The drop-down menu visited allows only one choice. Although this sounds like a good

candidate for ENUM, you may want to change the range of options later, so this is better as

VARCHAR. That way, you can change the value attributes of the <option> tags without

needing to change the table definition.

The views multiple-choice list is similar to the checkbox group, so that will be another SET.

The subscribe radio group is a straight yes/no choice, so it should be an ENUM column

type.

Finally, submitted needs to store the date and time, so we’ll make it a TIMESTAMP column.

Whenever a record is inserted or updated, the current date and time are automatically

inserted into the first TIMESTAMP column in a table. With a DATETIME column, you have to

insert the value explicitly.

Table 14-1 summarizes this analysis. You’ll use this summary to define the feedback table

in phpMyAdmin, so the table headings use the same terminology as phpMyAdmin. The

setting for Length/Values for the two SET columns is described in the instructions in

“Defining the feedback table.” Note that interests and views are specified as the only

fields not required by setting them to null. I’ve done this because the Dreamweaver Insert

Record server behavior can’t handle SET columns automatically. So we’ll come back to

these two later.

STORING RECORDS IN A DATABASE

443

14

Table 14-1. Column settings for the feedback table

Field Type Length/Values Attributes Null Extra Primary key

message_id INT UNSIGNED not null auto_increment Selected

name VARCHAR 50 not null

email VARCHAR 100 not null

comments TEXT not null

interests SET See text null

Continued

Defining a table in phpMyAdmin

Defining a database table normally requires writing a lengthy SQL query, but phpMyAdmin

makes the process a lot simpler through a form-based interface. The form is quite wide

and, unless you have a large monitor, you might need to scroll horizontally to see all the

fields. You might find the text in some screenshots hard to read, but all important information

is repeated in the instructions and Table 14-1.

1. Launch phpMyAdmin, and select the egdwcs3 database. In the main frame, type

feedback in the Name field, enter 9 as the Number of fields, and click Go.

2. This opens the form shown in Figure 14-6 with nine blank rows where you enter

the column definitions. Copy the values from Table 14-1. Designate message_id as

the table’s primary key by selecting the radio button as indicated in Figure 14-6.

Figure 14-6. Defining the columns for the feedback table in phpMyAdmin

For the SET and ENUM columns, you need to enter in Length/Values the value attributes

from the related form fields as a series of comma-separated strings. Each

string needs to be enclosed in single quotes. So, for interests, it looks like this:

‘Classical concerts’, ‘Rock/pop’, ‘Drama’, ‘Guided walks’, ‘Art’

February 28, 2009 Posted by | Uncategorized | 1 Comment

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP 02

The full range of options is as follows:

None: Don’t use—all pages should have a DOCTYPE declaration.

HTML 4.01 Transitional: Choose this if you don’t want to use XHTML.

HTML 4.01 Strict: This excludes deprecated elements (those destined for eventual

elimination)—use this only if you have a good knowledge of HTML and have made

a conscious decision not to use XHTML.

XHTML 1.0 Transitional: This offers the same flexibility as HTML 4.01 Transitional by

permitting the use of deprecated elements but applies the stricter rules of XML.

XHTML 1.0 Strict: This excludes all deprecated elements—use this only if you are

competent with XHTML.

XHTML 1.1: Don’t use—this DTD should not be used on pages delivered using the

text/html MIME type, the current standard for web servers.

XHTML Mobile 1.0: This is a subset of XHTML Basic for mobile devices—you can find

the full specification at http://www.openmobilealliance.org/tech/affiliates/wap/

wap-277-xhtmlmp-20011029-a.pdf.

If you choose an HTML document type, Dreamweaver automatically creates code according

to the HTML specification. Similarly, if you choose XHTML, your code automatically

follows the stricter rules, using lowercase for tag names and event handlers and inserting

a closing slash in empty tags such as <img>. You need to be careful when copying and pasting

code from other sources. If you’re not sure about the quality of the code, run

Commands ä Clean Up XHTML, which should correct most, if not all, problems.

If you select a Strict DTD, it’s important to realize that Dreamweaver does not prevent you

from using deprecated elements or attributes. Dreamweaver expects you to understand

the difference yourself.

Choosing the default encoding The decision to switch the default encoding in

Dreamweaver CS3 to Unicode (UTF-8) makes a lot of sense. Unicode supports nearly every

known writing system, so—as long as you have the right fonts on your computer—you can

combine Spanish, Russian, Chinese, and English all on the same web page. All modern

browsers support UTF-8, so there is no reason you shouldn’t use it. But—and it’s a big

but—this book concentrates heavily on using PHP and the MySQL database. Versions of

MySQL prior to the 4.1 series do not support UTF-8. If your hosting company uses MySQL

3.23 or 4.0, you might need to change the default encoding for your web pages. See

“Understanding collation” in Chapter 13.

Exploring the workspace

1-4 shows the default Windows workspace with a web page under construction and

all the main areas labeled. The main part of the workspace is occupied by the Document

window, which includes everything from the Document toolbar to the Tag selector.

DREAMWEAVER CS3—

7

1

1-4. The Dreamweaver workspace remains basically unchanged.

As you can see from 1-5, the Mac workspace is virtually the same. In harmony with

other Mac programs, the Close, Minimize, and Zoom buttons are at the top left of the

Document window. The Document window’s tabbed interface is displayed only when more

than one document is open. If you want the Mac version to display tabs all the time, open

Preferences from the Dreamweaver menu, select the General category, and check the

option labeled Always show tabs. Alternatively, if you don’t want the tabbed interface, deselect

the Open documents in tabs option.

Two other points to note about the Mac workspace: you can close a tab by clicking the 5

in a circle to the left of the file name; and the Property inspector overlaps the Files panel

on a 10245768 resolution monitor (the minimum display required for Dreamweaver CS3).

As a result, on a small monitor the Property inspector flops in front of or behind the Files

panel, depending on whether it has focus. This results in some icons being hidden, but you

can bring them back into view by clicking in any blank space in the Property inspector.

Alternatively, resize the Files panel to make room.

The main menus run across the top just below the title bar. The menus provide access to

all features, but I prefer to work directly in the workspace with Dreamweaver’s visual tools,

each of which I’ll describe briefly.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

8

1-5. Apart from a few minor differences, the Mac workspace is identical to Windows.

Insert bar

The Insert bar is really a collection of toolbars used to perform the most frequently used

operations in building web pages. It’s organized as a tabbed interface. 1-6 shows the

Common tab. When you first launch Dreamweaver, there are six other tabs (or categories).

Additional, context-sensitive tabs are displayed only when the features can be used in a

particular page, such as when using PHP or the XSL Transformation server behavior. The

tabs organize program features in logical groups, so some commonly used features, such

as tables and <div> tags, are duplicated on more than one tab to save time switching back

and forth.

1-6. The Common tab of the Insert bar houses some of the most frequently used operations.

If space is at a premium, you can save a few pixels of vertical space by switching to the

menu style shown in 1-7. Click the name at the left end to reveal a menu of available

categories. However, it takes two clicks to change categories, so you’ll probably find

the tabbed interface more convenient. Alternatively, you can put frequently used items in

the Favorites category as described shortly.

1-7. The Insert bar has an alternative menu style that saves a little space.

DREAMWEAVER CS3—

9

1

To use the menu style, click the panel Options menu button (shown alongside) at the top

right of the Insert bar, and select Show as Menu. To restore the tabbed interface, click the

category name at the left end of the Insert bar, and select Show as Tabs from the bottom

of the menu.

Table 1-1 describes briefly what each category contains. Although the Insert bar will look

familiar to users of previous versions of Dreamweaver, it has been revamped with many

new items and the removal of some old ones. For the benefit of readers upgrading from

Dreamweaver 8, I have indicated the main changes.

Table 1-1. The main features of Insert bar tabs (categories)

Tab/category Description Changes from Dreamweaver 8

Common Inserts the most commonly used Now contains all tags related

objects, such as tables, images, with <head> and <script>

and <div> tags. from the old HTML category.

Layout Offers various tools for layout, Layout Mode was removed

including table modification, but remains accessible

frames, and Spry widgets, such through View ä Table Mode ä

as menu bar (see Chapter 7), Layout Mode.

and tabbed and collapsible

panels (see Chapter 8).

Forms Creates forms and inserts all form No change, apart from the

elements, including Spry validation addition of Spry validation

widgets (forms and Spry validation widgets.

are covered in Chapter 9).

Data Offers access to most dynamic Previously the Application

features, including Spry data sets category. Import Tabular Data

(see Chapter 19) and PHP server (for CSV files) has been

behaviors (see Chapter 14 onward). relocated here from the

Also imports data from comma- Layout category. Addition of

separated value (CSV) files into Spry data features.

a static web page.

Spry All Spry features gathered in a New.

single category.

Text Provides an alternative to the Font Tag Editor was removed.

Property inspector for common

formatting options. Also, home

to definition list and HTML entities.

Favorites Left blank for you to customize. For backward compatibility,

gives access to the HTML and

Flash elements categories,

which have been removed

from the main Insert bar.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

10

Selecting options from the Insert bar To see what each button is

for, hold your mouse pointer over it, and a tooltip appears. Some

buttons have a little down arrow alongside them to the right. The

first time you click one of these buttons, Dreamweaver displays a

menu showing all options, as shown in the screenshot alongside.

Dreamweaver remembers your selection and always displays the

most recently used option. If you want to use the same option

again, click the button itself. To select a different option, click the

arrow to the right to reveal the menu again.

Customizing the Favorites category If switching among categories becomes too timeconsuming,

you can populate the Favorites category with your most frequently used items.

Select the Favorites tab, right-click, and select Customize Favorites from the context menu.

The drop-down menu at the top left of the Customize Favorite Objects dialog box (shown

in 1-8) lets you choose either from a master list or from individual categories. In the

left panel, select one item at a time, and click the button with the double chevron to add

it to the Favorite objects panel on the right. To remove an item, select it in the right panel,

and click the trash can button at the top right. The up and down arrows can be used to

change the position of the selected item, and the Add separator button inserts a separator

after the current item.

1-8. You can customize the Favorites category of the Insert bar for quick

access to frequently used options.

The Customize Favorite Objects dialog box gives access to all Insert bar categories, including

context-sensitive ones, such as PHP, as well as to items that have been removed since

Dreamweaver 8.

To copy your Favorites to a different computer, follow the instructions at the end of the

next section.

Document window

By default, Dreamweaver displays each web page in a tabbed interface. Tabs are created

left to right in the same order as the pages are opened, but you can drag and drop them

DREAMWEAVER CS3—

11

1

into any order. You can also tidy up the workspace by right-clicking any tab to reveal a

context menu that, among other things, lets you close the individual tab, close all tabs, or

close them all except the current one.

Document toolbar Running across the top of the Document window is the Document toolbar,

shown in 1-9. The three buttons on the left are the most important, as they let

you switch quickly between Code view, Design view, and a combination of both called Split

view, as shown in Figures 1-4 and 1-5. The Live Data view button is displayed only in

dynamic pages, such as a PHP page. It processes server-side code to show you a good

approximation of what the page will look like when parsed by a web server. Before you can

use Live Data view, you need to define a testing server as described in Chapter 4.

1-9. The Document toolbar mainly controls how your main work environment looks.

The following list briefly describes the other options on the Document toolbar:

Title: This is where you enter the document title that is displayed in the browser

title bar.

File management: This offers a quick way of uploading and downloading the current

file to and from your remote server. Setting the connection details is covered in

Chapter 4.

Preview in browser: This displays the current page in a browser or Device Central

(see “Checking what your page will look like in other media” later in this chapter).

Refresh: This refreshes Design view. It’s used only when you’re working in the

underlying code in Split view. Otherwise, Design view refreshes automatically.

View options: This turns rulers and guides on and off.

Visual aids: This controls the CSS visual aids described in “Using visual aids to understand

your CSS structure” later in this chapter.

Validate: This option checks your document, selected files, or the entire site against

World Wide Web Consortium (W3C) standards. Dreamweaver’s validator misses

some errors, particularly when checking pages against a Strict Document Type

Definition (DTD). Double-check against the official W3C Markup Validation Service

at http://validator.w3.org.

Check page: This runs checks for browser compatibility (see “Checking for browser

bugs” later in the chapter) and accessibility.

Code view This is where you work directly with all the XHTML, PHP, and other code that

controls your web page. Even if you rarely touch the code yourself, it’s important to

understand what’s happening in Code view. PHP code inserted in the wrong place will

bring your page down like a house of cards.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

12

Design view This renders your page as it should look in a standards-compliant browser.

The CSS rendering in Dreamweaver CS3 is generally very accurate, but don’t fall into the

trap of regarding Dreamweaver as a WYSIWYG (what you see is what you get) web page

builder. It’s an excellent visual design tool, but you need a solid understanding of XHTML

and CSS to use it to its best advantage.

Tag selector The status bar at the bottom of the Document window displays a hierarchical

view of the document indicating where the insertion point is at any given moment. As

1-10 shows, clicking one of the tags in the Tag selector highlights the element in

both Design view and Code view. This is extremely useful for editing an element or applying

a dynamic behavior to it. Right-clicking a tag in the Tag selector brings up a context

menu with a useful selection of editing options, including the ability to add an ID or a class

to the element. The Tag selector fulfils a similar role to the GoLive Markup Tree bar.

1-10. Clicking a tag in the Tag selector highlights the

element in Design view, as well as the underlying code.

Property inspector This context-sensitive panel gives direct access to all the main attributes

of the currently selected element. It’s equivalent to the Inspector palette in GoLive.

Panel groups The panel groups, which are displayed by default on the right of the screen,

give you access to more detailed or specialized features. Clicking a panel group’s title bar

toggles it open and closed. The most important one is the Files panel, which not only displays

the file hierarchy of your site but also ensures that all internal links are updated and

controls uploading and downloading to and from your remote server.

DREAMWEAVER CS3—

13

1

Organizing your workspace

The Windows version of Dreamweaver CS3 comes with three workspace layouts: Designer,

Coder, and Dual Screen. Designer is the default layout shown in 1-4. Coder puts the

panel groups on the left of the screen with the Property inspector collapsed. Dual Screen

detaches the panel groups and Property inspector and opens a separate Code Inspector,

ready for you to arrange as you like. The Mac version has two preset options: the Default

layout shown in 1-5 and the Dual Screen option.

To select a layout, go to Window ä Workspace Layout, and choose the layout you want

to use.

Rearranging panels

The preset layouts are just a start. You can undock any

panel group by hovering your mouse pointer over the

left side of a panel’s title bar until it turns into a fourheaded

arrow, as shown alongside (or a hand on the

Mac version). Hold down your mouse button, and drag

the panel to wherever you want.

Saving and sharing customized workspace layouts

In addition to undocking panel groups, you can reorganize the panels into different groups

to suit your own preferences or to reflect different priorities for various projects. To move

a panel, open its parent panel group, and select the panel you want to move. Then rightclick

the icon on the right of the panel group’s title bar to display the Options menu, as

shown in the following screenshot. You can choose to move the panel to an existing group

or to create a new panel group. The menu also offers other options, including renaming

the panel group. (The same options are available from a much larger menu if you use your

main mouse button.)

Once everything is the way you want it, save the new layout by choosing Window ä

Workspace Layout ä Save Current. The name of your customized workspace appears at the

top of the Workspace Layout submenu. There is no limit to the number of preset layouts

that you can create, and you can switch freely between layouts without restarting the program.

To rename or remove customized layouts, use the Manage option at the bottom of

the Workspace Layout submenu.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

14

You can transfer customized layouts to another computer by copying the XML files that

store the details. In Windows XP, the files are in C:\Documents and Settings\<username>\

Application Data\Adobe\Dreamweaver 9\Configuration\Workspace, where <username>

is the Windows account to which you are currently logged in. In Vista, they’re in C:\Users\

<username>\AppData\Roaming\Adobe\Dreamweaver 9\Configuration\Workspace. If you

can’t see the Application Data or AppData folder, see the next section, “Accessing hidden

files and folders in Windows.”

On a Mac, they are in Macintosh HD:Users:<username>:Library:Application Support:

Adobe:Dreamweaver 9:Configuration:Workspace.

The files have the same name that you used to save the layout. Simply copy them to the

other computer, and restart Dreamweaver. Since they’re XML, you can share them among

Windows and Mac users.

Accessing hidden files and folders in Windows

Most Dreamweaver configuration files are hidden by default in Windows XP and Vista. To

edit or copy them, you need to enable the option to view hidden files and folders. Once

you turn on this option, hidden folders are displayed as dimmed icons to remind you to

treat them with care.

In Windows XP, go to Start ä My Computer ä Tools ä Folder Options ä View. In Advanced

settings, select Show hidden files and folders. In Vista, go to Start ä Computer ä Organize ä

Folder and Search Options ä View. In Advanced settings, select Show hidden files and folders.

Displaying optional toolbars

Bizarre though it may seem, the Standard toolbar (see following screenshot) is not displayed

by default. To display it, go to View ä Toolbars, and select Standard. Alternatively,

right-click any toolbar, and select Standard from the context menu.

Dreamweaver automatically locates the Standard toolbar—which contains common file

functions such as New Document, Open, Save, Print, Cut, Copy, and Paste—immediately

below the Document toolbar. This isn’t really very helpful, because it means the toolbar

disappears as soon as all documents are closed.

However, on Windows you can move the Standard toolbar by positioning your mouse

pointer over the double row of dots at the left edge, holding down the main mouse button,

and dragging and dropping the toolbar to a new location. If you have a wide monitor,

you can place it alongside the Insert bar. Alternatively, dock it directly underneath the

Insert bar. It then remains available at all times.

On a Mac, there appears to be no way of undocking the Standard toolbar from the

Document toolbar.

Temporarily hiding all panels

When you want to clear the onscreen clutter to see your design in all its glory, just press

F4, and all the panels disappear, leaving the Document window on its own. In Windows,

DREAMWEAVER CS3—

15

1

the Document window expands to fill the entire workspace. On a Mac, panels disappear,

but the Document window doesn’t change size. Press F4 again: the panels return, and on

Windows, the Document window shrinks back to fit inside the workspace.

Organizing visual assets with Bridge

If you have used Adobe Creative Suite 2, you’ll already be familiar with Bridge, although it

has undergone a considerable transformation in CS3. It’s a powerful file organizer with

features designed to appeal to photographers and designers, and it now comes bundled

with Dreamweaver CS3. To do Bridge justice would require several chapters, so I’ll concentrate

on the main points that apply to Dreamweaver.

You can launch Bridge CS3 either from the Start menu in Windows and Applications in Mac

OS X or from inside Dreamweaver or any other CS3 program. To launch Bridge from inside

Dreamweaver, go to File ä Browse in Bridge, or click the Browse in Bridge button on the

Standard toolbar (shown alongside).

Bridge is a large program, so it doesn’t appear instantly. The advantage of launching Bridge

from inside Dreamweaver is that it automatically displays the root folder of the current

site in the Content tab. 1-11 shows the default layout of Bridge after navigating to a

site’s main images folder.

1-11. Bridge CS3 makes it easy to view and organize a website’s visual assets.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

16

The Content tab in the center displays the contents of the selected folder. At first glance, it

may not look very different from Windows Explorer or Mac Finder, but the first thing you’ll

notice is that if you have Flash movies (SWF files) or video (FLV), Fireworks PNG files,

Photoshop PSD files, or Adobe Illustrator AI files, you can see a thumbnail of the contents,

rather than an icon. You can also play Flash video and see the contents of most PDF files.

You can even flip through the pages of a PDF in the Preview panel at the top right.

Two features that will appeal to digital photographers are the abilities to use Bridge to

import photos directly from your camera (File ä Get Photos from Camera) and to preview

photos stored in Camera Raw, the generic name given to the native format of most

middle-range and professional-level digital cameras.

Controlling thumbnails

The thumbnails in the Content tab are scalable. Just drag the pointer at the right end of the

Bridge status bar to make them bigger or smaller. If the thumbnails look blurred, switch

the default to High Quality Thumbnails by opening the Preferences panel (Edit ä Preferences

or Bridge ä Preferences on a Mac) and selecting the Thumbnails category. The Thumbnails

category in Preferences also lets you choose other information to be displayed under the

thumbnail in addition to the file name.

Once a thumbnail has been generated, Bridge caches it in a central folder to speed up the

program’s performance. By default, the cache is created in your own user folders, but you

can change the location through the Advanced category of Preferences. It’s a good idea to

purge the cache from time to time to avoid clogging up your hard disk with thumbnails of

files no longer in use. You can clear the entire cache by clicking the Purge Cache button in

the Advanced category of Preferences. To clear the cached thumbnails for a single folder,

go to Tools ä Cache, and select Purge Cache for Folder “foldername”.

As well as the default view shown in 1-11, there are two other layouts: Filmstrip

Focus and Metadata Focus (which displays metadata alongside the thumbnail). You can

quickly switch between views by clicking one of the numbers at the bottom right of the

Bridge status bar.

If you want to display your images in their full glory, Bridge can create a full-screen

slideshow (View ä Slideshow or Slideshow Options). Press Esc to exit the slideshow.

Adding metadata

To get the most out of Bridge’s powerful search capabilities, you need to input information

about your images, such as keywords, designer/photographer, ranking (you can give

images star ratings), and so on. You fill in these details in the Metadata and Keywords tabs

at the bottom right of the default view.

Renaming files

One of the most useful features of Bridge is its ability to rename large numbers of files.

Let’s say you have just received a batch of photos from a client and they have meaningless

names such as DSC_0417.jpg. You can rename them all in seconds by selecting them in

DREAMWEAVER CS3—

17

1

Bridge and going to Tools ä Batch Rename. The options allow you to build up complex yet

meaningful names. You can rename the existing files in the same folder or make renamed

copies in a different folder.

Although you can use the batch rename feature to change the file name extension of all

files in a website from .html to .php, the integration with Dreamweaver is not smart

enough to update all internal links inside the files. This would be a useful addition to a

future version.

Dragging and dropping files

You can drag and drop any file directly from Bridge into Dreamweaver or another CS3 program.

If you drag an image into Dreamweaver, it’s inserted into the page wherever you

drop it, automatically creating the necessary XHTML. If you drag an image into Fireworks,

it’s immediately ready for editing.

Creating standards-compliant web pages

No sooner was Dreamweaver 8 out of the door than a team of Dreamweaver engineers

were recruited for a secret mission known as Project Hoover, a reference to the wellknown

brand of vacuum cleaners. The team’s task has been to sweep up relics of the past,

removing obsolete tags and markup from the code that Dreamweaver creates when laying

out a web page. Web standardistas will be quick to point out that the job isn’t complete,

but the team has trodden a delicate path between striving for full standards compliance

and maintaining backward compatibility with existing sites. I understand that the team

members haven’t packed away their coveralls; they plan to keep on vacuuming as they

work on the next release. They’re not ready to flip the Hoover off switch just yet.

Enhanced CSS support

In spite of some failings, particularly in regard to ensuring full compliance with a Strict

DTD—which is still left up to the user—I think it’s fair to say that Dreamweaver CS3 is the

most standards-compliant version of the program yet. It comes with an impressive array of

standard layouts, styled with fully-commented CSS. The handling of CSS style rules and the

visual rendering of pages in Design view are much improved. Dreamweaver CS3 is

browser-neutral: unlike the previous version, it doesn’t attempt to emulate Internet

Explorer 6. Instead, it attempts to render styles according to the CSS 2.1 recommendation

laid down by W3C—and mostly succeeds.

As part of Project Hoover, Dreamweaver dropped proprietary terminology in favor of

more descriptive terms.

Layers are dead . . . Welcome, AP elements

One important change for existing Dreamweaver users and those migrating from GoLive is

that layers are dead. Since a layer is really a <div> that has been absolutely positioned with

CSS, Dreamweaver CS3 now calls them AP elements. While this may seem a cosmetic

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

18

change, the idea is to clarify how they fit into a web page. What’s more, any tag can be

used to create an AP element, not just a <div>.

1-12 shows how Dreamweaver CS3 renders the following code (it’s in examples/

ch01/classof58.html in the download files):

<div id=”wrapper”>

<img id=”header” src=”images/header.jpg” alt=”Class of 58″ å

width=”720″ height=”175″ />

<ul id=”nav”>

<li><a href=”#”>Home</a></li>

<li><a href=”#”>2006 Reunion</a></li>

<li><a href=”#”>Memory Lane </a> </li>

<li><a href=”#”>Roll call</a></li>

<li><a href=”#”</a></li>

<li><a href=”#”>Contact</a></li>

</ul>

</div>

The wrapper <div> is relatively positioned to center the whole page in the browser and

contains two absolutely positioned elements: an image with the ID header and an

unordered list with the ID nav. As you can see, the AP Elements panel on the right of the

lists both absolutely positioned elements but not the relatively positioned one. In

previous versions of Dreamweaver, the Layers panel ignored absolutely positioned elements

unless they used a <div> tag.

1-12. Dreamweaver CS3 lists all absolutely positioned elements in the new AP Elements panel.

For backward compatibility, “layers” still live on in the name of the JavaScript function

associated with one of the Dreamweaver behaviors. Although the Show/Hide Layers

behavior has been renamed Show/Hide Elements, the function is still called

MM_showHideLayers. However, the behavior now handles any element that has an ID.

Behaviors are covered in the next chapter.

DREAMWEAVER CS3—

19

1

This new arrangement gives you much greater flexibility and control over your CSS.

There’s no longer any need to litter your code with unnecessary <div> tags, and the AP

Elements panel provides an instant snapshot of the absolutely positioned elements on your

page. You can drag and drop elements within the panel to change their z-index property,

and the eye icon on the left of the panel changes the visibility property. AP elements

nested inside another AP element are indented, and if you realize that the nesting is causing

an element to be displayed in the wrong place, you can separate them by dragging the

nested element to the left of the panel. Changes made in the AP Elements panel automatically

update the element’s style rules, even if they are in a separate style sheet.

Seeing the impact of CSS changes in real time

Grouped together with the AP Elements panel is the CSS Styles panel, which lets you

inspect and edit style rules without leaving Design view. As a result, you can see immediately

how your changes affect the layout of the page and tweak them until you get the

desired result. Like many features in Dreamweaver, the CSS Styles panel is context sensitive.

1-13 shows the styles for the nav unordered list in classof58.html. The properties

for the #nav ID selector are shown in the lower section of the panel, while the upper

sections show all the rules that affect the element currently selected in Design view. This

helps you understand how the rules are cascaded down to a particular element, making it

easier to troubleshoot styles that don’t work the way you expect. Chapters 5 and 6 show

you how to make effective use of the CSS Styles panel.

1-13.

The CSS Styles panel shows which styles in the

cascade affect the selected element.

The style rules for classof58.html are in classof58.css in the styles folder.

This example has been deliberately designed to demonstrate a new feature of

Dreamweaver CS3. I don’t necessarily recommend the use of AP elements to

achieve this particular layout.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

20

Improved style sheet management

A major innovation in Dreamweaver CS3 is the ability to drag and drop selectors in the

CSS Styles panel to reorder style blocks in your style sheet. You can also move styles out

of the <head> of a document into a new or an existing style sheet with just a couple of

mouse clicks. The target style sheet doesn’t even need to exist or to be open; everything

happens automatically.

If you have old-style layers with inline styles, you can clean up your pages easily by using

the Convert Inline CSS to Rule feature to move the rules into the <head> of the document

or an external style sheet. It doesn’t matter whether you’re in Code view or Design view; as

long as your cursor is anywhere inside a layer, just right-click and select CSS Styles ä

Convert Inline CSS to Rule from the context menu. Dreamweaver presents you with the dialog

box shown in 1-14.

1-14. As long as your cursor is inside a layer, Dreamweaver can move the

inline styles to an external style sheet or the head of the document.

Dreamweaver automatically chooses the ID as the name of the selector for the new rule.

Although you can change the name in the dialog box, this affects only the new style rule.

It doesn’t change the ID of the <div>. You can convert only one layer at a time, but it’s a

much quicker and more accurate way of tidying up legacy pages than attempting to cut

and paste everything manually. The Convert to drop-down menu at the top left of the

Convert Inline CSS dialog box has two other options: to create a class based on the inline

styles or to apply the styles to all <div> elements. They are there for completeness and

should be used rarely, if ever. You can test this feature using layers.html in

examples/ch01.

Another small improvement to CSS management in Dreamweaver CS3 is the option to

clean up the formatting of style rules by selecting Apply Source Formatting from the

Commands menu. The available formatting options are shown in 1-15. They’re very

basic in comparison with a dedicated CSS editor like TopStyle Pro but are nevertheless a

welcome addition. To access the CSS Source Format Options dialog box, go to Preferences

on the Edit menu (Dreamweaver menu on a Mac), select the Code Format category, and

click the Advanced Formatting CSS button.

DREAMWEAVER CS3—

21

1

1-15. You can tell Dreamweaver CS3 how you prefer

style sheets to be laid out.

Using visual aids to understand your CSS structure

Dreamweaver’s visual aids are a powerful feature that helps you visualize the underlying

structure of your page by highlighting individual block elements and their associated

padding and margins in Design view. Most visual aids are turned on by default but remain

in the background until you select a particular block element, such as a <div> or <table>.

1-16 shows one of the preset layouts with the container <div> selected in the Tag

selector (the file is also in examples/ch01/visualaids.html).

1-16. Dreamweaver lets you see the underlying structure of your page with powerful

visual aids.

January 22, 2009 Posted by | Uncategorized | Leave a comment

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP 01

In one respect, the change symbolizes the fact that Dreamweaver is under new ownership.

Macromedia, the company that turned Dreamweaver, Flash, and Fireworks into must-have

tools for web developers, was acquired by Adobe at the end of 2005. And Dreamweaver

(together with former Macromedia stable mates Fireworks and Flash) is now part of

Creative Suite, Adobe’s family of web and print design tools that includes Photoshop,

Illustrator, and InDesign. Creative Suite is now in its third iteration; hence CS3. Although

each program has a long history, the idea of Creative Suite is to promote greater integration

to make it easier to switch to the best tool for a particular job, such as Photoshop for

photo manipulation and retouching, and import the results into another program. And

that’s what’s happened to Dreamweaver: although it’s available as a stand-alone program,

it’s now closely integrated with its new Adobe brothers and sisters as part of Adobe Web

Suite CS3 and Design Suite CS3.

Even the stand-alone version of Dreamweaver CS3 comes bundled with three other

programs:

Extension Manager CS3: An updated version of the program that lets you install

third-party add-ons to extend the functionality of Dreamweaver, Flash, and Fireworks.

Adobe Bridge CS3: As the name suggests, this is intended to facilitate communication

between the various parts of Creative Suite, but it also works with the standalone

version of Dreamweaver. At one level, it’s like a super-charged version of

Windows Explorer or Finder on the Mac, but it shines in the handling of visual

assets. It allows you to see inside a wide variety of formats, making it easy to find

an image by what it looks like.

Adobe Device Central CS3: This is a brand new program that allows you to visualize

what your websites will look like in a wide range of mobile devices, not only

from the layout point of view but also simulating mobile backlight and sunlight

reflections.

The integration goes further through the ability to copy and paste directly from a

Photoshop PSD file into Dreamweaver. When you do so, Dreamweaver optimizes the file

for the Web. So, yes, the “CS3” does make sense. It’s not just change for the sake of

change.

Once installed, Extension Manager, Bridge, and Device Central are separate programs and

can be launched independently, but you have no choice whether to install them. The

installer simply lists them as “Shared Components.” As a result, this version of Dreamweaver

occupies roughly four times more disk space than previous versions. Personally, I like

Bridge and think that Device Central is likely to become increasingly useful as mobile Web

access grows in popularity. Others may disagree.

If you’re a long-term Dreamweaver user, though, the program that you know and love

hasn’t changed beyond recognition. As Figures 1-4 and 1-5 show, the workspace layout is

identical to Dreamweaver 8. The development team moved with Dreamweaver to Adobe,

and the improvements to the program are a logical progression. Adobe accompanied

its decision to include Dreamweaver in Creative Suite 3 with the bold step of dropping its

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

2

own web design program, GoLive. Although GoLive has been updated, it’s not in any of the

CS3 packages. What’s more, Adobe has created an online tutorial to help GoLive users

migrate to Dreamweaver (www.adobe.com/go/learn_dw_golive). This sends a clear message

that Adobe now regards Dreamweaver as its prime tool for developing standardscompliant

websites.

In this chapter, we’ll take a look at the most important features and changes in

Dreamweaver CS3, with particular emphasis on cascading style sheets (CSS) and creating

standards-compliant Extensible HyperText Markup Language (XHTML), both of which are

essential for building any modern website. Then, in the following chapter, we’ll take a look

at the tools Dreamweaver offers for building dynamic websites: Spry—Adobe’s implementation

of Asynchronous JavaScript + XML (Ajax)—and PHP.

What this chapter covers

Finding out what’s new in Dreamweaver CS3

Exploring and organizing the Dreamweaver workspace

Using Bridge to manage visual assets

Taking a first look at Dreamweaver’s support for cascading style sheets

Getting the best out of Code view

Getting your bearings in Dreamweaver

As the title of this book says, this is an essential guide to Dreamweaver CS3. So I don’t

intend to bore you to death with descriptions of every menu and submenu. However, all

readers may not be familiar with Dreamweaver, so I’ll start with a few signposts to guide

you around the Dreamweaver interface and help set basic program preferences. Most of

this will be familiar to experienced users of Dreamweaver, but there are some important

changes. To identify these changes, look for the New and Changed graphics in the margin.

Starting up

When you launch Dreamweaver CS3, the first thing you see after the program has finished

loading is the welcome screen shown in 1-1. The three columns in the top section

provide quick access to a recently opened document (this list is empty the first time you

launch Dreamweaver), create a new document or Dreamweaver site (site definition is

covered in Chapter 4), or select from a predefined layout. The Dreamweaver Exchange

option at the foot of the right column takes you directly to the Adobe Dreamweaver

Exchange (www.adobe.com/cfusion/exchange/index.cfm?view=sn120), where you can

obtain extensions to add extra functionality to Dreamweaver (many are free; others are

sold on a commercial basis). The bottom section of the welcome screen takes you to various

parts of the Adobe website and displays what Adobe considers useful information, for

example, available updates to the program.

DREAMWEAVER CS3—

3

1

1-1. The welcome screen offers access to recent documents and a quick way to

create new ones.

The Dreamweaver welcome screen reappears whenever you close all documents in the

workspace and connects to Adobe to see if there are any new announcements. In previous

versions, leaving the welcome screen enabled was a resource hog, so many developers

chose the Don’t show again option at the bottom left. This means what it says: once you

select it, the welcome screen disappears forever. If you want it back, go to Edit ä

Preferences (Dreamweaver ä Preferences on a Mac), choose the General category, and

select Show welcome screen.

Although the welcome screen no longer appears to be a resource hog, you may prefer to

switch it off because you get a much better range of options from the New Document dialog

box. You can also con Dreamweaver to reopen on startup any documents that

are still open when you close the program. Just select Reopen documents on startup in the

General category of the Preferences panel.

Creating a new document

To create a new document, select File ä New or press Ctrl+N/Cmd+N. This opens the New

Document dialog box, which has been considerably revamped, as shown in 1-2.

The biggest change is that, in addition to a completely blank page, you can now select one

of 32 CSS layouts. There are also options to change the page’s document type definition

(DTD)—by default, Dreamweaver CS3 uses XHTML 1.0 Transitional—and to attach an

external style sheet to the page at the time of creation. We’ll look at the CSS layouts and

style sheet options in Chapters 5 and 6.

DREAMWEAVER CS3 WITH CSS, AJAX, AND PHP

4

1-2. The New Document dialog box offers a massive range of options and preset layouts.

By selecting the appropriate option from the menu on the left side of the New Document

dialog box, you can also create new templates from scratch or a page from an existing

template (templates are covered in Chapter 12). The Page from Sample option offers a

wide range of preset layouts, but I don’t recommend using them, as many of them use oldstyle

presentational elements and deprecated attributes. The final option, labeled Other,

contains a variety of pages for programming languages such as ActionScript, C#, and

VBScript, none of which are used in this book.

Setting new document preferences

Click the Preferences button at the bottom left of the New Document dialog box.

Alternatively, choose Preferences from the Edit menu (Dreamweaver menu on a Mac), and

select the New Document category. Either presents you with the New Document Preferences

dialog box shown in 1-3.

The dialog box lets you set the following global preferences:

Default document lets you choose the type of document that will be created when

you use the keyboard shortcut for a new document (Ctrl+N/Cmd+N). For this to

work, you must deselect the option at the bottom labeled Show New Document dialog

box on Control+N/Cmd+N. Otherwise, the dialog box shown in 1-2 will

appear.

Default extension affects only (X)HTML files. Change the value only if you want to

use .htm to maintain unity with the naming convention of older sites.

Default Document Type (DTD) sets the default DOCTYPE declaration for all new web

pages. You cannot set one value for, say, .html and another for .php pages.

DREAMWEAVER CS3—

5

1

1-3. The New Document category of the Preferences panel

Default encoding lets you choose the character set to be used in all web pages. The

Dreamweaver CS3 default is Unicode (UTF-8). (In the Mac version, this is listed as

Unicode 4.0 UTF-8.) This is different from previous versions. The checkbox below

this option tells Dreamweaver to use the same character set to display existing

pages that don’t specify a particular encoding. It doesn’t insert any extra coding in

such pages.

Unicode Normalization Form is required only when using UTF-8 for encoding. It

should normally be set to C (Canonical Decomposition, followed by Canonical

Composition), and the Include Unicode Signature (BOM) checkbox should be deselected.

If you use any other encoding, set Unicode Normalization Form to None.

Choosing the default document type Many people misunderstand the purpose of the DTD

(the DOCTYPE declaration before the opening <html> tag). It simply tells the browser how

you have coded your page and is intended to speed up the correct rendering of your

design. It’s not a badge of honor or magic spell that somehow renders your web pages

future-proof. The default setting in Dreamweaver CS3 is XHTML 1.0 Transitional, and this is

the appropriate choice for most people when creating a new web page as long as you

understand the stricter rules imposed by XHTML.

Visit http://www.w3.org/TR/xhtml1/#diffs to learn about the differences

between HTML and XHTML. Also read the frequently asked questions

at www.w3.org/MarkUp/2004/xhtml-faq.

 

January 22, 2009 Posted by | Uncategorized | Leave a comment

Valentine Gifts & Valentine Jewelry

Heartfelt Gifts for your Valentine
Love, commitment and more… Specially handcrafted jewelry
symbolizing your journey of growing love.
Celebrate life, celebrate love. Special gifts to cherish the special moments of
togetherness.
More for less…. Enchanting sets of exquisitely matched jewelry with great
value for your money.
Choose a gemstone of her preference and complement it with a setting from our exquisite collection.
Valentine Jewelry
18k White Gold Diamond Heart Pendant (3/4 ct. tw.) $589.99 18k White Gold Ruby Heart Pendants
$339.99
18k White Gold Diamond and Ruby Designer Heart Pendant $399.99
A lovely accessory to compliment your outfit. This 18k white
gold pendant features round brilliant diamonds (0.70 ct. tw.) in
a pretty heart shape.
Dramatic and Colorful, this 18k white gold pendant features a heart
studded with ruby gemstones (1.63 ct. tw.). This gorgeous heart pendant
will add color to any attire
A graceful, stylized heart motif featuring round brilliant
diamonds (0.11 ct. tw.) and red ruby gemstones (0.34 ct. tw.) in
a contemporary design. This 18k white gold pendant is a treat
for the jewelry lover.
Valentine Gifts
Valentines Day Gifts
Valentine Jewelry Gift

February 8, 2008 Posted by | Valentine Gift | 2 Comments

Java Help, Java Tutorials, Java Programming, Java Tricks


Java Help, Java Tutorials, Java Programming, Java Tricks
lesson four

Working with Objects

CONTENTS

Creating New Objects

Using new

What new Does

A Note on Memory Management

Accessing and Setting Class and Instance Variables

Getting Values

Changing Values

Class Variables

Calling Methods

Class Methods

References to Objects

Casting and Converting Objects and Primitive Types

Casting Primitive Types

Casting Objects

Converting Primitive Types to Objects and Vice Versa

Odds and Ends

Comparing Objects

Determining the Class of an Object

Class and Object Reflection (Java 1.1)

The Java Class Library

Summary

Q&A

Let’s start today’s lesson with an obvious statement: Because Java is an
object-oriented language, you’re

going to be dealing with a lot of objects. You’ll create them, modify them, move
them around, change

their variables, call their methods, combine them with other objects-and, of
course, develop classes and

use your own objects in the mix.

Today, therefore, you’ll learn all about the Java object in its natural habitat.
Today’s topics include

Creating instances of classes

Testing and modifying class and instance variables in your new instance

Calling methods in that object

Casting (converting) objects and other data types from one class to another

Other odds and ends about working with objects

An overview of the Java class libraries

Creating New Objects

When you write a Java program, you define a set of classes. As you learned on
Day 2, “Object-Oriented

Programming and Java,” classes are templates for objects; for the most part, you
merely use the class to

create instances and then work with those instances. In this section, therefore,
you’ll learn how to create a

new object from any given class.

Remember strings from yesterday? You learned that using a string literal-a
series of characters enclosed in

double-quotes-creates a new instance of the class String with the value of that
string.

The String class is unusual in that respect-although it’s a class, there’s an
easy way to create instances of

that class using a literal. The other classes don’t have that shortcut; to
create instances of those classes you

have to do so explicitly by using the new operator.

Note

What about the literals for numbers and characters? Don’t they create

objects, too? Actually, they don’t. The primitive data types for

numbers and characters create numbers and characters, but for

efficiency, they aren’t actually objects. You can put object wrappers

around them if you need to treat them like objects (you’ll learn how to

do this in “Casting and `Converting Objects and Primitive Types”).

Using new

To create a new object, you use the new operator with the name of the class you
want to create an instance

of, then parentheses after that. The following examples create new instances of
the classes String,

Random, and Motorcycle, and store those new instances in variables of the
appropriate types:

String str = new String();

Random r = new Random();

Motorcycle m2 = new Motorcycle();

The parentheses are important; don’t leave them off. The parentheses can be
empty (as in these examples),

in which case the most simple, basic object is created; or the parentheses can
contain arguments that

determine the initial values of instance variables or other initial qualities of
that object:

Date dt = new Date(90, 4, 1, 4, 30);

Point pt = new Point(0,0);

The number and type of arguments you can use inside the parentheses with new are
defined by the class

itself using a special method called a constructor (you’ll learn more about
constructors later today). If you

try and create a new instance of a class with the wrong number or type of
arguments (or if you give it no

arguments and it needs some), then you’ll get an error when you try to compile
your Java program.

Here’s an example of creating several different types of objects using different
numbers and types of

arguments. The Date class, part of the java.util package, creates objects that
represent the current

date. Listing 4.1 is a Java program that shows three different ways of creating
a Date object using new.

Listing 4.1. Laura’s Date program.

1: import java.util.Date;

2:

3: class CreateDates {

4:

5: public static void main(String args[]) {

6: Date d1, d2, d3;

7:

8: d1 = new Date();

9: System.out.println(“Date 1: ” + d1);

10:

11: d2 = new Date(71, 7, 1, 7, 30);

12: System.out.println(“Date 2: ” + d2);

13:

14: d3 = new Date(“April 3 1993 3:24 PM”);

15: System.out.println(“Date 3: ” + d3);

16: }

17: }

Date 1: Tue Feb 13 09:36:56 PST 1996

Date 2: Sun Aug 01 07:30:00 PDT 1971

Date 3: Sat Apr 03 15:24:00 PST 1993

Analysis

In this example, three different date objects are created using different

arguments to the class listed after new. The first instance (line 8) uses

new Date() with no arguments, which creates a Date object for

today’s date (the first line of the output shows a sample; your output

will, of course, read the current date and time for you).

The second Date object you create in this example has five integer arguments.
The arguments represent a

date: year, month, day, hours, and minutes. And, as the output shows, this
creates a Date object for that

particular date: Sunday, August 1, 1971, at 7:30 a.m.

Note

Java numbers months starting from 0. So although you might expect

the seventh month to be July, month 7 in Java is indeed August.

The third version of Date takes one argument, a string, representing the date as
a text string. When the

Date object is created, that string is parsed, and a Date object with that date
and time is created (see the

third line of output). The date string can take many different formats; see the
API documentation for the

Date class (part of the java.util package) for information about what strings
you can use.

What new Does

When you use the new operator, the new instance of the given class is created,
and memory is allocated

for it. In addition (and most importantly), a special method defined in the
given class is called to initialize

the object and set up any initial values it needs. This special method is called
a constructor. Constructors

are special methods, defined in classes, that create and initialize new
instances of classes.

New Term

Constructors are special methods that initialize a new object, set its

variables, create any other objects that object needs, and generally

perform any other operations the object needs to initialize itself.

Multiple constructor definitions in a class can each have a different number or
type of arguments-then,

when you use new, you can specify different arguments in the argument list, and
the right constructor for

those arguments will be called. That’s how each of those different versions of
new that you used in the

CreateDates class can create different Date objects.

When you create your own classes, you can define as many constructors as you
need to implement that

class’s behavior. You’ll learn how to create constructors on Day 7, “More About
Methods.”

A Note on Memory Management

Memory management in Java is dynamic and automatic. When you create a new object
in Java, Java

automatically allocates the right amount of memory for that object in the heap.
You don’t have to allocate

any memory for any objects explicitly; Java does it for you.

What happens when you’re finished with that object? How do you de-allocate the
memory that object

uses? The answer, again, is that memory management is automatic. Once you’re
done with an object, you

reassign all the variables that might hold that object and remove it from any
arrays, thereby making the

object unusable. Java has a “garbage collector” that looks for unused objects
and reclaims the memory that

those objects are using. You don’t have to do any explicit freeing of memory;
you just have to make sure

you’re not still holding onto an object you want to get rid of. You’ll learn
more specific details about the

Java garbage collector and how it works on Day 21, “Under the Hood.”

New Term

A garbage collector is a special thing built into the Java environment

that looks for unused objects. If it finds any, it automatically removes

those objects and frees the memory those objects were using.

Accessing and Setting Class and Instance Variables

Now you have your very own object, and that object may have class or instance
variables defined in it.

How do you work with those variables? Easy! Class and instance variables behave
in exactly the same

ways as the local variables you learned about yesterday; you just refer to them
slightly differently than you

do regular variables in your code.

Getting Values

To get to the value of an instance variable, you use an expression in what’s
called dot notation. With dot

notation, the reference to an instance or class variable has two parts: the
object on the left side of the dot

and the variable on the right side of the dot.

New Term

Dot notation is an expression used to get at instance variables and

methods inside a given object.

For example, if you have an object assigned to the variable myObject, and that
object has a variable

called var, you refer to that variable’s value like this:

myObject.var;

This form for accessing variables is an expression (it returns a value), and
both sides of the dot can also be

expressions. This means that you can nest instance variable access. If that var
instance variable itself

holds an object and that object has its own instance variable called state, you
could refer to it like this:

myObject.var.state;

Dot expressions are evaluated left to right, so you start with myObject’s
variable var, which points to

another object with the variable state. You end up with the value of that state
variable after the entire

expression is done evaluating.

Changing Values

Assigning a value to that variable is equally easy-just tack an assignment
operator on the right side of the

expression:

myObject.var.state = true;

Listing 4.2 is an example of a program that tests and modifies the instance
variables in a Point object.

Point is part of the java.awt package and refers to a coordinate point with an x
and a y value.

Listing 4.2. The TestPoint Class.

1: import java.awt.Point;

2:

3: class TestPoint {

4: public static void main(String args[]) {

5: Point thePoint = new Point(10,10);

6:

7: System.out.println(“X is ” + thePoint.x);

8: System.out.println(“Y is ” + thePoint.y);

9:

10: System.out.println(“Setting X to 5.”);

11: thePoint.x = 5;

12: System.out.println(“Setting Y to 15.”);

13: thePoint.y = 15;

14:

15: System.out.println(“X is ” + thePoint.x);

16: System.out.println(“Y is ” + thePoint.y);

17:

18: }

19:}

X is 10

Y is 10

Setting X to 5.

Setting Y to 15.

X is 5

Y is 15

Analysis

In this example, you first create an instance of Point where X and Y

are both 10 (line 6). Lines 8 and 9 print out those individual values,

and you can see dot notation at work there. Lines 11 through 14

change the values of those variables to 5 and 15, respectively.

Finally, lines 16 and 17 print out the values of X and Y again to show

how they’ve changed.

Class Variables

Class variables, as you’ve already learned, are variables that are defined and
stored in the class itself. Their

values, therefore, apply to the class and to all its instances.

With instance variables, each new instance of the class gets a new copy of the
instance variables that class

defines. Each instance can then change the values of those instance variables
without affecting any other

instances. With class variables, there is only one copy of that variable. Every
instance of the class has

access to that variable, but there is only one value. Changing the value of that
variable changes it for all

the instances of that class.

You define class variables by including the static keyword before the variable
itself. You’ll learn more

about this on Day 6, “Creating Classes and Applications in Java.” For example,
take the following partial

class definition:

class FamilyMember {

static String surname = “Johnson”;

String name;

int age;

}

Instances of the class FamilyMember each have their own values for name and age.
But the class

variable surname has only one value for all family members. Change surname, and
all the instances of

FamilyMember are affected.

To access class variables, you use the same dot notation as you do with instance
variables. To get or

change the value of the class variable, you can use either the instance or the
name of the class on the left

side of the dot. Both of the lines of output in this example print the same
value:

FamilyMember dad = new FamilyMember();

System.out.println(“Family’s surname is: ” + dad.surname);

System.out.println(“Family’s surname is: ” + FamilyMember.surname);

Because you can use an instance to change the value of a class variable, it’s
easy to become confused

about class variables and where their values are coming from (remember that the
value of a class variable

affects all the instances). For this reason, it’s a good idea to use the name of
the class when you refer to a

class variable-it makes your code easier to read and strange results easier to
debug.

Calling Methods

Calling a method is similar to referring to an object’s instance variables:
Method calls to objects also use

dot notation. The object itself whose method you’re calling is on the left side
of the dot; the name of the

method and its arguments are on the right side of the dot:

myObject.methodOne(arg1, arg2, arg3);

Note that all calls to methods must have parentheses after them, even if that
method takes no arguments:

myObject.methodNoArgs();

If the method you’ve called returns an object that itself has methods, you can
nest methods as you would

variables. This next example calls the getName() method, which is defined in the
object returned by the

getClass() method, which was defined in myObject. Got it?

myObject.getClass().getName();

You can combine nested method calls and instance variable references as well (in
this case you’re calling

the methodTwo() method, which is defined in the object stored by the var
instance variable, which in

turn is part of the myObject object):

myObject.var.methodTwo(arg1, arg2);

System.out.println(), the method you’ve been using through the book this far to
print out bits of

text, is a great example of nesting variables and methods. The System class
(part of the java.lang

package) describes system-specific behavior. System.out is a class variable that
contains an instance of

the class PrintStream that points to the standard output of the system.
PrintStream instances have

a println() method that prints a string to that output stream.

Listing 4.3 shows an example of calling some methods defined in the String
class. Strings include

methods for string tests and modification, similar to what you would expect in a
string library in other

languages.

Listing 4.3. Several uses of String methods.

1: class TestString {

2:

3: public static void main(String args[]) {

4: String str = “Now is the winter of our discontent”;

5:

6: System.out.println(“The string is: ” + str);

7: System.out.println(“Length of this string: “

8: + str.length());

9: System.out.println(“The character at position 5: “

10: + str.charAt(5));

11: System.out.println(“The substring from 11 to 17: “

12: + str.substring(11, 17));

13: System.out.println(“The index of the character d: “

14: + str.indexOf(‘d’));

15: System.out.print(“The index of the beginning of the “);

16: System.out.println(“substring \”winter\”: “

17: + str.indexOf(“winter”));

18: System.out.println(“The string in upper case: “

19: + str.toUpperCase());

20: }

21: }

The string is: Now is the winter of our discontent

Length of this string: 35

The character at position 5: s

The substring from positions 11 to 17: winter

The index of the character d: 25

The index of the beginning of the substring “winter”: 11

The string in upper case: NOW IS THE WINTER OF OUR DISCONTENT

Analysis

In line 4, you create a new instance of String by using a string

literal (it’s easier that way than using new and then putting the

characters in individually). The remainder of the program simply calls

different string methods to do different operations on that string:

Line 6 prints the value of the string we created in line 4: “Now is the winter
of our

discontent”.

l

Line 7 calls the length() l method in the new String object. This string has 35
characters.

Line 9 calls the charAt() method, which returns the character at the given
position in the string.

Note that string positions start at 0, so the character at position 5 is s.

l

Line 11 calls the substring() method, which takes two integers indicating a
range and returns

the substring at those starting and ending points. The substring() method can
also be called

with only one argument, which returns the substring from that position to the
end of the string.

l

Line 13 calls the indexOf() method, which returns the position of the first
instance of the given

character (here, ‘d’).

l

Line 15 shows a different use of the indexOf() method, which takes a string
argument and

returns the index of the beginning of that string.

l

l Finally, line 19 uses the toUpperCase() method to return a copy of the string
in all uppercase.

Class Methods

Class methods, like class variables, apply to the class as a whole and not to
its instances. Class methods

are commonly used for general utility methods that may not operate directly on
an instance of that class,

but fit with that class conceptually. For example, the String class contains a
class method called

valueOf(), which can take one of many different types of arguments (integers,
booleans, other objects,

and so on). The valueOf() method then returns a new instance of String
containing the string value

of the argument it was given. This method doesn’t operate directly on an
existing instance of String, but

getting a string from another object or data type is definitely a String-like
operation, and it makes sense

to define it in the String class.

Class methods can also be useful for gathering general methods together in one
place (the class). For

example, the Math class, defined in the java.lang package, contains a large set
of mathematical

operations as class methods-there are no instances of the class Math, but you
can still use its methods

with numeric or boolean arguments. For example, the class method Math.max()
takes two arguments

and returns the larger of the two. You don’t need to create a new instance of
Math; just call the method

anywhere you need it, like this:

in biggerOne = Math.max(x, y);

To call a class method, you use dot notation as you do with instance methods. As
with class variables, you

can use either an instance of the class or the class itself on the left site of
the dot. However, for the same

reasons noted in the discussion on class variables, using the name of the class
for class methods makes

your code easier to read. The last two lines in this example produce the same
result (the string “5”):

String s, s2;

s = “foo”;

s2 = s.valueOf(5);

s2 = String.valueOf(5);

References to Objects

As you work with objects, one important thing going on behind the scenes is the
use of references to those

objects. When you assign objects to variables, or pass objects as arguments to
methods, you are passing

references to those objects, not the objects themselves or copies of those
objects.

An example should make this clearer. Examine Listing 4.4, which shows a simple
example of how

references work.

Listing 4.4. A references example.

1: import java.awt.Point;

2:

3: class ReferencesTest {

4: public static void main (String args[]) {

5: Point pt1, pt2;

6: pt1 = new Point(100, 100);

7: pt2 = pt1;

8:

9: pt1.x = 200;

10: pt1.y = 200;

11: System.out.println(“Point1: ” + pt1.x + “, ” + pt1.y);

12: System.out.println(“Point2: ” + pt2.x + “, ” + pt2.y);

13: }

14: }

Point1: 200, 200

Point2: 200, 200

Analysis

In the first part of this program, you declare two variables of type

Point (line 5), create a new Point object to pt1 (line 6), and

finally, assign the value of pt1 to pt2 (line 7).

Now, here’s the challenge. After changing pt1’s x and y instance variables in
lines 9 and 10, what will

pt2 look like?

As you can see, pt2’s x and y instance variables were also changed, even though
you never explicitly

changed them. When you assign the value of pt1 to pt2, you actually create a
reference from pt2 to the

same object to which pt1 refers (see Figure 4.1). Change the object that pt2
refers to, and you also

change the object that pt1 points to, because both are references to the same
object.

Figure 4.1 : References to objects.

Note

If you actually do want pt1 and pt2 to point to separate objects, you

should use new Point() for both lines to create separate objects.

The fact that Java uses references becomes particularly important when you pass
arguments to methods.

You’ll learn more about this later today, but keep these references in mind.

Technical Note

There are no explicit pointers or pointer arithmetic in Java as there are

in C-like languages-just references. However, with these references,

and with Java arrays, you have most of the capabilities that you have

with pointers without the confusion and lurking bugs that explicit

pointers can create.

Casting and Converting Objects and Primitive

Types

Sometimes in your Java programs you may have a value stored somewhere that is
the wrong type for what

you want to do with it. Maybe it’s an instance of the wrong class, or perhaps
it’s a float and you want it

to be an int. To convert the value of one type to another, you use casting.
Casting is a programming term

that means, effectively, converting a value or an object from one type to
another. The result of a cast is a

new value or object; casting does not change the original object or value.

New Time

Casting converts the value of an object or primitive type into another

type.

Although the concept of casting is a simple one, the rules for what types in
Java can be converted to what

other types are complicated by the fact that Java has both primitive types (int,
float, boolean), and

object types (String, Point, Window, and so on). There are three forms of casts
and conversions to

talk about in this section:

Casting between primitive types: int to float or float l to double

l Casting between object types: an instance of a class to an instance of another
class

l Converting primitive types to objects and then extracting primitive values
back out of those objects

Casting Primitive Types

Casting between primitive types allows you to “convert” the value of one type to
another primitive

type-for example, to assign a number of one type to a variable of another type.
Casting between primitive

types most commonly occurs with the numeric types; boolean values cannot be cast
to any other primitive

type.

Often, if the type you are casting to is “larger” than the type of the value
you’re converting, you may not

have to use an explicit cast. You can often automatically treat a byte or a
character as an int, for

example, or an int as a long, an int as a float, or anything as a double
automatically. In most

cases, because the larger type provides more precision than the smaller, no loss
of information occurs

when the value is cast. The exception is casting integers to floating-point
values; casting an int or a

long to a float or a long to a double may cause some loss of precision.

To convert a large value to smaller type, you must use an explicit cast, because
converting that value may

result in a loss of precision. Explicit casts look like this:

(typename)value

In this form, typename is the name of the type you’re converting to (for
example: short, int, float,

boolean), and value is an expression that results in the value you want to
convert. So, for example, in

this expression the value of x is divided by the value of y and the result is
cast to an int:

(int) (x / y);

Note that because the precedence of casting is higher than that of arithmetic,
you have to use parentheses

here; otherwise, the value of x would be cast first and then divided by y (which
might very well be a very

different result).

Casting Objects

Instances of classes can also be cast to instances of other classes, with one
restriction: The class of the

object you’re casting and the class you’re casting it to must be related by
inheritance; that is, you can cast

an object only to an instance of its class’s sub- or superclass-not to any
random class.

Analogous to converting a primitive value to a larger type, some objects may not
need to be cast

explicitly. In particular, because subclasses contain all the same information
as their superclass, you can

use an instance of a subclass anywhere a superclass is expected. (Did you just
have to read that sentence

four times before you understood it? I had to rewrite it a whole lot of times
before it became even that

simple. Bear with me, its not that bad. Let’s try an example.) Suppose you have
a method that takes two

arguments: one of type Object, and one of type Number. You don’t have to pass
instances of those

particular classes to that method. For the Object argument, you can pass any
subclass of Object (any

object, in other words), and for the Number argument you can pass in any
instance of any subclass of

Number (Integer, Boolean, Float, and so on); you don’t have to explicitly
convert them first.

Casting downward in the class hierarchy is automatic, but casting upward is not.
Converting an instance of

a subclass to an instance of a superclass loses the information the original
subclass provided and requires

an explicit cast. To cast an object to another class, you use the same casting
operation that you used for

base types:

(classname)object

In this case, classname is the name of the class you want to cast the object to,
and object is a

reference to the object you’re casting. Note that casting creates a reference to
the old object of the type

classname; the old object still continues to exist as it did before.

Here’s a (fictitious) example of a cast of an instance of the class GreenApple
to an instance of the class

Apple (where GreenApple is theoretically a subclass of Apple with more
information to define the

apple as green):

GreenApple a;

Apple a2;

a = new GreenApple();

a2 = (Apple) a;

In addition to casting objects to classes, you can also cast objects to
interfaces-but only if that object’s

class or one of its superclasses actually implements that interface. Casting an
object to an interface means

that you can call one of that interface’s methods even if that object’s class
does not actually implement that

interface. You’ll learn more about interfaces in Week 3.

Converting Primitive Types to Objects and Vice Versa

Now you know how to cast a primitive type to another primitive type and how to
cast between classes.

How can you cast one to the other?

You can’t! Primitive types and objects are very different things in Java and you
can’t automatically cast or

convert between the two. However, the java.lang package includes several special
classes that

correspond to each primitive data type: Integer for ints, Float for floats,
Boolean for

booleans, and so on. Note that the class names have an initial capital letter,
and the primitive types are

lowercase. Java treats these names very differently, so don’t confuse them, or
your methods and variables

won’t behave the way you expect.

Using class methods defined in these classes, you can create an
object-equivalent for all the primitive

types using new. The following line of code creates an instance of the Integer
class with the value 35:

Integer intObject = new Integer(35);

Once you have actual objects, you can treat those values as objects. Then, when
you want the primitive

values back again, there are methods for that as well-for example, the intValue()
method extracts an

int primitive value from an Integer object:

int theInt = intObject.intValue(); // returns 35

See the Java API documentation for these special classes for specifics on the
methods for converting

primitives to and from objects.

Note

In Java 1.0 there are special type classes for Boolean,

Character, Double, Float, Integer, and Long. Java 1.1

adds classes for Byte and Short, as well as a special wrapper class

for Void. The latter classes are used primarily for object reflection.

Odds and Ends

This section is a catchall for other information about working with objects,
particularly the following:

l Comparing objects

l Finding out the class of any given object

l Testing to see whether an object is an instance of a given class

Comparing Objects

Yesterday you learned about operators for comparing values: equals, not equals,
less than, and so on. Most

of these operators work only on primitive types, not on objects. If you try to
use other values as operands,

the Java compiler produces errors.

The exception to this rule is with the operators for equality: == (equal) and !=
(not equal). These

operators, when used with objects, test whether the two operands refer to
exactly the same object in

memory.

What should you do if you want to be able to compare instances of your class and
have meaningful

results? You have to implement special methods in your class, and you have to
call those methods using

those method names.

Technical Note

Java does not have the concept of operator overloading-that is, the

ability to redefine the behavior of the built-in operators using methods

in your own classes. The built-in operators remain defined only for

numbers.

A good example of this is the String class. It is possible to have two strings,
two independent objects in

memory with the same values-that is, the same characters in the same order.
According to the == operator,

however, those two String objects will not be equal, because, although their
contents are the same, they

are not the same object.

The String class, therefore, defines a method called equals() that tests each
character in the string

and returns true if the two strings have the same values. Listing 4.5
illustrates this.

Listing 4.5. A test of string equality.

1: class EqualsTest {

2: public static void main(String args[]) {

3: String str1, str2;

4: str1 = “she sells sea shells by the sea shore.”;

5: str2 = str1;

6:

7: System.out.println(“String1: ” + str1);

8: System.out.println(“String2: ” + str2);

9: System.out.println(“Same object? ” + (str1 == str2));

10:

11: str2 = new String(str1);

12:

13: System.out.println(“String1: ” + str1);

14: System.out.println(“String2: ” + str2);

15: System.out.println(“Same object? ” + (str1 == str2));

16: System.out.println(“Same value? ” + str1.equals(str2));

17: }

18: }

String1: she sells sea shells by the sea shore.

String2: she sells sea shells by the sea shore.

Same object? true

String1: she sells sea shells by the sea shore.

String2: she sells sea shells by the sea shore.

Same object? false

Same value? true

Analysis

The first part of this program (lines 4 through 6) declares two

variables (str1 and str2) assigns the literal she sells sea

shells by the sea shore. to str1, and then assigns that

value to str2. As you learned earlier when we talked about object

references, now str1 and str2 point to the same object, and the

equality test at line 10 proves that.

In the second part, you create a new string object with the same value as str1
and assign str2 to that

new string object. Now you have two different string objects in str1 and str2,
both with the same

value. Testing them to see whether they’re the same object by using the ==
operator (line 16) returns the

expected answer (false-they are not the same object in memory), as does testing
them using the

equals() method (line 17) (true-they have the same values).

Technical Note

Why can’t you just use another literal when you change str2, rather

than using new? String literals are optimized in Java-if you create a

string using a literal, and then use another literal with the same

characters, Java knows enough to give you the first String object

back. Both strings are the same objects-to create two separate objects

you have to go out of your way.

Determining the Class of an Object

Want to find out the class of an object? Here’s the way to do it for an object
assigned to the variable obj:

String name = obj.getClass().getName();

What does this do? The getClass() method is defined in the Object class, and as
such is available

for all objects. The result of that method is a Class object (where Class is
itself a class), which has a

method called getName(). getName() returns a string representing the name of the
class.

Another test that might be useful to you is the instanceof operator. instanceof
has two operands:

an object on the left and the name of a class on the right. The expression
returns true or false based on

whether the object is an instance of the named class or any of that class’s
subclasses:

“foo” instanceof String // true

Point pt = new Point(10, 10);

pt instanceof String // false

The instanceof operator can also be used for interfaces; if an object implements
an interface, the

instanceof operator with that interface name on the right side returns true.
You’ll learn all about

interfaces in Week 3.

Class and Object Reflection (Java 1.1)

Reflection, also known as introspection, is a somewhat lofty term to describe
the ability to “look inside” a

class or an object and get information about that object’s variables and methods
as well as actually set and

get the values of those variables and to call methods. Object reflection is
useful for tools such as class

browsers or debuggers, where getting at the information of an object on-the-fly
allows you to explore what

that object can do, or for component-based programs such as Java Beans, where
the ability for one object

to query another object about what it can do (and then ask it to do something)
is useful to building larger

applications.

The classes that support reflection of Java classes and objects will be part of
the core Java 1.1 API (they

are not available in the 1.0.2 version of the JDK). A new package,
java.lang.reflect, will contain

new classes to support reflection, which include the following:

l Field, for managing and finding out information about class and instance
variables

l Method, for managing class and instance methods

Constructor, for managing the special methods for creating new instances of
classes (you’ll

learn more about constructors on Day 7)

l

l Array, for managing arrays

Modifier, for decoding modifier information about classes, variables and methods
(more about

modifiers on Day 15, “Modifiers, Access Control, and Class Design”)

l

In addition, there will be a number of new methods available in the Class class
to help tie together the

various reflection classes.

You can find out more about the new reflection classes and methods from

http://java.sun.com/products/JDK/1.1/designspecs/reflection/.

The Java Class Library

To finish up today, let’s look at the Java class library. Actually, you’ve had
some experience with some of

the Java classes already, so they shouldn’t seem that strange.

The Java class library provides the set of classes that are guaranteed to be
available in any commercial

Java environment (for example, in any Java development environment or in
browsers such as Netscape).

Those classes are in the java package and include all the classes you’ve seen so
far in this book, plus a

whole lot more classes you’ll learn about later on in this book (and more you
may not learn about at all).

The Java Developer’s Kit comes with documentation for all of the Java class
library, which includes

descriptions of each class’s instance variables, methods, constructors,
interfaces, and so on. You can get to

this documentation (called the Java Application Programmer’s Interface, or API)
via the Web at

http://java.sun.com:80/products/JDK/CurrentRelease/api/packages.html. A

shorter summary of the Java API is in appendix C as well. Exploring the Java
class library and its methods

and instance variables is a great way to figure out what Java can and cannot do,
as well as how it can

become a starting point for your own development.

Here are the class packages that are part of the Java class library:

java.lang-Classes that apply to the language itself, including the Object class,
the String

class, and the System class. It also contains the special classes for the
primitive types (Integer,

Character, Float, and so on). You’ll get at least a glance at most of the
classes in this package

in this first week.

l

java.util-Utility classes, such as Date, as well as simple collection classes,
such as Vector

and Hashtable. You’ll learn more about these classes in the Bonus Week.

l

java.io-Input and output classes for writing to and reading from streams (such
as standard input

and output) and for handling files. Day 19, “Streams and I/O,” describes the
classes in this package.

l

java.net-Classes for networking support, including Socket and URL (a class to
represent

references to documents on the World Wide Web). You’ll learn a little about
networking on Day 14,

“Windows, Networking, and Other Tidbits,” and then on Day 26, “Client/Server
Networking in

Java.”

l

java.awt-This is the Abstract Windowing Toolkit. It contains classes to
implement graphical

user interface features, including classes for Window, Menu, Button, Font,
CheckBox, and so

on. It also includes mechanisms for managing system events and for processing
images (in the

java.awt.Image package). You’ll learn all about the awt in Week 2.

l

l java.applet-Classes to implement Java applets.

In addition to the Java classes, your development environment may also include
additional classes that

provide other utilities or functionality. Although these classes may be useful,
because they are not part of

the standard Java library, they may not be available to other people trying to
run your Java program unless

you explicitly include those classes with your program. This is particularly
important for applets, because

applets are expected to be able to run on any platform, using any Java-enabled
browser. Only classes

inside the java package are guaranteed to be available on all browsers and Java
environments.

Summary

Objects, objects everywhere. Today, you’ve learned all about how to deal with
objects: how to create

them, how to find out and change the values of their variables, and how to call
their methods. You have

also learned how to copy and compare them and how to convert them into other
objects. Finally, you have

learned a bit about the Java class libraries-which give you a whole slew of
classes to play with in your

own programs.

You now have the fundamentals of how to deal with most simple things in the Java
language. All you

have left are arrays, conditionals, and loops, which you’ll learn about
tomorrow. Then you’ll learn how to

define and use classes in Java applications on Day 6, and launch directly into
applets next week. With just

about everything you do in your Java programs, you’ll always come back to
objects.

Q&A

Q: I’m confused about the differences between objects and the primitive data
types, such as

int and boolean.

A: The primitive types in the language (byte, short, int, long, float, double,
boolean,

and char) represent the smallest things in the language. They are not objects,
although in many

ways they can be handled like objects-they can be assigned to variables and
passed in and out of

methods. Most of the operations that work exclusively on objects, however, will
not work with

primitive types.

Objects are instances of classes and, as such, are usually much more complex
data types than

simple numbers and characters, often containing numbers and characters as
instance or class

variables.

Q: No pointers in Java? If you don’t have pointers, how are you supposed to do
something like

linked lists, where you have a pointer from one nose to another so you can
traverse them?

A: Java doesn’t have no pointers at all; it has no explicit pointers. Object
references are, effectively,

pointers. So to create something like a linked list, you would create a class
called Node, which

would have an instance variable also of type Node. Then to link together node
objects all you

need to do is assign a node object to the instance variable of the object just
before it in the list.

Because object references are pointers, linked lists set up this way will behave
as you would

expect them to.

Q: In the section on calling methods, you had examples of calling a method with
a different

number of arguments each time-and it gave a different kind of result. How is
that possible?

A: That’s called method overloading. Overloading means that the same method can
have different

behavior based on the arguments it’s called with-and the number and type of
arguments can vary.

When you define methods in your own classes, you define separate method
signatures with

different sets of arguments and different definitions. When a method is called,
Java figures out

which definition to execute based on the number and type of arguments with which
you called it.

You’ll learn all about this on Day 6.

Q: No operator overloading in Java? Why not? I thought Java was based on C++,
and C++ has

operator overloading.

A: Java was indeed based on C++, but it was also designed to be simple, so many
of C++’s features

have been removed. The argument against operator overloading is that because the
operator can

be defined to mean anything; it makes it very difficult to figure out what any
given operator is

doing at any one time. This can result in entirely unreadable code. When you use
a method, you

know it can mean many things to many classes, but when you use an operator you
would like to

know that it always means the same thing. Given the potential for abuse, the
designers of Java felt

it was one of the C++ features that was best left out.

January 8, 2008 Posted by | Java Help | 1 Comment

Java Help, Java Tutorials, Java Programming, Java Tricks


Java Help, Java Tutorials, Java Programming, Java Tricks

Lesson Three

Java Basics

CONTENTS

Statements and Expressions

Variables and Data Types

Declaring Variables

Notes on Variable Names

Variable Types

Assigning Values to Variables

Comments

Literals

Number Literals

Boolean Literals

Character Literals

String Literals

Expressions and Operators

Arithmetic

More About Assignment

Incrementing and Decrementing

Comparisons

Logical Operators

Bitwise Operators

Operator Precedence

String Arithmetic

Summary

Q&A

Already this week you’ve learned about Java programming in very broad terms-what
a Java program andan executable look like, and how to create simple classes. For
the remainder of this week, you’re going to get down to details and deal with
the specifics of what the Java language looks like.

Today you won’t define any classes or objects or worry about how any of them
communicate inside a

Java program. Rather, you’ll draw closer and examine simple Java statements-the
basic things you can do

in Java within a method definition such as main().

Today you’ll learn about the following:

Java statements and expressions

Variables and data types

Comments

Literals

Arithmetic

Comparisons

Logical operators

Technical Note

Java looks a lot like C++, and-by extension-like C. Much of the

syntax will be very familiar to you if you are used to working in these

languages. If you are an experienced C or C++ programmer, you may

want to pay special attention to the technical notes (such as this one),

because they provide information about the specific differences

between these and other traditional languages and Java.

Statements and Expressions

A statement indicates the simplest tasks you can accomplish in Java; a statement
forms a single Java

operation. All the following are simple Java statements:

int i = 1;

import java.awt.Font;

System.out.println(“This motorcycle is a “

+ color + ” ” + make);

m.engineState = true;

Statements sometimes return values-for example, when you add two numbers
together or test to see

whether one value is equal to another. These kind of statements are called
expressions. You’ll learn about

these later today.

White space in Java statements, as with C, is unimportant. A statement can be
contained on a single line

or on multiple lines, and the Java compiler will be able to read it just fine.
The most important thing to

remember about Java statements is that each one ends with a semicolon (;).
Forget the semicolon, and

your Java program won’t compile.

Java also has compound statements, or blocks, which can be placed wherever a
single statement can.

Block statements are surrounded by braces ({}). You’ll learn more about blocks
on Day 5, “Arrays,

Conditionals, and Loops.”

Variables and Data Types

Variables are locations in memory in which values can be stored. Each one has a
name, a type, and a

value. Before you can use a variable, you have to declare it. After it is
declared, you can then assign

values to it (you can also declare and assign a value to a variable at the same
time, as you’ll learn in this

section).

Java actually has three kinds of variables: instance variables, class variables,
and local variables.

Instance variables, as you learned yesterday, are used to define the attributes
of a particular object. Class

variables are similar to instance variables, except their values apply to all
that class’s instances (and to the

class itself) rather than having different values for each object.

Local variables are declared and used inside method definitions, for example,
for index counters in loops,

as temporary variables, or to hold values that you need only inside the method
definition itself. They can

also be used inside blocks, which you’ll learn about on Day 5. Once the method
(or block) finishes

executing, the variable definition and its value cease to exist. Use local
variables to store information

needed by a single method and instance variables to store information needed by
multiple methods in the

object.

Although all three kinds of variables are declared in much the same ways, class
and instance variables

are accessed and assigned in slightly different ways from local variables. Today
you’ll focus on variables

as used within method definitions; tomorrow you’ll learn how to deal with
instance and class variables.

Note Unlike other languages, Java does not have global variables-that is,

variables that are global to all parts of a program. Instance and class

variables can be used to communicate global information between

and among objects. Remember that Java is an object-oriented

language, so you should think in terms of objects and how they

interact, rather than in terms of programs.

Declaring Variables

To use any variable in a Java program, you must first declare it. Variable
declarations consist of a type

and a variable name:

int myAge;

String myName;

boolean isTired;

Variable definitions can go anywhere in a method definition (that is, anywhere a
regular Java statement

can go), although they are most commonly declared at the beginning of the
definition before they are

used:

public static void main (String args[]) {

int count;

String title;

boolean isAsleep;

}

You can string together variable names with the same type on one line:

int x, y, z;

String firstName, LastName;

You can also give each variable an initial value when you declare it:

int myAge, mySize, numShoes = 28;

String myName = “Laura”;

boolean isTired = true;

int a = 4, b = 5, c = 6;

If there are multiple variables on the same line with only one initializer (as
in the first of the previous

examples), the initial value applies to only the last variable in a declaration.
You can also group

individual variables and initializers on the same line using commas, as with the
last example.

Local variables must be given values before they can be used (your Java program
will not compile if you

try to use an unassigned local variable). For this reason, it’s a good idea
always to give local variables

initial values. Instance and class variable definitions do not have this
restriction. (Their initial value

depends on the type of the variable: null for instances of classes, 0 for
numeric variables, ” for

characters, and false for booleans.)

Notes on Variable Names

Variable names in Java can start with a letter, an underscore (_), or a dollar
sign ($). They cannot start

with a number. After the first character, your variable names can include any
letter or number. Symbols,

such as %, *, @, and so on, are often reserved for operators in Java, so be
careful when using symbols in

variable names.

In addition, the Java language uses the Unicode character set. Unicode is a
character set definition that

not only offers characters in the standard ASCII character set, but also
includes several thousand other

characters for representing most international alphabets. This means that you
can use accented characters

and other glyphs as legal characters in variable names, as long as they have a
Unicode character number

above 00C0.

Warning

The Unicode specification is a two-volume set of lists of thousands of

characters. If you don’t understand Unicode, or don’t think you have a

use for it, it’s safest just to use plain numbers and letters in your

variable names. You’ll learn a little more about Unicode later.

Finally, note that the Java language is case sensitive, which means that
uppercase letters are different

from lowercase letters. This means that the variable X is different from the
variable x, and a rose is not

a Rose is not a ROSE. Keep this in mind as you write your own Java programs and
as you read Java

code other people have written.

By convention, Java variables have meaningful names, often made up of several
words combined. The

first word is lowercase, but all following words have an initial uppercase
letter:

Button theButton;

long reallyBigNumber;

boolean currentWeatherStateOfPlanetXShortVersion;

Variable Types

In addition to the variable name, each variable declaration must have a type,
which defines what values

that variable can hold. The variable type can be one of three things:

One of the eight primitive data types

The name of a class or interface

An array

You’ll learn about how to declare and use array variables on Day 5; this lesson
focuses on the primitive

and class types.

Primitive Types

The eight primitive data types handle common types for integers, floating-point
numbers, characters, and

boolean values (true or false). They’re called primitive because they’re built
into the system and are

not actual objects, which makes them more efficient to use. Note that these data
types are

machine-independent, which means that you can rely on their sizes and
characteristics to be consistent

across your Java programs.

There are four Java integer types, each with a different range of values (as
listed in Table 3.1). All are

signed, which means they can hold either positive or negative numbers. Which
type you choose for your

variables depends on the range of values you expect that variable to hold; if a
value becomes too big for

the variable type, it is silently truncated.

Table 3.1. Integer types.

Type Size Range

byte 8 bits -128 to 127

short 16 bits -32,768 to 32,767

int 32 bits -2,147,483,648 to 2,147,483,647

long 64 bits -9,223,372,036,854,775,808 to

9,223,372,036,854,775,807

Floating-point numbers are used for numbers with a decimal part. Java
floating-point numbers are

compliant with IEEE 754 (an international standard for defining floating-point
numbers and arithmetic).

There are two floating-point types: float (32 bits, single precision) and double
(64 bits, double

precision).

The char type is used for individual characters. Because Java uses the Unicode
character set, the char

type has 16 bits of precision, unsigned.

Finally, the boolean type can have one of two values, true or false. Note that
unlike in other C-like

languages, boolean is not a number, nor can it be treated as one. All tests of
boolean variables should

test for true or false.

Note that all the primitive types are in lowercase. Be careful when you use them
in your programs that

you do use the lowercase, because there are also classes with the same names
(and an initial capital

letter) that have different behavior-so, for example, the primitive type boolean
is different from the

Boolean class. You’ll learn more about these special classes and what they’re
used for on Day 4,

“Working with Objects.”

Class Types

In addition to the eight primitive data types, variables in Java can also be
declared to hold an instance of

a particular class:

String LastName;

Font basicFont;

OvalShape myOval;

Each of these variables can hold instances of the named class or of any of its
subclasses. The latter is

useful when you want a variable to be able to hold different instances of
related classes. For example,

let’s say you had a set of fruit classes-Apple, Pear, Strawberry, and so on- all
of which inherited

from the general class Fruit. By declaring a variable of type Fruit, that
variable can then hold

instances of any of the Fruit classes. Declaring a variable of type Object means
that variable can

hold any object.

Technical Note

Java does not have a typedef statement (as in C and C++). To

declare new types in Java, you declare a new class; then variables can

be declared to be of that class’s type.

Assigning Values to Variables

Once a variable has been declared, you can assign a value to that variable by
using the assignment

operator =, like this:

size = 14;

tooMuchCaffiene = true;

Comments

Java has three kinds of comments: two for regular comments in source code and
one for the special

documentation system javadoc.

The symbols /* and */ surround multiline comments, as in C or C++. All text
between the two

delimiters is ignored:

/* I don’t know how I wrote this next part; I was working

really late one night and it just sort of appeared. I

suspect the code elves did it for me. It might be wise

not to try and change it.

*/

These comments cannot be nested; that is, you cannot have a comment inside a
comment.

Double-slashes (//) can be used for a single line of comment. All the text up to
the end of the line is

ignored:

int vices = 7; // are there really only 7 vices?

The final type of comment begins with /** and ends with */. The contents of
these special comments

are used by the javadoc system, but are otherwise used identically to the first
type of comment.

javadoc is used to generate API documentation from the code. You’ll learn more
about javadoc on

Day 22, “Java Programming Tools.”

Literals

Literal is a programming language term that essentially means that what you type
is what you get. For

example, if you type 4 in a Java program, you automatically get an integer with
the value 4. If you type

‘a’, you get a character with the value a. Literals are used to indicate simple
values in your Java

programs.

New Term

A literal is a simple value where “what you type is what you get.”

Numbers, characters, and strings are all examples of literals.

Literals may seem intuitive most of the time, but there are some special cases
of literals in Java for

different kinds of numbers, characters, strings, and boolean values.

Number Literals

There are several integer literals. 4, for example, is a decimal integer literal
of type int (although you

can assign it to a variable of type byte or short because it’s small enough to
fit into those types). A

decimal integer literal larger than an int is automatically of type long. You
also can force a smaller

number to a long by appending an L or l to that number (for example, 4L is a
long integer of value

4). Negative integers are preceded by a minus sign-for example, -45.

Integers can also be expressed as octal or hexadecimal: A leading 0 indicates
that a number is octal-for

example, 0777 or 0004. A leading 0x (or 0X) means that it is in hex (0xFF,
0XAf45). Hexadecimal

numbers can contain regular digits (0-9) or upper- or lowercase hex digits (a-f
or A-F).

Floating-point literals usually have two parts, the integer part and the decimal
part-for example,

5.77777. A floating-point literal results in a floating-point number of type
double, regardless of the

precision of the number. You can force the number to the type float by appending
the letter f (or F) to

that number-for example, 2.56F.

You can use exponents in floating-point literals using the letter e or E
followed by the exponent (which

can be a negative number): 10e45 or .36E-2.

Boolean Literals

Boolean literals consist of the keywords true and false. These keywords can be
used anywhere you

need a test or as the only possible values for boolean variables.

Character Literals

Character literals are expressed by a single character surrounded by single
quotes: ‘a’, ‘#’, ‘3’, and

so on. Characters are stored as 16-bit Unicode characters. Table 3.2 lists the
special codes that can

represent nonprintable characters, as well as characters from the Unicode
character set. The letter d in the

octal, hex, and Unicode escapes represents a number or a hexadecimal digit (a-f
or A-F).

Table 3.2. Character escape codes.

Escape Meaning

\n Newline

\t Tab

\b Backspace

\r Carriage return

\f Formfeed

\\ Backslash

\’ Single quote

\” Double quote

\ddd Octal

\xdd Hexadecimal

\udddd Unicode character

Technical Note

C and C++ programmers should note that Java does not include

character codes for \a (bell) or \v (vertical tab).

String Literals

A combination of characters is a string. Strings in Java are instances of the
class String. Strings are not

simply arrays of characters as they are in C or C++, although they do have many
array-like

characteristics (for example, you can test their length, and access and change
individual characters).

Because string objects are real objects in Java, they have methods that enable
you to combine, test, and

modify strings very easily.

String literals consist of a series of characters inside double quotes:

“Hi, I’m a string literal.”

“” //an empty string

Strings can contain character constants such as newline, tab, and Unicode
characters:

“A string with a \t tab in it”

“Nested strings are \”strings inside of\” other strings”

“This string brought to you by Java\u2122″

In the last example, the Unicode code sequence for \u2122 produces a trademark
symbol ( ).

Note

Just because you can represent a character using a Unicode escape

does not mean your computer can display that character-the computer

or operating system you are running may not support Unicode, or the

font you’re using may not have a glyph (picture) for that character.

All that Unicode escapes in Java provide is a way to encode Unicode

characters for systems that support Unicode.

Java 1.1 will provide better capabilities for the display of Unicode

characters and for handling international character sets.

When you use a string literal in your Java program, Java automatically creates
an instance of the class

String for you with the value you give it. Strings are unusual in this respect;
the other literals do not

behave in this way (none of the primitive data types are actual objects), and
usually creating a new object

involves explicitly creating a new instance of a class. You’ll learn more about
strings, the String class,

and the things you can do with strings later today and tomorrow.

Expressions and Operators

Expressions are the simplest form of statement in Java that actually
accomplishes something: All

expressions, when evaluated, return a value (other statements don’t necessarily
do so). Arithmetic and

tests for equality and magnitude are common examples of expressions. Because
they return a value, you

can assign that result to a variable or test that value in other Java
statements.

Most of the expressions in Java use operators. Operators are special symbols for
things like arithmetic,

various forms of assignment, increment and decrement, and logical operations.

New Term

Expressions are statements that return a value.

New Term

Operators are special symbols that are commonly used in expressions.

Arithmetic

Java has five operators for basic arithmetic (see Table 3.3).

Table 3.3. Arithmetic operators.

Operator Meaning Example

+ Addition 3 + 4

- Subtraction 5 – 7

* Multiplication 5 * 5

/ Division 14 / 7

% Modulus 20 % 7

Each operator takes two operands, one on either side of the operator. The
subtraction operator (-) can

also be used to negate a single operand.

Integer division results in an integer. Because integers don’t have decimal
fractions, any remainder is

ignored. The expression 31 / 9, for example, results in 3 (9 goes into 31 only 3
times).

Modulus (%) gives the remainder once the operands have been evenly divided. For
example, 31 % 9

results in 4 because 9 goes into 31 three times, with 4 left over.

Note that the result type of most arithmetic operations involving integers is an
int regardless of the

original type of the operands (shorts and bytes are both automatically converted
to int). If either or

both operands is of type long, the result is of type long. If one operand is an
integer and another is a

floating-point number, the result is a floating point. (If you’re interested in
the details of how Java

promotes and converts numeric types from one type to another, you may want to
check out the Java

Language Specification on Sun’s official Java Web site at http://java.sun.com/;
that’s more

detail than I want to cover here.)

Listing 3.1 is an example of simple arithmetic in Java.

Listing 3.1. Simple arithmetic.

1: class ArithmeticTest {

2: public static void main (String args[]) {

3: short x = 6;

4: int y = 4;

5: float a = 12.5f;

6: float b = 7f;

7:

8: System.out.println(“x is ” + x + “, y is ” + y);

9: System.out.println(“x + y = ” + (x + y));

10: System.out.println(“x – y = ” + (x – y));

11: System.out.println(“x / y = ” + (x / y));

12: System.out.println(“x % y = ” + (x % y));

13:

14: System.out.println(“a is ” + a + “, b is ” + b);

15: System.out.println(“a / b = ” + (a / b));

16: }

17: }

x is 6, y is 4

x + y = 10

x – y = 2

x / y = 1

x % y = 2

a is 12.5, b is 7

a / b = 1.78571

Analysis

In this simple Java application (note the main() method), you

initially define four variables in lines 3 through 6: x and y, which are

integers (type int), and a and b, which are floating-point numbers

(type float). Keep in mind that the default type for floating-point

literals (such as 12.5) is double, so to make sure these are

numbers of type float, you have to use an f after each one (lines 5

and 6).

The remainder of the program merely does some math with integers and
floating-point numbers and

prints out the results.

There is one other thing to mention about this program: the method
System.out.println().

You’ve seen this method on previous days, but you haven’t really learned exactly
what it does. The

System.out.println() method merely prints a message to the standard output of
your system-to

the screen, to a special window, or maybe just to a special log file, depending
on your system and the

development environment you’re running. The System.out.println() method takes a
single

argument-a string-but you can use + to concatenate multiple values into a single
string, as you’ll learn

later today.

More About Assignment

Variable assignment is a form of expression; in fact, because one assignment
expression results in a

value, you can string them together like this:

x = y = z = 0;

In this example, all three variables now have the value 0.

The right side of an assignment expression is always evaluated before the
assignment takes place. This

means that expressions such as x = x + 2 do the right thing; 2 is added to the
value of x, and then

that new value is reassigned to x. In fact, this sort of operation is so common
that Java has several

operators to do a shorthand version of this, borrowed from C and C++. Table 3.4
shows these shorthand

assignment operators.

Table 3.4. Assignment operators.

Expression Meaning

x += y x = x + y

x -= y x = x – y

x *= y x = x * y

x /= y x = x / y

Technical Note

Technically, the shorthand assignment and longhand expressions are

not exactly equivalent, particularly in cases where x or y may

themselves be complicated expressions and your code relies on side

effects of those expressions. In most instances, however, they are

functionally equivalent. For more information about very complicated

expressions, evaluation order, and side effects, you may want to

consult the Java Language Specification.

Incrementing and Decrementing

As in C and C++, the ++ and — operators are used to increment or decrement a
variable’s value by 1.

For example, x++ increments the value of x by 1 just as if you had used the
expression x = x + 1.

Similarly x– decrements the value of x by 1. (Unlike C and C++, Java allows x
to be floating point.)

These increment and decrement operators can be prefixed or postfixed; that is,
the ++ or — can appear

before or after the value it increments or decrements. For simple increment or
decrement expressions,

which one you use isn’t overly important. In complex assignments, where you are
assigning the result of

an increment or decrement expression, which one you use makes a difference.

Take, for example, the following two expressions:

y = x++;

y = ++x;

These two expressions yield very different results because of the difference
between prefix and postfix.

When you use postfix operators (x++ or x–), y gets the value of x before x is
changed; using prefix,

the value of x is assigned to y after the change has occurred. Listing 3.2 is a
Java example of how all this

works.

Listing 3.2. Test of prefix and postfix increment operators.

1: class PrePostFixTest {

2:

3: public static void main (String args[]) {

4: int x = 0;

5: int y = 0;

6:

7: System.out.println(“x and y are ” + x + ” and ” + y );

8: x++;

9: System.out.println(“x++ results in ” + x);

10: ++x;

11: System.out.println(“++x results in ” + x);

12: System.out.println(“Resetting x back to 0.”);

13: x = 0;

14: System.out.println(“————“);

15: y = x++;

16: System.out.println(“y = x++ (postfix) results in:”);

17: System.out.println(“x is ” + x);

18: System.out.println(“y is ” + y);

19: System.out.println(“————“);

20:

21: y = ++x;

22: System.out.println(“y = ++x (prefix) results in:”);

23: System.out.println(“x is ” + x);

24: System.out.println(“y is ” + y);

25: System.out.println(“————“);

26:

27: }

28: }

x and y are 0 and 0

x++ results in 1

++x results in 2

Resetting x back to 0.

————

y = x++ (postfix) results in:

x is 1

y is 0

————

y = ++x (prefix) results in:

x is 2

y is 2

————

In the first part of this example, you increment x alone using both prefix and
postfix increment

operators. In each, x is incremented by 1 each time. In this simple form, using
either prefix or

postfix works the same way.

In the second part of this example, you use the expression y = x++, in which the
postfix increment

operator is used. In this result, the value of x is incremented after that value
is assigned to y. Hence the

result: y is assigned the original value of x (0), and then x is incremented by
1.

In the third part, you use the prefix expression y = ++x. Here, the reverse
occurs: x is incremented

before its value is assigned to y. Because x is 1 from the previous step, its
value is incremented (to 2),

and then that value is assigned to y. Both x and y end up being 2.

Technical Note

Technically, this description is not entirely correct. In reality, Java

always completely evaluates all expressions on the right of an

expression before assigning that value to a variable, so the concept of

“assigning x to y before x is incremented” isn’t precisely right.

Instead, Java takes the value of x and “remembers” it, evaluates

(increments) x, and then assigns the original value of x to y. Although

in most simple cases this distinction may not be important, for more

complex expressions with side effects, it may change the behavior of

the expression overall. See the Language Specification for many more

details about expression evaluation in Java.

Comparisons

Java has several expressions for testing equality and magnitude. All of these
expressions return a boolean

value (that is, true or false). Table 3.5 shows the comparison operators.

Table 3.5. Comparison operators.

Operator Meaning Example

== Equal x == 3

!= Not equal x != 3

< Less than x < 3

> Greater than x > 3

<= Less than or equal to x <= 3

>= Greater than or equal to x >= 3

Logical Operators

Expressions that result in boolean values (for example, the comparison
operators) can be combined by

using logical operators that represent the logical combinations AND, OR, XOR,
and logical NOT.

For AND combinations, use either the & or && operators. The entire expression
will be true only if both

expressions on either side of the operator are also true; if either expression
is false, the entire expression

is false. The difference between the two operators is in expression evaluation.
Using &, both sides of the

expression are evaluated regardless of the outcome. Using &&, if the left side
of the expression is false,

the entire expression is assumed to be false (the value of the right side
doesn’t matter), so the expression

returns false, and the right side of the expression is never evaluated. (This is
often called a

“short-circuited” expression.)

For OR expressions, use either | or ||. OR expressions result in true if either
or both of the expressions

on either side is also true; if both expression operands are false, the
expression is false. As with & and

&&, the single | evaluates both sides of the expression regardless of the
outcome; and || is

short-circuited: If the left expression is true, the expression returns true and
the right side is never

evaluated.

In addition, there is the XOR operator ^, which returns true only if its
operands are different (one true

and one false, or vice versa) and false otherwise (even if both are true).

In general, only the && and || are commonly used as actual logical combinations.
&, |, and ^ are more

commonly used for bitwise logical operations.

For NOT, use the ! operator with a single expression argument. The value of the
NOT expression is the

negation of the expression; if x is true, !x is false.

Bitwise Operators

Finally, here’s a short summary of the bitwise operators in Java. Most of these
expressions are inherited

from C and C++ and are used to perform operations on individual bits in
integers. This book does not go

into bitwise operations; it’s an advanced topic covered better in books on C or
C++. Table 3.6

summarizes the bitwise operators.

Table 3.6. Bitwise operators.

Operator Meaning

& Bitwise AND

| Bitwise OR

^ Bitwise XOR

<< Left shift

>> Right shift

>>> Zero fill right shift

~ Bitwise complement

<<= Left shift assignment (x = x << y)

>>= Right shift assignment (x = x >> y)

>>>= Zero fill right shift assignment (x = x

>>> y)

x&=y AND assignment (x = x & y)

x|=y OR assignment (x = x | y)

x^=y XOR assignment (x = x ^ y)

Operator Precedence

Operator precedence determines the order in which expressions are evaluated.
This, in some cases, can

determine the overall value of the expression. For example, take the following
expression:

y = 6 + 4 / 2

Depending on whether the 6 + 4 expression or the 4 / 2 expression is evaluated
first, the value of y

can end up being 5 or 8. Operator precedence determines the order in which
expressions are evaluated,

so you can predict the outcome of an expression. In general, increment and
decrement are evaluated

before arithmetic, arithmetic expressions are evaluated before comparisons, and
comparisons are

evaluated before logical expressions. Assignment expressions are evaluated last.

Table 3.7 shows the specific precedence of the various operators in Java.
Operators further up in the table

are evaluated first; operators on the same line have the same precedence and are
evaluated left to right

based on how they appear in the expression itself. For example, given that same
expression y = 6 + 4

/ 2, you now know, according to this table, that division is evaluated before
addition, so the value of y

will be 8.

Table 3.7. Operator precedence.

Operator Notes

. [] () Parentheses (()) are used to group expressions; dot

(.) is used for access to methods and variables

within objects and classes (discussed tomorrow);

square brackets ([]) are used for arrays (this is

discussed later on in the week)

++ — ! ~

instanceof

The instanceof operator returns true or

false based on whether the object is an instance

of the named class or any of that class’s subclasses

(discussed tomorrow)

new (type)expression The new operator is used for creating new instances

of classes; () in this case is for casting a value to

another type (you’ll learn about both of these

tomorrow)

* / % Multiplication, division, modulus

+ – Addition, subtraction

<< >> >>> Bitwise left and right shift

< > <= >= Relational comparison tests

== != Equality

& AND

^ XOR

| OR

&& Logical AND

|| Logical OR

? : Shorthand for if…then…else (discussed on

Day 5)

= += -= *= /= %= ^= Various assignments

&= |= <<= >>= >>>= More assignments

You can always change the order in which expressions are evaluated by using
parentheses around the

expressions you want to evaluate first. You can nest parentheses to make sure
expressions evaluate in the

order you want them to (the innermost parenthetic expression is evaluated
first). The following

expression results in a value of 5, because the 6 + 4 expression is evaluated
first, and then the result of

that expression (10) is divided by 2:

y = (6 + 4) / 2

Parentheses also can be useful in cases where the precedence of an expression
isn’t immediately clear-in

other words, they can make your code easier to read. Adding parentheses doesn’t
hurt, so if they help you

figure out how expressions are evaluated, go ahead and use them.

String Arithmetic

One special expression in Java is the use of the addition operator (+) to create
and concatenate strings. In

most of the examples shown today and in earlier lessons, you’ve seen lots of
lines that looked something

like this:

System.out.println(name + ” is a ” + color + ” beetle”);

The output of that line (to the standard output) is a single string, with the
values of the variables (name

and color), inserted in the appropriate spots in the string. So what’s going on
here?

The + operator, when used with strings and other objects, creates a single
string that contains the

concatenation of all its operands. If any of the operands in string
concatenation is not a string, it is

automatically converted to a string, making it easy to create these sorts of
output lines.

Technical Note

An object or type can be converted to a string if you implement the

method toString(). All objects have a default string

representation, but most classes override toString() to provide a

more meaningful printable representation.

String concatenation makes lines such as the previous one especially easy to
construct. To create a string,

just add all the parts together-the descriptions plus the variables-and print it
to the standard output, to the

screen, to an applet, or anywhere.

The += operator, which you learned about earlier, also works for strings. For
example, take the following

expression:

myName += ” Jr.”;

This expression is equivalent to this:

myName = myName + ” Jr.”;

just as it would be for numbers. In this case, it changes the value of myName,
which might be something

like John Smith to have a Jr. at the end (John Smith Jr.).

Summary

As you have learned in the last two lessons, a Java program is made up primarily
of classes and objects.

Classes and objects, in turn, are made up of methods and variables, and methods
are made up of

statements and expressions. It is those last two things that you’ve learned
about today; the basic building

blocks that enable you to create classes and methods and build them up to a
full-fledged Java program.

Today, you have learned about variables, how to declare them and assign values
to them; literals for

easily creating numbers, characters, and strings; and operators for arithmetic,
tests, and other simple

operations. With this basic syntax, you can move on tomorrow to learning about
working with objects

and building simple, useful Java programs.

To finish up this summary, Table 3.8 is a list of all the operators you have
learned about today so that

you can refer back to them.

Table 3.8. Operator summary.

Operator Meaning

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus

< Less than

> Greater than

<= Less than or equal to

>= Greater than or equal to

== Equal

!= Not equal

&& Logical AND

|| Logical OR

! Logical NOT

& AND

| OR

^ XOR

<< Left shift

>> Right shift

>>> Zero fill right shift

~ Complement

= Assignment

++ Increment

—- Decrement

+= Add and assign

-= Subtract and assign

*= Multiply and assign

/= Divide and assign

%= Modulus and assign

&= AND and assign

|= OR and assign

<<= Left shift and assign

^= XOR and assign

>>= Right shift and assign

>>>= Zero fill right shift and assign

Q&A

Q: I didn’t see any way to define constants.

A: You can’t create local constants in Java; you can create only constant
instance and class

variables. You’ll learn how to do this tomorrow.

Q: What happens if you assign an integer value to a variable that is too large
for that variable

to hold?

A: Logically, you would think that the variable is just converted to the next
larger type, but this isn’t

what happens. What does happen is called overflow. This means that if a number
becomes too

big for its variable, that number wraps around to the smallest possible negative
number for that

type and starts counting upward toward zero again.

Because this can result in some very confusing (and wrong) results, make sure
that you declare

the right integer type for all your numbers. If there’s a chance a number will
overflow its type,

use the next larger type instead.

Q: How can you find out the type of a given variable?

A: If you’re using any of the primitive types (int, float, boolean), and so on,
you can’t. If you

care about the type, you can convert the value to some other type by using
casting. (You’ll learn

about this tomorrow.)

If you’re using class types, you can use the instanceof operator, which you’ll
learn more

about tomorrow.

Q: Why does Java have all these shorthand operators for arithmetic and
assignment? It’s

really hard to read that way.

A: The syntax of Java is based on C++, and therefore on C. One of C’s implicit
goals is the

capability of doing very powerful things with a minimum of typing. Because of
this, shorthand

operators, such as the wide array of assignments, are common.

There’s no rule that says you have to use these operators in your own programs,
however. If you

find your code to be more readable using the long form, no one will come to your
house and

make you change it.

Q: You covered simple math in this section using operators. I’m assuming that
Java has ways

of doing more complex math operations?

A: You assume correctly. A special class in the java.lang package, called
java.lang.Math,

has a number of methods for exponential, trigonometric, and other basic math
operations. In fact,

because you call these methods using the Math class itself, these are prime
examples of class

methods. You’ll learn more about this tomorrow.

January 8, 2008 Posted by | Java Help | 1 Comment

Java Help, Java Tutorials, Java Programming, Java Tricks

Lesson Two

Thinking in Objects: An Analogy

CONTENTS

Objects and Classes

Behavior and Attributes

Attributes

Behavior

Creating a Class

Inheritance, Interfaces, and Packages

Inheritance

Creating a Class Hierarchy

How Inheritance Works

Single and Multiple Inheritance

Interfaces and Packages

Creating a Subclass

Summary

Q&A

Object-oriented programming (OOP) is one of the biggest programming ideas of
recent years, and you might worry that you must spend ears learning all about
object-oriented programming methodologies and how they can make your life easier
than The Old Way of programming. It all comes down to organizing your programs
in ways that echo how things are put together in the real world.

Today you’ll get an overview of object-oriented programming concepts in Java and
how they relate to how you structure your own

programs:

What classes and objects are and how they relate to each other

The two main parts of a class or object: its behaviors and its attributes

Class inheritance and how inheritance affects the way you design your programs

Some information about packages and interfaces

If you’re already familiar with object-oriented programming, much of today’s
lesson will be old hat to you.

You may want to skim it and go to a movie today instead. Tomorrow, you’ll get
into more specific details.

Thinking in Objects: An Analogy

Consider, if you will, Legos. Legos, for those who do not spend much time with
children, are small plastic building blocks in various colors and sizes. They
have small round bits on one side that fit into small round holes on other Legos
so that they fit together snugly to create larger shapes. With different Lego
parts (Lego wheels, Lego engines, Lego hinges, Lego pulleys), you can put
together castles, automobiles, giant robots that swallow cities, or just about
anything else you can imagine. Each Lego part is a small object that fits
together with other small objects in predefined ways to create other larger
objects. That is roughly how object-oriented

programming works: putting together smaller elements to build larger ones.

Here’s another example. You can walk into a computer store and, with a little
background and often some help, assemble an entire pc computer system from
various components: a motherboard, a CPU chip, a video card, a hard disk, a
keyboard, and so on. Ideally, when you finish assembling all the various
self-contained units, you have a system in which all the units work together to
create a larger system with which you can solve the problems you bought the
computer for in the first place.

Internally, each of those components may be vastly complicated and engineered by
different companies with different methods of design. But you don’t need to know
how the component works, what every chip on the board does, or how, when you
press the A key, an A gets sent to your computer. As the assembler of the
overall system, each component you use is a self-contained unit, and all you are
interested in is how the units interact with each other. Will this video card
fit into the slots on the motherboard, and will this monitor work with this
video card? Will each particular component speak the right commands to the other
components it interacts with so that each part of the computer is understood by
every other part? Once you know what the interactions are between the components
and can match the interactions, putting together the overall system is easy.

What does this have to do with programming? Everything. Object-oriented
programming works in exactly this same way. Using object-oriented programming,
your overall program is made up of lots of different self-contained components
(objects), each of which has a specific role in the program and all of which can
talk to each other in predefined ways.

Objects and Classes

Object-oriented programming is modeled on how, in the real world, objects are
often ade up of many kindsof smaller objects. This capability of combining
objects, owever, is only one very general aspect ofobject-oriented programming.
Object-oriented rogramming provides several other concepts and features to make
creating and using objects easier and more flexible, and the most important of
these features is classes.

When you write a program in an object-oriented language, you don’t define actual
objects. You define classes of objects, where a class is a template for multiple
objects with similar features. Classes embody all the features of a particular
set of objects. For example, you might have a Tree class that describes the
features of all trees (has leaves and roots, grows, creates chlorophyll). The
Tree class serves as an abstract model for the concept of a tree-to reach out
and grab, or interact with, or cut down a tree you have to have a concrete

instance of that tree. Of course, once you have a tree class, you can create
lots of different instances of that tree, and each different tree instance can
have different features (short, tall, bushy, drops leaves in autumn), while
still behaving like and being immediately recognizable as a tree (see Figure
2.1).

Figure 2.1 : The Tree class and several Tree instances.

New Term

A class is a generic template for a set of objects with similar features.

An instance of a class is another word for an actual object. If class is the
general (generic) representation of an object, an instance is its concrete
representation. So what, precisely, is the difference between an instance and an
object? Nothing, really. Object is the more general term, but both instances and
objects are the concrete representation of a class. In fact, the terms instance
and object are often used interchangeably in OOP lingo.

An instance of a tree and a tree object are both the same thing.

New Term

An instance is the specific concrete representation of a class.

Instances and objects are the same thing.

What about an example closer to the sort of things you might want to do in Java
programming? You might create a class for the user interface element called a
button. The Button class defines the features of a button (its label, its size,
its appearance) and how it behaves. (Does it need a single-click or a
double-click to activate it? Does it change color when it’s clicked? What does
it do when it’s activated?) After you define the Button class, you can then
easily create instances of that button-that is, button objects-that all take on
the basic features of the button as defined by the class, but may have different
appearances and behavior based on what you want that particular button to do. By
creating a Button class, you don’t have to keep rewriting the code for each
individual button you want to use in your program, and you can reuse the Button
class to create different kinds of buttons as you need them in this program and
in other programs.

Tip

If you’re used to programming in C, you can think of a class as sort of creating
a new composite data type by using struct and typedef. Classes, however, can
provide much more than just a collection of data, as you’ll discover in the rest
of today’s lesson. When you write a Java program, you design and construct a set
of classes. Then when your program runs, instances of those classes are created
and discarded as needed. Your task, as a Java programmer, is to create the right
set of classes to accomplish what your program needs to accomplish. Fortunately,
you don’t have to start from the very beginning: The Java environment comes with
a standard set

of classes (called a class library) that implement a lot of the basic behavior
you need-not only for basic programming tasks (classes to provide basic math
functions, arrays, strings, and so on), but also for graphics and networking
behavior. In many cases, the Java class libraries may be enough so that all you
have to do in your Java program is create a single class that uses the standard
class libraries. For complicated Java programs, you may have to create a whole
set of classes with defined interactions between them.

New Term

A class library is a collection of classes intended to be reused repeatedly in
different programs. The standard Java class libraries contain quite a few
classes for accomplishing basic programming tasks in Java.

Behavior and Attributes

Every class you write in Java has two basic features: attributes and behavior.
In this section you’ll learn about each one as it applies to a theoretical
simple class called Motorcycle. To finish up this section, you’ll create the
Java code to implement a representation of a motorcycle.

Attributes

Attributes are the individual things that differentiate one object from another
and determine the appearance, state, or other qualities of that object. Let’s
create a theoretical class called Motorcycle. A motorcycle class might include
the following attributes and have these typical values:

l Color: red, green, silver, brown

l Style: cruiser, sport bike, standard

l Make: Honda, BMW, Bultaco

Attributes of an object can also include information about its state; for
example, you could have features for engine condition (off or on) or current
gear selected.

Attributes are defined in classes by variables. Those variables’ types and names
are defined in the class, and each object can have its own values for those
variables. Because each instance of a class can have different values for its
variables, these variables are often called instance variables.

New Term

An instance variable defines the attributes of the object. Instance variables’
types and names are defined in the class, but their values

are set and changed in the object. Instance variables may be initially set when
an object is created and stay constant throughout the life of the object, or
they may be able to change at will as the program runs. Change the value of the
variable, and you

change an object’s attributes. In addition to instance variables, there are also
class variables, which apply to the class itself and to all its

instances. Unlike instance variables, whose values are stored in the instance,
class variables’ values are stored in the class itself. You’ll learn about class
variables later on this week and more specifics about instance variables
tomorrow.

Behavior

A class’s behavior determines how an instance of that class operates; for
example, how it will “react” if asked to do something by another class or object
or if its internal state changes. Behavior is the only way objects can do
anything to themselves or have anything done to them. For example, to go back to
the theoretical Motorcycle class, here are some behaviors that the Motorcycle
class might have:

l Start the engine

l Stop the engine

l Speed up

l Change gear

l Stall

To define an object’s behavior, you create methods, a set of Java statements
that accomplish some task. Methods look and behave just like functions in other
languages but are defined and accessible solely inside a class. Java does not
have functions defined outside classes (as C++ does).

New Term

Methods are functions defined inside classes that operate on instances

of those classes.

While methods can be used solely to operate on an individual object, methods are
also used between objects to communicate with each other. A class or an object
can call methods in another class or object to communicate changes in the
environment or to ask that object to change its state. Just as there are
instance and class variables, there are also instance and class methods.
Instance methods (which are so common that they’re usually just called methods)
apply and operate on an instance of a class; class methods apply and operate on
the class itself. You’ll learn more about class methods later on this week.

Creating a Class

Up to this point, today’s lesson has been pretty theoretical. In this section,
you’ll create a working example of the Motorcycle class so that you can see how
instance variables and methods are defined in a class in Java. You’ll also
create a Java application that creates a new instance of the Motorcycle class
and shows its instance variables.

Note

I’m not going to go into a lot of detail about the actual syntax of this example
here. Don’t worry too much about it if you’re not really sure

what’s going on; it will become clear to you later on this week. All you really
need to worry about in this example is understanding the

basic parts of this class definition. Ready? Let’s start with a basic class
definition. Open the text editor you’ve been using to create Java source code
and enter the following (remember, upper- and lowercase matters):

class Motorcycle {

}

Congratulations! You’ve now created a class. Of course, it doesn’t do very much
at the moment, but that’s a

Java class at its very simplest.

First, let’s create some instance variables for this class-three of them, to be
specific. Just below the first line,

add the following three lines:

String make;

String color;

boolean engineState = false;

Here you’ve created three instance variables: Two, make and color, can contain
String objects (a string is the generic term for a series of characters; String,
with a capital S, is part of that standard class library mentioned earlier). The
third, engineState, is a boolean variable that refers to whether the engine is
off or on; a value of false means that the engine is off, and true means that
the engine is on. Note that boolean is lowercase b.

New Term

A boolean is a value of either true or false.

Technical Note

boolean in Java is a real data type that can have the values true or false.
Unlike in C, booleans are not numbers. You’ll hear about this again tomorrow so
that you won’t forget. Now let’s add some behavior (methods) to the class. There
are all kinds of things a motorcycle can do, but to keep things short, let’s add
just one method-a method that starts the engine. Add the following lines below
the

instance variables in your class definition:

void startEngine() {

if (engineState == true)

System.out.println(“The engine is already on.”);

else {

engineState = true;

System.out.println(“The engine is now on.”);

}

}

The startEngine() method tests to see whether the engine is already running (in
the part engineState == true) and, if it is, merely prints a message to that
effect. If the engine isn’t already running, it changes the state of the engine
to true (turning the engine on) and then prints a message. Finally, because the
startEngine() method doesn’t return a value, its definition includes the word
void at the beginning. (You can also define methods to return values; you’ll
learn more about method definitions on Day 6, “Creating Classes and Applications
in Java.”)

Tip

Here and throughout this book, whenever I refer to the name of a method, I’ll
add empty parentheses to the end of the name (for

example, as I did in the first sentence of the previous paragraph: “The
startEngine() method…” This is a convention used in the

programming community at large to indicate that a particular name is a method
and not a variable. The parentheses are silent.

With your methods and variables in place, save the program to a file called
Motorcycle.java (remember that you should always name your Java source files the
same names as the class they define). Listing 2.1 shows what your program should
look like so far.

Listing 2.1. The Motorcycle.java file.

1:class Motorcycle {

2:

3: String make;

4: String color;

5: boolean engineState = false;

6:

7: void startEngine() {

8: if (engineState == true)

9: System.out.println(“The engine is already on.”);

10: else {

11: engineState = true;

12: System.out.println(“The engine is now on.”);

13: }

14: }

15:}

Tip

The indentation of each part of the class isn’t important to the Java compiler.
Using some form of indentation, however, makes your lass definition easier for
you and other people to read. The indentation used here, with instance variables
and methods indented from the lass definition, is the style used throughout this
book. The Java class libraries use a similar indentation. You can choose any
indentation style hat you like.

Before you compile this class, let’s add one more method just below the
startEngine() method (that is, between lines 14 and 15). The showAtts() method
is used to print the current values of all the instance variables in an instance
of your Motorcycle class. Here’s what it looks like:

void showAtts() {

System.out.println(“This motorcycle is a “

+ color + ” ” + make);

if (engineState == true)

System.out.println(“The engine is on.”);

else System.out.println(“The engine is off.”);

}

The showAtts() method prints two lines to the screen: the make and color of the
motorcycle object and whether the engine is on or off. Now you have a Java class
with three instance variables and two methods defined. Save that file again, and
compile it using one of the following methods:

Note

After this point, I’m going to assume you know how to compile and run Java
programs. I won’t repeat this information after this.

Windows

From inside a DOS shell, CD to the directory containing your Java source file,
and use the javac command to compile it:

javac Motorcycle.java

Macintosh

Drag and drop the Motorcycle.java file onto the Java Compiler icon.

Salaris

From a command line, CD to the directory containing your Java source file, and
use the javac command to compile it:

javac Motorcycle.java

When you run this little program using the java or Java Runner programs, you’ll
get an error. Why? When you run a compiled Java class directly, Java assumes
that the class is an application and looks for a main()

method. Because we haven’t defined a main() method inside the class, the Java
interpreter (java) gives you

an error something like one of these two errors:

In class Motorcycle: void main(String argv[]) is not defined

Exception in thread “main”: java.lang.UnknownError

To do something with the Motorcycle class-for example, to create instances of
that class and play with them-you’re going to need to create a separate Java
applet or application that uses this class or add a main()method to this one.
For simplicity’s sake, let’s do the latter. Listing 2.2 shows the main() method
you’ll add to the Motorcycle class. You’ll want to add this method to your
Motorcycle.java source file just before the last closing brace (}), underneath
the startEngine() and showAtts() methods.

Listing 2.2. The main() method for Motorcycle.java.

1: public static void main (String args[]) {

2: Motorcycle m = new Motorcycle();

3: m.make = “Yamaha RZ350″;

4: m.color = “yellow”;

5: System.out.println(“Calling showAtts…”);

6: m.showAtts();

7: System.out.println(“——–“);

8: System.out.println(“Starting engine…”);

9: m.startEngine();

10: System.out.println(“——–“);

11: System.out.println(“Calling showAtts…”);

12: m.showAtts();

13: System.out.println(“——–“);

14: System.out.println(“Starting engine…”);

15: m.startEngine();

16:}

With the main() method in place, the Motorcycle class is now an official
application, and you can

compile it again and this time it’ll run. Here’s how the output should look:

Calling showAtts…

This motorcycle is a yellow Yamaha RZ350

The engine is off.

——–

Starting engine…

The engine is now on.

——–

Calling showAtts…

This motorcycle is a yellow Yamaha RZ350

The engine is on.

——–

Starting engine…

The engine is already on.

Analysis

The contents of the main() method are all going to look very new to you, so
let’s go through it line by line so that you at least have a basic idea of what
it does (you’ll get details about the specifics of all of this tomorrow and the
day after).

The first line declares the main() method. The first line of the main() method
always looks like this; you’ll learn the specifics of each part later this week.

Line 2, Motorcycle m = new Motorcycle();, creates a new instance of the
Motorcycle class and stores a reference to it in the variable m. Remember, you
don’t usually operate directly on classes in your Java programs; instead, you
create objects from those classes and then call methods in those objects. Lines
3 and 4 set the instance variables for this Motorcycle object: The make is now a
Yamaha RZ350 (a very pretty motorcycle from the mid-1980s), and the color is
yellow.

Lines 5 and 6 call the showAtts() method, defined in your Motorcycle object.
(Actually, only 6 does; 5 just prints a message that you’re about to call this
method.) The new motorcycle object then prints out the values of its instance
variables-the make and color as you set in the previous lines-and shows that the
engine is off.

Line 7 prints a divider line to the screen; this is just for prettier output.

Line 9 calls the startEngine() method in the motorcycle object to start the
engine. The engine should now be on.

Line 11 prints the values of the instance variables again. This time, the report
should say the engine is now on.

Line 15 tries to start the engine again, just for fun. Because the engine is
already on, this should print the message The engine is already on.

Listing 2.3 shows the final Motorcycle class, in case you’ve been having trouble
compiling and running the one you’ve got (and remember, this example and all the
examples in this book are available on the CD that accompanies the book):

Listing 2.3. The final version of Motorcycle.java.

1: class Motorcycle {

2:

3: String make;

4: String color;

5: boolean engineState;

6:

7: void startEngine() {

8: if (engineState == true)

9: System.out.println(“The engine is already on.”);

10: else {

11: engineState = true;

12: System.out.println(“The engine is now on.”);

13: }

14: }

15:

16: void showAtts() {

17: System.out.println(“This motorcycle is a “

18: + color + ” ” + make);

19: if (engineState == true)

20: System.out.println(“The engine is on.”);

21: else System.out.println(“The engine is off.”);

22: }

23:

24: public static void main (String args[]) {

25: Motorcycle m = new Motorcycle();

26: m.make = “Yamaha RZ350″;

27: m.color = “yellow”;

28: System.out.println(“Calling showAtts…”);

29: m.showAtts();

30: System.out.println(“——“);

31: System.out.println(“Starting engine…”);

32: m.startEngine();

33: System.out.println(“——“);

34: System.out.println(“Calling showAtts…”);

35: m.showAtts();

36: System.out.println(“——“);

37: System.out.println(“Starting engine…”);

38: m.startEngine();

39: }

40:}

Inheritance, Interfaces, and Packages

Now that you have a basic grasp of classes, objects, methods, variables, and how
to put them all together in a Java program, it’s time to confuse you again.
Inheritance, interfaces, and packages are all mechanisms for organizing classes
and class behaviors. The Java class libraries use all these concepts, and the
best class libraries you write for your own programs will also use these
concepts.

Inheritance

Inheritance is one of the most crucial concepts in object-oriented programming,
and it has a very direct effect on how you design and write your Java classes.
Inheritance is a powerful mechanism that means when you write a class you only
have to specify how that class is different from some other class; inheritance
will give you automatic access to the information contained in that other class.

With inheritance, all classes-those you write, those from other class libraries
that you use, and those from the standard utility classes as well-are arranged
in a strict hierarchy (see Figure 2.2). Each class has a superclass (the class
above it in the hierarchy), and each class can have one or more subclasses
(classes below that class in the hierarchy). Classes further down in the
hierarchy are said to inherit from classes further up in the hierarchy.

Figure 2.2 : A class hierarchy.

Subclasses inherit all the methods and variables from their superclasses-that
is, in any particular class, if the superclass defines behavior that your class
needs, you don’t have to redefine it or copy that code from some other class.
Your class automatically gets that behavior from its superclass, that superclass
gets behavior from its superclass, and so on all the way up the hierarchy. Your
class becomes a combination of all the features of the classes above it in the
hierarchy.

New Term

Inheritance is a concept in object-oriented programming where all classes are
arranged in a strict hierarchy. Each class in the hierarchy

has superclasses (classes above it in the hierarchy) and any number of
subclasses (classes below it in the hierarchy). Subclasses inherit

attributes and behavior from their superclasses.

At the top of the Java class hierarchy is the class Object; all classes inherit
from this one superclass. Object is the most general class in the hierarchy; it
defines behavior inherited by all the classes in Java. Each class further down
in the hierarchy adds more information and becomes more tailored to a specific
purpose. In this way, you can think of a class hierarchy as defining very
abstract concepts at the top of the hierarchy and those ideas becoming more
concrete the farther down the chain of superclasses you go. Most of the time
when you write new Java classes, you’ll want to create a class that has all the
information some other class has, plus some extra information. For example, you
may want a version of a Button with its own built-in label. To get all the
Button information, all you have to do is define your class to inherit from
Button. Your class will automatically get all the behavior defined in Button
(and in Button’s superclasses), so all you have to worry about are the things
that make your class different from Button itself.

This mechanism for defining new classes as the differences between them and
their superclasses is called subclassing.

Subclassing involves creating a new class that inherits from some other class in
the class hierarchy. Using subclassing, you only need to define the differences
between your class and its parent; the additional behavior is all available to
your class through inheritance.

New Term

Subclassing is the process of creating a new class that inherits from some other
already-existing class.

What if your class defines an entirely new behavior and isn’t really a subclass
of another class? Your class can also inherit directly from Object, which still
allows it to fit neatly into the Java class hierarchy. In fact, if you create a
class definition that doesn’t indicate its superclass in the first line, Java
automatically assumes you’re inheriting from Object. The Motorcycle class you
created in the previous section inherited from

Object.

Creating a Class Hierarchy

If you’re creating a larger set of classes for a very complex program, it makes
sense for your classes not only to inherit from the existing class hierarchy,
but also to make up a hierarchy themselves. This may take some planning
beforehand when you’re trying to figure out how to organize your Java code, but
the advantages are significant once it’s done:

When you develop your classes in a hierarchy, you can factor out information
common to multiple classes in superclasses, and then reuse that superclass’s
information over and over again. Each subclass gets that common information from
its superclass.

l

Changing (or inserting) a class further up in the hierarchy automatically
changes the behavior of its subclasses-no need to change or recompile any of the
lower classes because they get the new information through inheritance and not
by copying any of the code.

l

For example, let’s go back to that Motorcycle class and pre tend you created a
Java program to implement all the features of a motorcycle. It’s done, it works,
and everything is fine. Now, your next task is to create a Java class called
Car.

Car and Motorcycle have many similar features-both are vehicles driven by
engines. Both have transmissions, headlamps, and speedometers. So your first
impulse may be to open your Motorcycle class file and copy over a lot of the
information you already defined into the new class Car.

A far better plan is to factor out the common information for Car and Motorcycle
into a more general class hierarchy. This may be a lot of work just for the
classes Motorcycle and Car, but once you add Bicycle, Scooter, Truck, and so on,
having common behavior in a reusable superclass significantly reduces the amount
of work you have to do overall.

Let’s design a class hierarchy that might serve this purpose. Starting at the
top is the class Object, which is the root of all Java classes. The most general
class to which a motorcycle and a car both belong might be called Vehicle. A
vehicle, generally, is defined as a thing that propels someone from one place to
another. In the Vehicle class, you define only the behavior that enables someone
to be propelled from point a to point b, and nothing more.

Below Vehicle? How about two classes: PersonPoweredVehicle and
EnginePoweredVehicle? EnginePoweredVehicle is different from Vehicle because it
has an engine, and the behaviors might include stopping and starting the engine,
having certain amounts of gasoline and oil, and perhaps the speed or gear in
which the engine is running. Person-powered vehicles have some kind of mechanism
for translating people motion into vehicle motion-pedals, for example. Figure
2.3 shows what you have so far.

Figure 2.3 : The basic vehicle hierarchy.

Now let’s become even more specific. With EnginePoweredVehicle, you might have
several classes:

Motorcycle, Car, Truck, and so on. Or you can factor out still more behavior and
have intermediate

classes for TwoWheeled and FourWheeled vehicles, with different behaviors for
each (see Figure 2.4).

Figure 2.4 : Two-wheeled and four-wheeled vehicles.

Finally, with a subclass for the two-wheeled engine-powered vehicles, you can
have a class for motorcycles. Alternatively, you could additionally define
scooters and mopeds, both of which are two-wheeled engine-powered vehicles but
have different qualities from motorcycles. Where do qualities such as make or
color come in? Wherever you want them to go-or, more usually, where they fit
most naturally in the class hierarchy. You can define the make and color on
Vehicle, and all the subclasses will have those variables as well. The point to
remember is that you have to define a feature or a behavior only once in the
hierarchy; it’s automatically reused by each subclass.

How Inheritance Works

How does inheritance work? How is it that instances of one class can
automatically get variables and methods from the classes further up in the
hierarchy?

For instance variables, when you create a new instance of a class, you get a
“slot” for each variable defined in the current class and for each variable
defined in all its superclasses. In this way, all the classes combine to form a
template for the current object, and then each object fills in the information
appropriate to its situation. Methods operate similarly: New objects have access
to all the method names of its class and its superclasses, but method
definitions are chosen dynamically when a method is called. That is, if you call
a method on a particular object, Java first checks the object’s class for the
definition of that method. If it’s not defined in the object’s class, it looks
in that class’s superclass, and so on up the chain until the method definition
is found (see Figure 2.5).

Figure 2.5 : How methods are located.

Things get complicated when a subclass defines a method that has the same
signature (name, number, and type of arguments) as a method defined in a
superclass. In this case, the method definition that is found first (starting at
the bottom and working upward toward the top of the hierarchy) is the one that
is actually executed. Therefore, you can intentionally define a method in a
subclass that has the same signature as a method in a superclass, which then
“hides” the superclass’s method. This is called overriding a method. You’ll
learn all about methods on Day 7, “More About Methods.”

New Term

Overriding a method is creating a method in a subclass that has the same
signature (name, number, and type of arguments) as a method in a superclass.
That new method then hides the superclass’s method

(see Figure 2.6).

Figure 2.6 : Overriding methods.

Single and Multiple Inheritance

Java’s form of inheritance, as you learned in the previous sections, is called
single inheritance. Single inheritance means that each Java class can have only
one superclass (although any given superclass can have multiple subclasses).

In other object-oriented programming languages, such as C++, classes can have
more than one superclass, and they inherit combined variables and methods from
all those classes. This is called multiple inheritance. Multiple inheritance can
provide enormous power in terms of being able to create classes that factor just
about all imaginable behavior, but it can also significantly complicate class
definitions and the code to produce them.

Java makes inheritance simpler by being only singly inherited.

Interfaces and Packages

There are two remaining concepts to discuss here: packages and interfaces. Both
are advanced topics for implementing and designing groups of classes and class
behavior. You’ll learn about both interfaces and packages on Day 16, “Packages
and Interfaces,” but they are worth at least introducing here.

Recall that each Java class has only a single superclass, and it inherits
variables and methods from that superclass and all its superclasses. Although
single inheritance makes the relationship between classes and the functionality
those classes implement easy to understand and to design, it can also be
somewhat restrictive-in particular, when you have similar behavior that needs to
be duplicated across different “branches” of the class hierarchy. Java solves
this problem of shared behavior by using the concept of interfaces, which
collect

method names into one place and then allow you to add those methods as a group
to the various classes that need them. Note that interfaces contain only method
names and interfaces (arguments, for example), not actual definitions.

Although a single Java class can have only one superclass (due to single
inheritance), that class can also implement any number of interfaces. By
implementing an interface, a class provides method implementations (definitions)
for the method names defined by the interface. If two very disparate classes
implement the same interface, they can both respond to the same method calls (as
defined by that interface), although what each class actually does in response
to those method calls may be very different.

New Term

An interface is a collection of method names, without definitions, that can be
added to classes to provide additional behavior not included

with those methods the class defined itself or inherited from its superclasses.
You don’t need to know very much about interfaces right now. You’ll learn more
as the book progresses, so if all this is very confusing, don’t panic! The final
new Java concept for today is packages. Packages in Java are a way of grouping
together related classes and interfaces in a single library or collection.
Packages enable modular groups of classes to be available only if they are
needed and eliminate potential conflicts between class names in different groups
of classes.

You’ll learn all about packages, including how to create and use them, in Week
3. For now, there are only a

few things you need to know:

l The class libraries in the Java Developer’s Kit are contained in a package
called java. The classes in the java package are guaranteed to be available in
any Java implementation and are the only classes guaranteed to be available
across different implementations. The java package itself contains other
packages for classes that define the language, the input and output classes,
some basic networking, the

window toolkit functions, and classes that define applets. Classes in other
packages (for example, classes in the sun or netscape packages) may be available
only in specific implementations.

By default, your Java classes have access to only the classes in java.lang (the
base language package inside the java package). To use classes from any other
package, you have to either refer to them explicitly by package name or import
them into your source file.

l

To refer to a class within a package, list all the packages that class is
contained in and the class name, all separated by periods (.). For example, take
the Color class, which is contained in the awt package (awt stands for Abstract
Windowing Toolkit). The awt package, in turn, is inside the java package. To
refer to the Color class in your program, you use the notation java.awt.Color.

l

Creating a Subclass

To finish up today, let’s create a class that is a subclass of another class and
override some methods. You’ll also get a basic feel for how packages work in
this example.

Probably the most typical instance of creating a subclass, at least when you
first start programming in Java, is creating an applet. All applets are
subclasses of the class Applet (which is part of the java.applet package). By
creating a subclass of Applet, you automatically get all the behavior from the
window toolkit and the layout classes that enable your applet to be drawn in the
right place on the page and to interact with system operations, such as
keypresses and mouse clicks. In this example, you’ll create an applet similar to
the Hello World applet from yesterday, but one that draws the Hello string in a
larger font and a different color. To start this example, let’s first construct
the class definition itself. Let’s go to your text editor, and enter the
following class definition:

public class HelloAgainApplet extends java.applet.Applet {

}

Here, you’re creating a class called HelloAgainApplet. Note the part that says
extends java.applet.Applet-that’s the part that says your applet class is a
subclass of the Applet class. Note that because the Applet class is contained in
the java.applet package, you don’t have automatic access to that class, and you
have to refer to it explicitly by package and class name.

The other part of this class definition is the public keyword. Public means that
your class is available to the Java system at large once it is loaded. Most of
the time you need to make a class public only if you want it to be visible to
all the other classes in your Java program, but applets, in particular, must be
declared to be public. (You’ll learn more about public classes in Week 3.)

A class definition with nothing in it doesn’t really have much of a point;
without adding or overriding any of its superclasses’ variables or methods,
there’s no reason to create a subclass at all. Let’s add some information to
this class, inside the two enclosing braces, to make it different from its
superclass.

First, add an instance variable to contain a Font object:

Font f = new Font(“TimesRoman”, Font.BOLD, 36);

The f instance variable now contains a new instance of the class Font, part of
the java.awt package. This particular Font object is a Times Roman font,
boldface, 36 points high. In the previous Hello World applet, the font used for
the text was the default font: 12-point Times Roman. Using a Font object, you
can change the font of the text you draw in your applet.

By creating an instance variable to hold this font object, you make it available
to all the methods in your class. Now let’s create a method that uses it.

When you write applets, there are several “standard” methods defined in the
applet superclasses that you will commonly override in your applet class. These
include methods to initialize the applet, to make it start running, to handle
operations such as mouse movements or mouse clicks, or to clean up when the
applet stops running. One of those standard methods is the paint() method, which
actually displays your applet onscreen. The default definition of paint()
doesn’t do anything-it’s an empty method. By overriding paint(), you tell the
applet just what to draw on the screen. Here’s a definition of paint():

public void paint(Graphics g) {

g.setFont(f);

g.setColor(Color.red);

g.drawString(“Hello again!”, 5, 40);

}

There are two things to know about the paint() method. First, note that this
method is declared public, just as the applet itself was. The paint() method is
actually public for a different reason-because the method it’s overriding is
also public. If a superclass’s method is defined as public, your override method
also has to be public, or you’ll get an error when you compile the class.

Second, note that the paint() method takes a single argument: an instance of the
Graphics class. The Graphics class provides platform-independent behavior for
rendering fonts, colors, and behavior for drawing basic lines and shapes. You’ll
learn a lot more about the Graphics class in Week 2, when you create more
extensive applets.

Inside your paint() method, you’ve done three things:

You’ve told the graphics object that the default drawing font will be the one
contained in the instance

variable f.

l

l You’ve told the graphics object that the default color is an instance of the
Color class for the color red. Finally, you’ve drawn your “Hello Again!” string
onto the screen, at the x and y positions of 5 and 25. The string will be
rendered in the new font and color.

l

For an applet this simple, this is all you need to do. Here’s what the applet
looks like so far:

public class HelloAgainApplet extends java.applet.Applet {

Font f = new Font(“TimesRoman”,Font.BOLD,36);

public void paint(Graphics g) {

g.setFont(f);

g.setColor(Color.red);

g.drawString(“Hello again!”, 5, 40);

}

}

If you’ve been paying close attention, you’ll notice that something is wrong
with this example up to this point. If you don’t know what it is, try saving
this file (remember, save it to the same name as the class:
HelloAgainApplet.java) and compiling it. You should get a bunch of errors
similar to this one:

HelloAgainApplet.java:7: Class Graphics not found in type declaration.

Why are you getting these errors? Because the classes you’re referring to in
this class, such as Graphics and Font, are part of a package that isn’t
available by default. Remember that the only package you have access to
automatically in your Java programs is java.lang. You referred to the Applet
class in the first line of the class definition by referring to its full package
name (java.applet.Applet). Further on in the program,

however, you referred to all kinds of other classes as if they were available.
The compiler catches this and tells you that you don’t have access to those
other classes.

There are two ways to solve this problem: Refer to all external classes by full
package name or import the appropriate class or package at the beginning of your
class file. Which one you choose to do is mostly a matter of choice, although if
you find yourself referring to a class in another package lots of times, you may
want to import it to cut down on the amount of typing.

In this example, you’ll import the classes you need. There are three of them:
Graphics, Font, and Color.

All three are part of the java.awt package. Here are the lines to import these
classes. These lines go at the

top of your program, before the actual class definition:

import java.awt.Graphics;

import java.awt.Font;

import java.awt.Color;

Tip

You also can import an entire package of public classes by using an asterisk (*)
in place of a specific class name. For example, to

import all the classes in the awt package, you can use this line:

import java.awt.*;

Now, with the proper classes imported into your program, HelloAgainApplet.java
should compile cleanly to a class file. Listing 2.4 shows the final version to
double-check.

Listing 2.4. The final version of HelloAgainApplet.java.

1:import java.awt.Graphics;

2:import java.awt.Font;

3:import java.awt.Color;

4:

5:public class HelloAgainApplet extends java.applet.Applet {

6:

7: Font f = new Font(“TimesRoman”,Font.BOLD,36);

8:

9: public void paint(Graphics g) {

10: g.setFont(f);

11: g.setColor(Color.red);

12: g.drawString(“Hello again!”, 5, 40);

13: }

14:}

To test it, create an HTML file with the <APPLET> tag as you did yesterday.
Here’s an HTML file to use:

<HTML>

<HEAD>

<TITLE>Another Applet</TITLE>

</HEAD>

<BODY>

<P>My second Java applet says:

<BR><APPLET CODE=”HelloAgainApplet.class” WIDTH=200 HEIGHT=50>

</APPLET>

</BODY>

</HTML>

For this HTML example, your Java class file is in the same directory as this
HTML file. Save the file to HelloAgainApplet.html and fire up your Java-enabled
browser or the Java applet viewer. Figure 2.7 shows the result you should be
getting (the “Hello Again!” string is red).

Figure 2.7 : The HelloAgain applet.

Summary

If this is your first encounter with object-oriented programming, a lot of the
information in this lesson is going to seem really theoretical and overwhelming.
Fear not-the further along in this book you get, and the more Java classes and
applications you create, the easier it is to understand.

One of the biggest hurdles of object-oriented programming is not necessarily the
concepts; it’s their names. OOP has lots of jargon surrounding it. To summarize
today’s material, here’s a glossary of terms and concepts you learned today:

class: A template for an object, which contains variables and methods
representing behavior and

attributes. Classes can inherit variables and methods from other classes.

class method: A method defined in a class, which operates on the class itself
and can be called via the class or any of its instances.

class variable: A variable that is “owned” by the class and all its instances as
a whole and is stored in the class.

instance: The same thing as an object; each object is an instance of some class.

instance method: A method defined in a class, which operates on an instance of
that class. Instance methods are usually called just methods.

instance variable: A variable that is owned by an individual instance and whose
value is stored in the instance.

interface: A collection of abstract behavior specifications that individual
classes can then implement.

object: A concrete instance of some class. Multiple objects that are instances
of the same class have access to the same methods, but often have different
values for their instance variables.

package: A collection of classes and interfaces. Classes from packages other
than java.lang must be explicitly imported or referred to by full package name.

subclass: A class lower in the inheritance hierarchy than its parent, the
superclass. When you create a new class, it’s often called subclassing.

superclass: A class further up in the inheritance hierarchy than its child, the
subclass.

Q&A

Q: Methods are effectively functions that are defined inside classes. If they
look like functions and act like functions, why aren’t they called functions?

A: Some object-oriented programming languages do call them functions (C++ calls
them member functions). Other object-oriented languages differentiate between
functions inside and outside a body of a class or object, where having separate
terms is important to understanding how each works.

Because the difference is relevant in other languages and because the term
method is now in such common use in object-oriented technology, Java uses the
word as well.

Q: I understand instance variables and methods, but not the idea of class
variables and methods.

A: Most everything you do in a Java program will be with objects. Some behaviors
and attributes, however, make more sense if they are stored in the class itself
rather than in the object. For example, to create a new instance of a class, you
need a method that is defined and available in the class itself. (Otherwise, how
can you create an object? You need an object to call the method, but you don’t
have

an object yet.) Class variables, on the other hand, are often used when you have
an attribute whose value you want to share with all the instances of a class.

Most of the time, you’ll use instance variables and methods. You’ll learn more
about class variables and methods later this week.

January 4, 2008 Posted by | Java Help | 2 Comments

Java Help, Java Tutorials, Java Programming, Java Tricks

Lesson One

Java Programming

What Is Java?

Java’s Past, Present, and Future

Why Learn Java?

Java Is Platform Independent

Java Is Object Oriented

Java Is Easy to Learn

Getting Started Programming in Java

Getting a Java Development Environment

Installing the JDK and Sample Files

Configuring the JDK

Creating a Java Application

Creating a Java Applet

Troubleshooting

Summary

Q&A

What exactly Java is, and its current status

Why you should learn Java-its various features and advantages over other
programming languages Getting started programming in Java-what you’ll need in
terms of software and background, as well as some basic terminology

How to create your first Java programs-to close this day, you’ll create both a
simple Java application and a simple Java applet!

What Is Java?

Based on the enormous amount of press Java is getting and the amount of
excitement it has generated, you may get the impression that Java will save the
world-or at least solve all the problems of the Internet.

Not so. Java’s hype has run far ahead of its capabilities, and while Java is
indeed new and interesting, it really is another programming language with which
you write programs that run on the Internet. In this respect, Java is closer to
popular programming languages such as C, C++, Visual Basic, or Pascal, than it
is to a page description language such as HTML, or a very simple scripting
language such as JavaScript.

More specifically, Java is an object-oriented programming language developed by
Sun Microsystems, a company best known for its high-end UNIX workstations.
Modeled after C++, the Java language was designed to be small, simple, and
portable across platforms and operating systems, both at the source and at the
binary level, which means that Java programs (applets and applications) can run
on any machine that has the Java virtual machine installed (you’ll learn more
about this later).

Java is usually mentioned in the context of the World Wide Web, where browsers
such as Netscape’s Navigator and Microsoft’s Internet Explorer claim to be “Java
enabled.” Java enabled means that the browser in question can download and play
Java programs, called applets, on the reader’s system. Applets appear in a Web
page much the same way as images do, but unlike images, applets are dynamic and
interactive. Applets can be used to create animation, figures, forms that
immediately respond to inputfrom the reader, games, or other interactive effects
on the same Web pages among the text and graphics.

Figure 1.1 shows an applet running in Netscape 3.0. (This applet, at http://prominence.com/java/poetry/,
is an electronic version of the refrigerator magnets

that you can move around to create poetry or messages.)

New Term

Applets are programs that are downloaded from the World Wide Web by a Web
browser and run inside an HTML Web page. You’ll need a Java-enabled browser such
as Netscape Navigator or Microsoft’s

Internet Explorer to run applets.

To create an applet, you write it in the Java language, compile it using a Java
compiler, and refer to that applet in your HTML Web pages. You put the resulting
HTML and Java files on a Web site in the same

way that you make ordinary HTML and image files available. Then, when someone
using a Java-enabled browser views your page with the embedded applet, that
browser downloads the applet to the local system and executes it, allowing your
reader to view and interact with your applet in all its glory. (Readers using
other browsers may see text, a static graphic, or nothing.) You’ll learn more
about how applets, browsers, and the World Wide Web work together later in this
book.

While applets are probably the most popular use of Java, the important thing to
understand about Java s that you can do so much more with it than create and use
applets. Java was written as a full-fledged general-purpose programming language
in which you can accomplish the same sorts of tasks and solve the same sorts of
problems that you can in other programming languages, such as C or C++.

Java’s Past, Present, and Future

The Java language was developed at Sun Microsystems in 1991 as part of a
research project to develop software for consumer electronics devices-television
sets, VCRs, toasters, and the other sorts of machines

you can buy at any department store. Java’s goals at that time were to be small,
fast, efficient, and easily portable to a wide range of hardware devices. Those
same goals made Java an ideal language for

distributing executable programs via the World Wide Web and also a
general-purpose programming language for developing programs that are easily
usable and portable across different platforms.

The Java language was used in several projects within Sun (under the name Oak),
but did not get very much commercial attention until it was paired with HotJava.
HotJava, an experimental World Wide Web

browser, was written in 1994 in a matter of months, both as a vehicle for
downloading and running applets and also as an example of the sort of complex
application that can be written in Java. Although HotJava got a lot of attention
in the Web community, it wasn’t until Netscape incorporated HotJava’s

ability to play applets into its own browser that Java really took off and
started to generate the excitement that it has both on and off the World Wide
Web. Java has generated so much excitement, in fact, that

inside Sun the Java group spun off into its own subsidiary called JavaSoft.

Versions of Java itself, or, as it’s most commonly called, the Java API,
correspond to versions of Sun’s Java Developer’s Kit, or JDK. As of this
writing, the current version of the JDK is 1.0.2. Previously

released versions of the JDK (alphas and betas) did not have all the features or
had a number of security-related bugs. Most Java tools and browsers conform to
the features in the 1.0.2 JDK, and all the

examples in this book run on that version as well.

The next major release of the JDK and therefore of the Java API will be 1.1,
with a prerelease version available sometime in the later part of 1996. This
release will have few changes to the language, but a

number of additional capabilities and features added to the class library.
Throughout this book, if a feature will change or will be enhanced in 1.1, we’ll
let you know, and in the last two days of this book

you’ll find out more about new Java features for 1.1 and for the future.

Currently, to program in Java, you’ll need a Java development environment of
some sort for your platform. Sun’s JDK works just fine for this purpose and
includes tools for compiling and testing Java

applets and applications. In addition, a wide variety of excellent Java
development environments have been developed, including Sun’s own Java Workshop,
Symantec’s Café, Microsoft’s Visual J++ (which is

indeed a Java tool, despite its name), and Natural Intelligence’s Roaster, with
more development tools appearing all the time.

To run and view Java applets, you’ll need a Java-enabled browser or other tool.
As mentioned before, recent versions of Netscape Navigator (2.0 and higher) and
Internet Explorer (3.0) can both run Java

applets. (Note that for Windows you’ll need the 32-bit version of Netscape, and
for Macintosh you’ll need Netscape 3.0.) You can also use Sun’s own HotJava
browser to view applets, as long as you have the 1.0

prebeta version (older versions are not compatible with newer applets, and vice
versa). Even if you don’t have a Java-enabled browser, many development tools
provide simple viewers with which you can run

your applets. The JDK comes with one of these; it’s called the appletviewer.

Note

If you’re running Windows 3.x as your main system, very few toolsexist for you
to be able to work with Java. As I write this, the only

Java tool available for writing and running Java applets is a version ofthe JDK
from IBM called the ADK. You can write applets using this

tool, and view them using the applet viewer that comes with thatpackage (neither
Netscape nor Internet Explorer will run Java applets

on Windows 3.1). See http://www.alphaWorks.ibm.com/ for more information.

What’s in store for Java in the future? A number of new developments have been
brewing (pardon the

pun):

Sun is developing a number of new features for the Java environment, including a
number of new class libraries for database integration, multimedia, electronic
commerce, and other uses. Sun also

has a Java-based Web server, a Java-based hardware chip (with which you can
write Java-specific systems), and a Java-based operating system. You’ll learn
about all these things later in this book.

The 1.1 release of the JDK will include many of these features; others will be
released as separate

packages.

Sun is also developing a framework called Java Beans, which will allow the
development of component objects in Java, similarly to Microsoft’s ActiveX (OLE)
tech-nology. These different

components can then be easily combined and interact with each other using
standard component assembly tools. You’ll learn more about Java Beans later in
this book.

Java capabilities will be incorporated into a wide variety of operating systems,
including Solaris, Windows 95, and MacOS. This means that Java applications (as
opposed to applets) can run nearly

anywhere without needing additional software to be installed.

Many companies are working on performance enhancements for Java programs,
including the aforementioned Java chip and what are called just-in-time
compilers.

Why Learn Java?

At the moment, probably the most compelling reason to learn Java-and probably
the reason you bought this book-is that applets are written in Java. Even if
that were not the case, Java as a programming

language has significant advantages over other languages and other environments
that make it suitable for just about any programming task. This section
describes some of those advantages.

Java Is Platform Independent Platform independence-that is, the ability of a
program to move easily from one computer system to

another-is one of the most significant advantages that Java has over other
programming languages, particularly if your software needs to run on many
different platforms. If you’re writing software for the

World Wide Web, being able to run the same program on many different systems is
crucial to that program’s success. Java is platform independent at both the
source and the binary level.

New Term

Platform independence means that a program can run on anycomputer system. Java
programs can run on any system for which a Java virtual machine has been
installed.

At the source level, Java’s primitive data types have consistent sizes across
all development platforms. Java’s foundation class libraries make it easy to
write code that can be moved from platform to platform

without the need to rewrite it to work with that platform. When you write a
program in Java, you don’t need to rely on features of that particular operating
system to accomplish basic tasks. Platform

independence at the source level means that you can move Java source files from
system to system and have them compile and run cleanly on any system.

Platform independence in Java doesn’t stop at the source level, however. Java
compiled binary files are also platform independent and can run on multiple
platforms (if they have a Java virtual machine

available) without the need to recompile the source.

Normally, when you compile a program written in C or in most other languages,
the compiler translates your program into machine code or processor
instructions. Those instructions are specific to the

processor your computer is running-so, for example, if you compile your code on
an Intel-based system,the resulting program will run only on other Intel-based
systems. If you want to use the same program on

another system, you have to go back to your original source code, get a compiler
for that system, and recompile your code so that you have a program specific to
that system. Figure 1.2 shows the result of

this system: multiple executable programs for multiple systems.

Things are different when you write code in Java. The Java development
environment actually has two parts: a Java compiler and a Java interpreter. The
Java compiler takes your Java program and, instead of

generating machine codes from your source files, it generates bytecodes.
Bytecodes are instructions that look a lot like machine code, but are not
specific to any one processor.

To execute a Java program, you run a program called a bytecode interpreter,
which in turn reads the bytecodes and executes your Java program (see Figure
1.3). The Java bytecode interpreter is often also

called the Java virtual machine or the Java runtime.

New Term

Java bytecodes are a special set of machine instructions that are notspecific to
any one processor or computer system. A platform-specific bytecode interpreter
executes the Java bytecodes. The bytecodeinterpreter is also called the Java
virtual machine or the Java runtime

interpreter.

Where do you get the bytecode interpreter? For applets, the bytecode interpreter
is built into every Java-enabled browser, so you don’t have to worry about
it-Java applets just automatically run. For more

general Java applications, you’ll need to have the interpreter installed on your
system in order to run that Java program. Right now, you can get the Java
interpreter as part of your development environment, or if

you buy a Java program, you’ll get it with that package. In the future, however,
the Java bytecode interpreter will most likely come with every new operating
system-buy a Windows machine, and you’ll

get Java for free.

Why go through all the trouble of adding this extra layer of the bytecode
interpreter? Having your Java programs in bytecode form means that instead of
being specific to any one system, your programs can be

run on any platform and any operating or window system as long as the Java
interpreter is available. This capability of a single binary file to be
executable across platforms is crucial to what makes applets work

because the World Wide Web itself is also platform independent. Just as HTML
files can be read on any platform, so can applets be executed on any platform
that has a Java-enabled browser.

The disadvantage of using bytecodes is in execution speed. Because
system-specific programs run directly on the hardware for which they are
compiled, they run significantly faster than Java bytecodes,

which must be processed by the interpreter. For many basic Java programs, speed
may not be an issue. If you write programs that require more execution speed
than the Java interpreter can provide, you have

several solutions available to you, including being able to link native code
into your Java program or using special tools (called just-in-time compilers) to
convert your Java bytecodes into native code and

speed up their execution. Note that by using any of these solutions, you lose
the portability that Java bytecodes provide. You’ll learn about each of these
mechanisms on Day 20, “Using Native Methods and

Libraries.”

Java Is Object Oriented

To some, the object-oriented programming (OOP) technique is merely a way of
organizing programs, and it can be accomplished using any language. Working with
a real object-oriented language and

programming environment, however, enables you to take full advantage of
object-oriented methodology and its capabilities for creating flexible, modular
programs and reusing code.

Many of Java’s object-oriented concepts are inherited from C++, the language on
which it is based, but it borrows many concepts from other object-oriented
languages as well. Like most object-oriented

programming languages, Java includes a set of class libraries that provide basic
data types, system input and output capabilities, and other utility functions.
These basic libraries are part of the standard Java

environment, which also includes simple libraries, form networking, common
Internet protocols, and user interface toolkit functions. Because these class
libraries are written in Java, they are portable across

platforms as all Java applications are.

You’ll learn more about object-oriented programming and Java tomorrow.

Java Is Easy to Learn

In addition to its portability and object orientation, one of Java’s initial
design goals was to be small and simple, and therefore easier to write, easier
to compile, easier to debug, and, best of all, easy to learn.

Keeping the language small also makes it more robust because there are fewer
chances for programmers to make mistakes that are difficult to fix. Despite its
size and simple design, however, Java still has a

great deal of power and flexibility.

Java is modeled after C and C++, and much of the syntax and object-oriented
structure is borrowed from the latter. If you are familiar with C++, learning
Java will be particularly easy for you because you have

most of the foundation already. (In fact, you may find yourself skipping through
the first week of this book fairly rapidly. Go ahead; I won’t mind.)Although
Java looks similar to C and C++, most of the more complex parts of those
languages have been

excluded from Java, making the language simpler without sacrificing much of its
power. There are no pointers in Java, nor is there pointer arithmetic. Strings
and arrays are real objects in Java. Memory

management is automatic. To an experienced programmer, these omissions may be
difficult to get used to, but to beginners or programmers who have worked in
other languages, they make the Java language

far easier to learn.

However, while Java’s design makes it easier to learn than other programming
languages, working with a programming language is still a great deal more
complicated than, say, working in HTML. If you have

no programming language background at all, you may find Java difficult to
understand and to grasp. But don’t be discouraged! Learning programming is a
valuable skill for the Web and for computers in

general, and Java is a terrific language to start out with.

Getting Started Programming in JavaEnough background! For the second half of
this day let’s actually dive into simple Java programming and

create two Java programs: a standalone Java application and an applet that you
can view in a Java-enabled browser. Although both these programs are extremely
simple, they will give you an idea of

what a Java program looks like and how to compile and run it.

Getting a Java Development Environment In order to write Java programs, you
will, of course, need a Java development environment. (Although

browsers such as Netscape allow you to play Java applets, they don’t let you
write them. For that you’ll need a separate tool.) Sun’s JDK, which is available
for downloading at the JavaSoft Web site

(http://www.javasoft.com/) and included on the CD for this book, will do just
fine. It runs on Solaris, Windows 95 and NT, and Macintosh. However, despite the
JDK’s popularity, it is not the easiest

development tool to use. If you’re used to using a graphical user
interface-based development tool with an integrated editor and debugger, you’ll
most likely find the JDK’s command-line interfaces rather

primitive. Fortunately, the JDK is not the only tool in town.

As mentioned earlier, a number of third-party development environments (called
integrated development environments, or IDEs) are also available for developing
in Java. These include Sun’s Java Workshop for

Solaris, Windows NT and Windows 95 (you can get more information about it athttp://www.sun.com/developer-products/java/);
Symantec’s Café for Windows 95,

Windows NT, and Macintosh (http://cafe.symantec.com/); Microsoft’s Visual J++
forWindows 95 and Windows NT (http://www.microsoft.com/visualj/); and Natural

Intelligence’s Roaster(http://www.natural.com/pages/products/roaster/index.html).
All three are

commercial programs, but you might be able to download trial or limited versions
of these programs to

try them out. You’ll learn more about the features and capabilities of the
various Java IDEs on Day 22,

“Java Programming Tools.”

Note

I find the graphical development environments far easier to use than

the standard JDK. If you have the money and the time to invest in one

of these tools, I highly recommend you do so. It’ll make your Java

development experience much more pleasant.

Installing the JDK and Sample Files

Sun’s JDK for Solaris, Windows, and Macintosh is included as part of the CD-ROM
that comes with this

book. Also on the CD-ROM are all of the code examples from this book-a great
help if you don’t want to

type them all in again. To install either the JDK or the sample files (or both),
use one of the following

procedures:

Note

If you don’t have access to a CD-ROM drive, you can also get accessto these
files over the World Wide Web. You can download the JDK

itself from http://java.sun.com/products/JDK/1.0.2/and install it per the
instructions on those pages. The sample files

from this book are available on the Web site for this book:http://www.lne.com/Web/JavaProf/.

If you download the JDK and source files, as opposed to getting themoff the
CD-ROM, make sure you read the section “Configuring the

JDK” to make sure everything is set up right.Windows Sun’s JDK runs on Windows
95 and Windows NT. It does not run on

Windows 3.x.

To install the JDK or the sample files on Windows, run the Setup program on the
CD-ROM

(double-clicking the CD icon will do this automatically). By default, the
package will be installed into

C:\Java; you can install it anywhere on your hard disk that you’d like. You’ll
be given options to install

the JDK, the sample files, and various other extra files; choose the options you
want and those files will

be installed.

If you’ve installed the JDK, note that in the directory JDK\lib there is a file
called classes.zip. Do

not unzip this file; it needs to remain in zip form for it to work correctly.
The file JDK\src.zip

contains the source code for many of the JDK libraries; you can unzip this one
if you like. Make sure if

you do that you have a zip program that supports long filenames, or it will not
work correctly!

Macintosh

Sun’s JDK for Macintosh runs on System 7 (MacOS) for 68KB or

Power Mac.

To install the JDK or the sample files on the Macintosh, double-click the
installation program on the

CD-ROM. By default, the package will be installed into the folder Java on your
hard disk; you can

install it anywhere on your disk that you’d like. You’ll be given options to
install the JDK, the sample

files, and various other extra files; choose the options you want and those
files will be installed.

Solaris

Sun’s JDK for Solaris runs on Solaris 2.3, 2.4, and 2.5, as well as the

x86 version of Solaris.

The CD-ROM for this book contains the tarred and zipped JDK in the directory

jdk/solaris/jdk1.02.tgz. Using the utilities gunzip and tar, you can extract the
contents of

that file anywhere on the file system you would like. For example, if you copy
the .tgz file to your

home directory and use the following commands to extract it, you’ll end up with
a java directory that

contains the full JDK:

gunzip ./jdk1.02.tgz

tar xvf ./jdk1.02.tar

Note that in the directory java\lib there is a file called classes.zip. Do not
unzip this file; it

needs to remain in zip form for it to work correctly. The file java\src.zip
contains the source code

for many of the JDK libraries; you can unzip this one if you’re interested in
the source code.

The sample files are also contained on the CD-ROM in authors/authors.tar. Create
a directory

where the sample files will live (for example, a directory called javasamples in
your home directory),

copy the authors.tar file there, and then use the tar command to extract it,
like this:

mkdir ~/javasamples

cp /cdrom/authors/authors.tar

tar xvf authors.tar

Configuring the JDK

If you’ve installed the JDK using the setup programs from the CD-ROM, chances
are good that it has

been correctly configured for you. However, because most common problems with
Java result from

configuration errors, I recommend that you double-check your configuration to
make sure everything is

right. And if you’ve installed the JDK from a source other than the CD-ROM,
you’ll definitely want to

read this section to make sure you’re all set up.

Windows

The JDK needs two important modifications to yourautoexec.bat file in order to
work correctly: The JDK\bin

directory must be in your execution path, and you must have the CLASSPATH
variable set up.

Edit your autoexec.bat file using your favorite editor (Notepad will do just
fine). Look for a line

that looks something like this:

PATH C:\WINDOWS;C:\WINDOWS\COMMAND;C:\DOS; …

Somewhere in that line you should see an entry for the JDK; if you installed the
JDK from CD-ROM, it’ll

look something like this (the dots are there to indicate that there may be other
stuff on this line):

PATH C:\WINDOWS; … C:\TEAchY~1\JDK\BIN; …

If you cannot find any reference to JDK\BIN or JAVA\BIN in your PATH, you’ll
need to add it. Simply

include the full pathname to your JDK installation to the end of that line,
starting with C: and ending

with BIN; for example, C:\JAVA\BIN or C:\Java\JDK\BIN.

Note

The directories Teach Yourself Java and TEAchY~1 are

actually the same thing; the former is how the directory appears in

Windows 95, and the latter is how it appears in DOS. Either one will

work fine; there’s no need to change it if one or the other appears.

Note, however, that if the pathname contains spaces, it must be in

quotes.

The second thing you’ll need to add to the autoexec.bat file (if it isn’t
already there) is a

CLASSPATH variable. Look for a line that looks something like this:

SET CLASSPATH=C:\TEAchY~1\JDK\lib\classes.zip;.;The CLASSPATH variable may also
have other entries in it for Netscape or Internet Explorer, but the one

you’re most interested in is a reference to the classes.zip file in the JDK, and
to the current

directory (.). If your autoexec.bat file does not include either of these
locations, add a line to the

file that contains both these things (the line shown above will work just fine).

After saving your autoexec.bat file, you’ll need to restart Windows for the
changes to take effect.

Macintosh

The JDK for Macintosh should need no further configuration after

installation.

Solaris

To configure the JDK for Solaris, all you need to do is add the

java/bin or jdk/bin directory to your execution path. Usually a

line something like this in your .cshrc, .login, or .profile

files will work:

set path= (~/java/bin/ $path)

This line assumes that you’ve installed the JDK (as the directory java) into
your home directory; if

you’ve installed it somewhere else, you’ll want to substitute that pathname.

Make sure you use the source command with the name of the appropriate file to
make sure the changes

take effect (or log out and log back in again):

source ~/.login

Creating a Java Application

Now let’s actually get to work. We’ll start by creating a simple Java
application: the classic Hello World

example that many programming language books use to begin.

Java applications are different from Java applets. Applets, as you have learned,
are Java programs that

are downloaded over the World Wide Web and executed by a Web browser on the
reader’s machine.

Applets depend on a Java-enabled browser in order to run.

New Term

Java applications, however, are more general programs written in the

Java language. Java applications don’t require a browser to run; in

fact, Java can be used to create all the kinds of applications that you

would normally use a more conventional programming language tocreate.

Java applications are standalone Java programs that do not require a Web browser
to run. Java

applications are more general-purpose programs such as you’d find on any
computer.

A single Java program can be an applet or an application, or both, depending on
how you write that

program and the capabilities that program uses. Throughout this first week as
you learn the Java

language, you’ll be writing mostly applications; then you’ll apply what you’ve
learned to write applets in

Week 2. If you’re eager to get started with applets, be patient. Everything that
you learn while you’re

creating simple Java applications will apply to creating applets, and it’s
easier to start with the basics

before moving onto the hard stuff. You’ll be creating plenty of applets in Week
2.Creating the Source File

As with all programming languages, your Java source files are created in a plain
text editor, or in an

editor that can save files in plain ASCII without any formatting characters. On
UNIX, emacs, pico,

and vi will work; on Windows, Notepad or DOS Edit are both text editors that
will work (although I

prefer to use the shareware TextPad). On the Macintosh, SimpleText (which came
with your Mac) or the

shareware BBedit will work. If you’re using a development environment like Café
or Roaster, it’ll have

its own built-in text editor you can use.

Note

If you’re using Windows to do your Java development, you may have

to make sure Windows understands the .java file extension before

you start; otherwise, your text editor may insist on giving all your

files a .txt extension. The easiest way to do this is to go to any

Windows Explorer window, choose View|Options|File Types, choose

New Type, and add Java Source File and .java to the

Description of Type and Associated Extension boxes, respectively.

Fire up your editor of choice and enter the Java program shown in Listing 1.1.
Type this program, as

shown, in your text editor. Be careful that all the parentheses, braces, and
quotes are there, and that

you’ve used all the correct upper- and lowercase letters.

Note

You can also find the code for these examples on the CD-ROM as

part of the sample code. However, it’s a good idea to actually type

these first few short examples in so that you get a feel for what Java

code actually looks like.

Listing 1.1. Your first Java application.

1: class HelloWorld {

2: public static void main (String args[]) {

3: System.out.println(“Hello World!”);

4: }

5: }

Warning

The number before each line is part of the listing and not part of the

program; the numbers are there so I can refer to specific line numbers

when I explain what’s going on in the program. Do not include them

in your own file.

After you’ve finished typing in the program, save the file somewhere on your
disk with the name

HelloWorld.java. This is very important. Java source files must have the same
name as the class

they define (including the same upper- and lowercase letters), and they must
have the extension .java.

Here, the class definition has the name HelloWorld, so the filename must be
HelloWorld.java. If

you name your file something else (even something like helloworld.java or

Helloworld.java), you won’t be able to compile it. Make absolutely certain the
name isHelloWorld.java.

You can save your Java files anywhere you like on your disk, but I like to have
a central directory or

folder to keep them all in. For the examples in this chapter, I’ve put my files
into a directory called

TYJtests (short for Teach Yourself Java Tests).

Compiling and Running the Source File

Now it’s time to compile the file. If you’re using the JDK, you can use the
instructions for your computer

system contained in the next few pages. If you’re using a graphical development
environment, there will

most likely be a button or option to compile the file (check with the
documentation that came with your

program).

Windows

To compile the Java source file, you’ll use the command-line Java

compiler that comes with the JDK. To run the compiler, you’ll need to

first start up a DOS shell. In Windows 95, the DOS shell is under the

Programs menu (it’s called MS-DOS Prompt).

From inside DOS, change directories to the l

ocation where you’ve saved your HelloWorld.java file.

I put mine into the directory TYJtests, so to change directories I’d use this
command:

CD C:\TYJtests

Once you’ve changed to the right directory, use the javac command as follows,
with the name of the

file as you saved it in Windows (javac stands for Java compiler). Note that you
have to make sure you

type all the same upper- and lowercase here as well:

javac HelloWorld.java

Note

The reason that I’ve emphasized using the original filename is that

once you’re inside the DOS shell, you might notice that your nice

long filenames have been truncated to old-style 8.3 names and that, in

fact, HelloWorld.java actually shows up as HELLOW~1.jav.

Don’t panic; this is simply a side effect of Windows 95 and how it

manages long filenames. Ignore the fact that the file appears to be

HELLOW~1.jav and just use the filename you originally used when you saved the
file.

Figure 1.4 shows what I’ve done in the DOS shell so you can make sure you’re
following along.

Figure 1.4 : Compiling Java in the DOS shell.

If all goes well, you’ll end up with a file called HelloWorld.class (or at least
that’s what it’ll be

called if you look at it outside the DOS shell; from inside DOS its called
HELLOW~1.cla). That’s your

Java bytecode file. If you get any errors, go back to your original source file
and make sure you typed it

exactly as it appears in Listing 1.1 with the same upper- and lowercase. Also
make sure the filename has

exactly the same upper- and lowercase as the name of the class (that is, both
should be HelloWorld).

Once you have a class file, you can run that file using the Java bytecode
interpreter. The Java interpreter

is called simply java, and you run it from the DOS shell as you did javac. Run
your Hello World

program like this from the command line, with all the same upper- and lowercase
(and note that the

argument to the java program does not have a .class extension):

java HelloWorld

If your program was typed and compiled correctly, you should get the phrase
Hello World! printed

to your screen as a response. Figure 1.5 shows how I did it.

Figure 1.5 : Running Java applications in the DOS shell.

Note

Remember, the Java compiler and the Java interpreter are different

things. You use the Java compiler (javac) for your Java source files

to create .class files, and you use the Java interpreter (java) to

actually run your class files.

Macintosh

The JDK for the Mac comes with an application called Java

Compiler. To compile your Java source file, simply drag and drop it

on top of the Java Compiler icon. The program will compile your

Java file and, if there are no errors, create a file called

HelloWorld.class in the same folder as your original source

file.

Tip

Putting an alias for Java Compiler on the desktop makes it easy to

drag and drop Java source files.

If you get any errors, go back to your original source file and make sure you
typed it exactly as it appears

in Listing 1.1, with the same upper- and lowercase. Also make sure the filename
has exactly the same

upper- and lowercase as the name of the class (that is, both should be
HelloWorld).

Once you’ve successfully generated a HelloWorld.class file, simply double-click
it to run it. The

application Java Runner, part of the Mac JDK, will start, and the program will
ask you for command-line

arguments. Leave that screen blank and click OK. A window labeled stdout will
appear with the

message Hello World!. Figure 1.6 shows that window.

Figure 1.6 : Running Java applications on the Mac using Java Runner.

That’s it! Keep in mind as you work that you use the Java Compiler application
to compile your .java

files into .class files, which you can then run using Java Runner.

To compile the Java source file in Solaris, you’ll use the command-line Java
compiler that comes with the

JDK. From a UNIX command line, cd to the directory that contains your Java
source file. I put mine in

the directory TYJtests, so to change directories I’d use this command:

cd ~/TYJtests

Once you’re in the right directory, use the javac command with the name of the
file, like this:

javac HelloWorld.java

If all goes well, you’ll end up with a file called HelloWorld.class in the same
directory as your

source file. That’s your Java bytecode file. If you get any errors, go back to
your original source file and

make sure you typed it exactly as it appears in Listing 1.1, with the same
upper- and lowercase letters.

Also make sure the filename has exactly the same upper- and lowercase letters as
the name of the class

(that is, both should be HelloWorld).

Once you have a class file, you can run that file using the Java bytecode
interpreter. The Java interpreter

is called simply java, and you run it from the command line as you did javac,
like this (and note that

the argument to the java program does not have a .class extension):

java HelloWorld

If your program was typed and compiled correctly, you should get the phrase
Hello World! printed

to your screen as a response. Figure 1.7 shows a listing of all the commands I
used to get to this point

(the part with [desire]~[1] is my system prompt).

Figure 1.7 : Compiling and running a Java application on Solaris.

Note

Remember that the Java compiler and the Java interpreter are

different things. You use the Java compiler (javac) for your Java

source files to create .class files, and you use the Java interpreter

(java) to actually run your class files.

Creating a Java Applet

Creating applets is different from creating a simple application. Java applets
run and are displayed inside

a Web page with other page elements, and therefore have special rules for how
they behave. Because of

these special rules for applets, creating an applet may in many cases be more
complex than creating an

application.

For example, to create a simple Hello World applet, instead of merely being able
to print a message as a

set of characters, you have to make space for your message on the Web pages and
then use special font

and graphics operations to paint the message to the screen.

Note

Actually, you can run a plain Java application as an applet, but the

Hello World message will print to a special window or to a log

file, depending on how the browser has its output set up. You’ll learn

more about this next week.

Creating the Source File

In this example, you’ll create a simple Hello World applet, place it inside a
Web page, and view the

result. As with the Hello World application, you’ll first create the source file
in a plain text editor. Listing

1.2 shows the code for the example.

Listing 1.2. The Hello World applet.

1: import java.awt.Graphics;

2:

3: public class HelloWorldApplet extends java.applet.Applet {

4:

5: public void paint(Graphics g) {

6: g.drawString(“Hello world!”, 5, 25);

7: }

8:}

Save that file just as you did the Hello World application, with the filename
exactly the same as the name

of the class. In this case the class name is HelloWorldApplet, so the filename
you save it to would

be HelloWorldApplet.java. As with the application, I put the file in a directory
called TYJch01,

but you can save it anywhere you like.

Compiling the Source File

The next step is to compile the Java applet file. Despite the fact that this is
an applet, you compile the file

exactly the same way you did the Java application, using one of the following
procedures:

javac HelloWorldApplet.java

javac HelloWorldApplet.java

Windows

From inside a DOS shell, cd to the directory containing your applet

source file, and use the javac command to compile it (watch those

upper- and lowercase letters):

Macintosh

Drag and drop the HelloWorldApplet.java file onto the Java

Compiler icon.

Salaris

From a command line, cd to the directory containing your applet

source file and use the javac command to compile it:

Including the Applet in a Web Page

If you’ve typed the file correctly, you should end up with a file called
HelloWorldApplet.class in

the same directory as your source file. That’s your Java applet file; to have
the applet run inside a Web

page you must refer to that class file inside the HTML code for that page using
the <APPLET> tag.

Listing 1.3 shows a simple HTML file you can use.

Listing 1.3. The HTML with the applet in it.

1: <HTML>

2: <HEAD>

3: <TITLE>Hello to Everyone!</TITLE>

4: </HEAD><BODY>

5: <P>My Java applet says:

6: <APPLET CODE=”HelloWorldApplet.class” WIDTH=150 HEIGHT=25>

7: </APPLET>

8: </BODY>

9: </HTML>

You’ll learn more about <APPLET> later in this book, but here are two things to
note about it:

Use the CODE attribute to indicate the name of the class that contains your
applet, here

HelloWorldApplet.Class.

Use the WIDTH and HEIGHT attributes to indicate the size of the applet on the
page. The browser

uses these values to know how big a chunk of space to leave for the applet on
the page. Here, a

box 150 pixels wide and 25 pixels high is created.

Save the HTML file in the same directory as your class file, with a descriptive
name and an .html

extension (for example, you might name your HTML file the same name as your

applet-HelloWorldApplet.html).

Note

As mentioned earlier with the Java source files, your text editor may

insist on naming your HTML files with a .txt extension if Windows

does not understand what the .html extension is used for. Select

View|Options|File Types from any Windows Explorer window to add

a new file type for HTML files to solve this problem.

Now you’re ready for the final test-actually viewing the result of running your
applet. To view the applet,

you need one of the following:

A browser that supports Java applets, such as Netscape 2.0 or Internet Explorer
3.0. If you’re

running on the Macintosh, you’ll need Netscape 3.0 or later. If you’re running
on Windows 95 or

NT, you’ll need the 32-bit version of Netscape. And if you’re using Internet
Explorer, you’ll need

the 3.0 beta 5 or later (the final version will do just fine).

The appletviewer application, which is part of the JDK. The appletviewer is not
a Web

browser and won’t let you to see the entire Web page, but it’s acceptable for
testing to see how an

applet will look and behave if there is nothing else available.

An applet viewer or runner tool that comes with your development environment.

If you’re using a Java-enabled browser such as Netscape to view your applet
files, you can use the Open

File… item under the File menu to navigate to the HTML file containing the
applet (make sure you open

the HTML file and not the class file). In Internet Explorer, select File|Open
and then Browse to find the

file on your disk. You don’t need to install anything on a Web server yet; all
this works on your local

system. Note that the Java applet may take a while to start up after the page
appears to be done loading;

be patient. Figure 1.8 shows the result of running the applet in Netscape.

Figure 1.8 : The applet running in Netscape.

If you don’t have a Web browser with Java capabilities built into it, you can
use the JDK’s

appletviewer program to view your Java applet.

appletviewer HTML/HelloWorldApplet.html

Windows or Solaris

To run the appletviewer in Windows or Solaris versions of the JDK, cd to the
directory where your HTML and class files are contained and use the appletviewer
command with the name ofthe HTML file you just created:

The appletviewer will show you only the applet itself, not the HTML text around
the applet.

Although the appletviewer is a good way to do simple tests of Java applets, it’s
a better idea to get a

Java-enabled browser so that you can see your applet on its page in its full
glory.

Troubleshooting

If you’ve run into any problems with the previous examples, this section can
help. Here are some of the

most common problems and how to fix them:

Bad command or filename or Command not found

These errors result when you do not have the JDK’s bin directory in your
execution path, or the

path to that directory is wrong. On Windows, double-check your autoexec.bat
file; on UNIX,

check the system file with your path commands in it (.cshrc, .login, .profile,
or some

similar file).

javac: invalid argument

Make sure the name of the file you’re giving to the javac command is exactly the
same name as

the file. In particular, in the DOS shell you want to use the Windows filename
with a .java

extension, not the DOS equivalent (HELLOW~1.jav, for example).

Warning: public class HelloWorldApplet must be defined in a file

called HelloWorldApplet.java

This error most often happens if there is a mismatch between the name of the
class as defined in

the Java file itself (the name following the word class) and the name of the
java source file. Both

the filenames must match, including upper- and lowercase letters (this
particular error implies that

the filename had lowercase letters). Rename either the filename or the class
name, and this error

will go away.

Insufficient-memory errors

The JDK is not the most efficient user of memory. If you’re getting errors about
memory, consider

closing larger programs before running Java compiles, turn on virtual memory, or
install more RAM.

Other code errors

If you’re unable to compile the Java source files because of other errors I
haven’t mentioned here,

be sure that you’ve typed them in exactly as they appear, including all upper-
and lowercase letters.

Java is case sensitive, meaning that upper- and lowercase letters are treated
differently, so you will

need to make sure that everything is capitalized correctly. If all else fails,
try comparing your

source files to the files on the CD-ROM.

Summary

Today you’ve gotten a basic introduction to the Java language and its goals and
features. Java is a

programming language, similar to C or C++, in which you can develop a wide range
of programs. The

most common use of Java at the moment is in creating applets for HotJava, an
advanced World Wide

Web browser also written in Java. Applets are Java programs that are downloaded
and run as part of a

Web page. Applets can create animation, games, interactive programs, and other
multimedia effects on

Web pages.

Java’s strengths lie in its portability-both at the source and at the binary
level, in its object-oriented

design-and in its simplicity. Each of these features helps make applets
possible, but they also make Java

an excellent language for writing more general-purpose programs that do not
require a Java-enabled

browser to run. These general-purpose Java programs are called applications.

To end this day, you experimented with an example of an applet and an example of
an application,

getting a feel for the differences between the two and how to create, compile,
and run Java programs-or,

in the case of applets, how to include them in Web pages. From here, you now
have the foundation to

create more complex applications and applets. Onward to Day 2, “Object-Oriented
Programming and

Java”!

Q&A

Q: I know a lot about HTML, but not much about computer programming. Can I still
write

Java programs?

A: If you have no programming experience whatsoever, you most likely will find
programming

Java significantly more difficult than HTML. However, Java is an excellent
language to learn

programming with, and if you patiently work through the examples and the
exercises in this

book, you should be able to learn enough to get started with Java.

Q: What’s the relationship between JavaScript and Java?

A: They have the same first four letters.

A common misconception in the Web world today is that Java and JavaScript have
more in

common than they actually do. Java is the general-purpose programming language
that you’ll

learn about in this book; you use it to create applets. JavaScript is a
Netscape-invented scripting

language that looks sort of like Java; with it you can do various nifty things
in Web pages. They

are independent languages, used for different purposes. If you’re interested in
JavaScript

programming, you’ll want to pick up another book, such as Teach Yourself
JavaScript in a Week

or Laura Lemay’s Web Workshop: JavaScript, both also available from Sams.net
Publishing.

Q: According to today’s lesson, Java applets are downloaded via a Java-enabled
browser such

as Netscape and run on the reader’s system. Isn’t that an enormous security
hole? What

stops someone from writing an applet that compromises the security of my
system-or

worse, that damages my system?

A: Sun’s Java team has thought a great deal about the security of applets within
Java-enabled

browsers and has implemented several checks to make sure applets cannot do nasty
things:

Java applets cannot read or write to the disk on the local system.

Java applets cannot execute any programs on the local system.

Java applets cannot connect to any machines on the Web except for the server
from which

they are originally downloaded.

Note that some of these restrictions may be allowed in some browsers or may be
turned on in the

browser configuration. However, you cannot expect any of these capabilities to
be available.

In addition, the Java compiler and interpreter check both the Java source code
and the Java

bytecodes to make sure that the Java programmer has not tried any sneaky tricks
(for example,

overrunning buffers or stack frames).

These checks obviously cannot stop every potential security hole (no system can
promise that!),

but they can significantly reduce the potential for hostile applets. You’ll
learn more about

security issues for applets on Day 8, “Java Applet Basics,” and in greater
detail on Day 21,

“Under the Hood.”

Q: I followed all the directions you gave for creating a Java applet. I loaded
it into HotJava,but Hello World didn’t show up. What did I do wrong?

A: Don’t use HotJava to view applets you’ve created in this book; get a more
up-to-date browsersuch as Netscape or Internet Explorer. HotJava was an
experimental browser and has not beenupdated since soon after its original
release. The steps you take to define and write an applethave changed since
then, and the applets you write now will not run on HotJava.

Q: You’ve mentioned Solaris, Windows, and Macintosh in this chapter. What about
otheroperating systems?

A: If you use a flavor of UNIX other than Solaris, chances are good that the JDK
has been ported to your system. Here are some examples:

SGI’s version of the JDK can be found at

http://www.sgi.com/Products/cosmo/cosmo_instructions.html.

Information about Java for Linux can be found at

http://www.blackdown.org/java-linux/.

IBM has ported the JDK to OS/2 and AIX. Find out more from

http://www.ncc.hurley.ibm.com/javainfo/.

OSF is porting the JDK to HP/UX, Unixware, Sony NEWS, and Digital UNIX. See

http://www.osf.org/mall/web/javaport.htm.

(Thanks to Elliote Rusty Harold’s Java FAQ at
http://www.sunsite.unc.edu/javafaq/javafaq/html for this information.)

Q: Why doesn’t Java run on Windows 3.1?

A: Technical limitations in Windows 3.1 make porting Java to Windows 3.1
particularly difficult.Rumor has it that both IBM and Microsoft are working on
ports, but no real information is forthcoming.

Q: I’m using Notepad on Windows to edit my Java files. The program insists on
adding a.txt extension to all my files, regardless of what I name them (so I
always end up with files like HelloWorld.java.txt). Short of renaming them
before I compile them, what else can I do to fix this?

A: Although you can rename the files just before you compile them, that can get
to be a pain, particularly when you have a lot of files. The problem here is
that Windows doesn’t understand the .java extension (you may also have this
problem with HTML’s .html extension as well). To fix this, go into any Windows
Explorer window and select View|Options|File Types. Fromthat panel, select New
Type. Enter Java Source Files in the Description of Type box and .java into the
Associated Extension box. Then click OK. Do the same with HTML files if you need
to, and click OK again. You should now be able to use Notepad (or any other text
editor) to create and save Java and HTML files.

Q: Where can I learn more about Java and find applets and applications to play
with?

A: You can read the rest of this book! Here are some other places to look for
Java information and Java applets:

The Java home page at http://www.java.sun.com/ is the official source for Java
information, including information about the JDK, about the upcoming 1.1
release, and about developer tools such as the Java Workshop, as well as
extensive documentation.

Gamelan, at http://www.gamelan.com/, is a repository of applets and Java

information, organized into categories. If you want to play with applets or
applications, this is the place to look.

For Java discussion, check out the comp.lang.java newsgroups, including

comp.lang.java.programmer, comp.lang.java.tech,

comp.lang.java.advocacy, and so on. (You’ll need a Usenet newsreader to access

January 4, 2008 Posted by | Java Help | 8 Comments

C Language Help, C Language Tutorials, C Language Programming, C Language Tricks { The C# Language }

C Language Help, C Language Tutorials, C Language Programming, C Language Tricks
XML Documentation

XML is used extensively to document program code. Apart from its status of a
mere acronym for eXtensible Markup Language, XML has a bounden duty to achieve
miracles. However, in this chapter, we would be utilizing XML for the temporal
task of documenting our code.

a.cs

class zzz

{

public static void Main() {

}

}

Run the compiler as

>csc a.cs /doc:a.xml

a.xml

<?xml version=”1.0″?>

<doc>

<assembly>

<name>a</name>

</assembly>

<members>

</members>

</doc>

This is the smallest possible XML file that can be effectively generated by the
compiler. The compiler with the /doc option, followed by a colon and filename,
creates the XML file, which documents the C# program.

Anything placed within the <> brackets is called a tag. At the expense of
sounding tediously repetitive, we once again reiterate that, each and every XML
file must necessarily start with a tag beginning with <?xml>. This has to be
followed by an attribute called version. Presently, the only permissible value
that can be assigned to version is 1.0. There may never be a version 1.1 or 2.0.

Subsequent to this, a root tag called ‘doc’ is specified, which envelopes all
other tags within the file. We have a tag called ‘assembly’ which owns another
tag called ‘name’ that encloses our assembly name ‘a’. The name of our program
file is ‘a’. There are no members to be documented, therefore, no names are
noticeable within the member tags. This is the basic structure of our XML file.

It is the bare minimum, no frills structure, created by the compiler. Now, let
us refurbish and garnish it through our own contribution.

a.cs

class zzz

{

/// <vijay>

public static void Main()

{

}

}

We have added an XML tag in the .cs file. The documentation clearly states that
an XML tag in a .cs file cannot begin with two slashes, since they are reserved
for comments. It should instead have three slashes. The use of the two slashes
generates the following warning:

Output

a.cs(4,3): warning CS1570: XML comment on ‘zzz.Main()’ has badly formed XML –
‘End tag ‘member’ does not match the start tag ‘vijay’.’

From now on, we shall only display the pertinent portions of the XML file, while
ignoring the portion that is invariant, such as, the doc and the /doc tags. So
in the file created as:

a.xml

<?xml version=”1.0″?>

<doc>

<assembly>

<name>a</name>

</assembly>

<members>

<!– Badly formed XML comment ignored for member “M:zzz.Main” –>

</members>

</doc>

we will display only

xml fragment:

<members>

<!– Badly formed XML comment ignored for member “M:zzz.Main” –>

</members>

In the next program, a closing tag of ‘vijay’ is specified.

a.cs

class zzz

{

/// <vijay>

/// Function Main gets called first

/// </vijay>

public static void Main()

{

}

}

xml

<members>

<member name=”M:zzz.Main”>

<vijay>

Function Main gets called first

</vijay>

</member>

</members>

The earlier program generated an error, since there was no corresponding end tag
to the tag ‘vijay’.

Every tag in XML must necessarily have a start tag and an end tag. We are at
liberty to choose any name for the tag. Therefore, we have chosen the name
‘vijay’.

The compiler creates a tag called ‘member’ under the tag members, and adds an
attribute called ‘name’. This attribute is initialized to the name of the C#
entity, which is the position where the tag is placed. The value assigned to
name is, as follows:

• The letter M,

• Followed by a colon separator,

• Followed by the name of the class zzz,

• Followed by a dot separator

• Finally, followed by the name of the function i.e. Main.

Thus, it displays M:zzz.Main.

The tag ‘vijay’ and the text enclosed between the start and end tags of ‘vijay’
subsequently, get enclosed within the members tag.

a.cs

/// <mukhi>

/// Our first class

/// </mukhi>

class zzz

{

/// <vijay>

/// Function Main gets called first

/// </vijay>

public static void Main()

{

}

}

xml

<members>

<member name=”T:zzz”>

<mukhi>

Our first class

</mukhi>

</member>

<member name=”M:zzz.Main”>

<vijay>

Function Main gets called first

</vijay>

</member>

</members>

Now, we have added a tag called ‘mukhi’ to the class. It is however, not added
to the function. So presently, there are two member tags, and each has been
assigned a name within the members tag. The value assigned to the member name,
which encloses ‘mukhi’ is, T:zzz. This is created in a manner akin to that of a
function, with the exception of the prefix M, which has now been replaced by T.

The rationale behind introducing XML tags is that, a program known as an XML
Parser can interpret the above XML file, and subsequently, create legible
output. But, the prime impediment that we have to confront and surmount is, the
reluctance of programmers to document code in the first place.

a.cs

class zzz

{

/// <mukhi>

/// Our Instance Variable

/// </mukhi>

public int i;

public static void Main()

{

/// <vijay>

/// Our first variable ii

/// </vijay>

int ii;

}

}

Output

a.cs(12,5): warning CS0168: The variable ‘ii’ is declared but never used

a.cs(9,1): warning CS1587: XML comment is not placed on a valid language element

a.cs(6,12): warning CS0649: Field ‘zzz.i’ is never assigned to, and will always
have its default value 0

xml

<members>

<member name=”F:zzz.i”>

<mukhi>

Our Instance Variable

</mukhi>

</member>

</members>

Oops! The tags that we give within a function have not been reflected in the XML
file. The instance variable i is flagged with an F, but the local variable ii is
not reflected in the XML file at all.

We have carried this out on purpose, with the intention of demonstrating that it
is not permissible to write XML tags within functions. Even though our preceding
action would not produce any error, the compiler shall, in any case, ignore our
work of art.

We will now dwell upon the names or string ids, which the compiler generates for
names of entities. They are as follows:

• N stands for a Namespace.

• T represents a type, class, interface, struct, enum and delegate.

• F denotes a field.

• P is indicative of either properties or indexers.

• M indicates all methods

• E represents an event.

• ! is indicative of an error.

a.cs

/// <vijay>

/// Our first variable ii

/// </vijay>

namespace aaa

{

class zzz

{

public static void Main()

{

}

}

}

Output

a.cs(1,1): warning CS1587: XML comment is not placed on a valid language element

We do not enjoy the license to place tags wherever we desire. Thus, a warning is
issued when a tag is placed on a namespace, although no error is generated. In
the earlier case too, a similar warning was issued, when we had placed the tags
in a function.

a.cs

class zzz

{

/// <vijay>

/// Our function <c>Main</c> is the starting point

/// </vijay>

public static void Main()

{

}

}

xml

<member name=”M:zzz.Main”>

<vijay>

Our function <c>Main</c> is the starting point

</vijay>

</member>

If we place the tag <c> and </c> around some text in a tag, the tag gets copied
verbatim, into the XML file. It is indicative of the fact that the text
represents some code. To mark multiple lines of code, the code tag may be used
instead. The example tag also works in a similar manner.

a.cs

/// <exception cref=”System.Exception”> Our exception class </exception>

class zzz

{

public static void Main()

{

}

}

xml

<member name=”T:zzz”>

<exception cref=”T:System.Exception”> Our exception class </exception>

</member>

The exception tag works as demonstrated above, with a single difference in that,
the attribute cref is inspected for accuracy. This attribute should represent a
valid C# entity, otherwise, the following warning is displayed:

/// <exception cref=”System.Exceptions”> Our exception class </exception>

Output

a.cs(2,22): warning CS1574: XML comment on ‘zzz’ has cref attribute
‘System.Exceptions’ that could not be found.

xml

<member name=”T:zzz”>

<exception cref=”!:System.Exceptions”> Our exception class </exception>

</member>

All the tags expounded by us so far, have a similar functionality. This surely
must have kindled your curiosity to discover the rationale behind employing so
many tags.

This is so, because each of these tags, documents different features of the
language. Let us assume that we yearn for a bulleted list in our final
documentation. We would have to use the following tags to accomplish it:

a.cs

/// <list type=”bullet”>

/// <item>

/// <description>One</description>

/// </item>

/// <item>

/// <description>Two</description>

/// </item>

/// </list>

class zzz

{

public static void Main() {

}

}

xml fragment

<member name=”T:zzz”>

<list type=”bullet”>

<item>

<description>One</description>

</item>

<item>

<description>Two</description>

</item>

</list>

</member>

The attribute type can also be assigned the values of bullet, number or table.

Bear in mind, that an external program will interpret the XML tags. Thus, the
designers of the language standardized tags for describing different things. If
the same tag had been utilized for documenting every feature, a single program
would have sufficed to document the entire C# code. We have employed tags such
as ‘vijay’ and ‘mukhi’ to document our classes. Chaos would reign supreme, if
everyone decided to use his or her own names. Appended below is a brief summary
of the various tags that can be employed in the XML file:

• The param tag is used to document the parameters to a function.

• The paramref tag is used to document a parameter name.

• The permission tag is used to document the access allowed to a member.

• The remarks tag is used to specify the overall information for a class.

• The return tag has been introduced to document the return values of functions.

• The seealso tag helps in implementing the ‘see also’ section at the bottom of
help sections.

• The summary tag documents all members of a type.

• The value tag documents all the properties.

December 26, 2007 Posted by | The C# Language | 2 Comments

Follow

Get every new post delivered to your Inbox.