1 some variations on date display 2 print html code what php prints will be placed into the file,...
TRANSCRIPT
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 writes 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. (That’s called a literal.)
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=“act_Tuesday.gif” width=“50px” /> or<img src=“act_Friday.gif” width=“50px” /> 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.
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.)
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.
9
Following the rules
print "<img src=\"act_";
print date('l');
print ".gif\" width=\"50px\" />";• 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
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.