some variations on date display

16
1 Some Variations on Date Display

Upload: bond

Post on 19-Jan-2016

28 views

Category:

Documents


0 download

DESCRIPTION

Some Variations on Date Display. Print HTML Code. What PHP prints will be placed into the file and then the file is sent to the web client to be rendered. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Some Variations on Date Display

1

Some Variations on Date Display

Page 2: Some Variations on Date Display

2

Print HTML Code

What PHP prints will be placed into the file and then the file is sent to the web client to be rendered.

Thus PHP can write anything that the web client will understand. In the above example, it is writing HTML. It write a bold tag <b>, then the date, then a closing bold tag </b>.

Note that if we want to print something as is (e.g. <b>), we put it in quotation marks.

Page 3: Some Variations on Date Display

3

Result of PHP writing HTML bold tags around the date.

Page 4: Some Variations on Date Display

4

Having PHP write HTML tags with attributes

• Next let us write the code that displays an image corresponding to the day of the week. The HTML we want written is

<img src=“TuesdayBanner.gif” width=“650”> or<img src=“FridayBanner.gif” width=“650”> etc.

• The PHP code date(‘l’) will take care of the day of the week. (That’s a small ell.)

• The real problem is that the PHP code requires quotation marks and the desired HTML code also has quotation marks and we need to keep these distinct.

Page 5: Some Variations on Date Display

5

Escape sequences

• To distinguish HTML quotes from PHP quotes, we use what is called an escape sequence. The HTML quotes are preceded by a back slash (\”).

• The back slash now also becomes special and so if we want a back slash we have to write \\.

• (For other escape sequences, see column 1 of the PHP 4 Reference Card (Gould) under special characters.)

Page 6: Some Variations on Date Display

6

Rules of thumb for quotes

• In an escape sequence, the backslash always precedes the quote. – (Some people want to reverse the order for a closing

quote, but that’s wrong.)

• The overall number of quotes must be even. • The overall number of \” must also be even. • Occurrences of \” must always be inside regular

quotes.

Page 7: Some Variations on Date Display

7

PHP writing a image tag

Page 8: Some Variations on Date Display

8

The result of PHP writing a image tag

Page 9: Some Variations on Date Display

9

Following the rules

print "<img src=\"";

print date('l');

print "Banner.gif\" width=\"650\">";

• Note in the above example– The back slashes are always before the quotes– The overall number of quotes is 8 (which is even)– The overall number of \” is 4 (which is even)– The \” are always inside pairs of regular quotes

Page 10: Some Variations on Date Display

10

Another approach is to place the PHP code right in the middle of an HTML tag.

Page 11: Some Variations on Date Display

11

The other approach gives the same result.

Page 12: Some Variations on Date Display

12

PHP can also write JavaScript

Page 13: Some Variations on Date Display

13

“I can cause accidents, too”

Missing semicolon

Page 14: Some Variations on Date Display

14

Do not become obsessed with the line you are told the

error is on.

Missing quote

Page 15: Some Variations on Date Display

15

Errors can occur at different stages.

Missing \”

Error is not in PHP but in the HTML that was written.

Look for mistake by viewing the source.

Page 16: Some Variations on Date Display

16

The PHP was OK, but it did not write good HTML.

Missing quote in HTML