160 likes | 294 Views
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.
E N D
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.
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.
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.)
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.
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
Another approach is to place the PHP code right in the middle of an HTML tag.
“I can cause accidents, too” Missing semicolon
Do not become obsessed with the line you are told the error is on. Missing quote
Errors can occur at different stages. Error is not in PHP but in the HTML that was written. Look for mistake by viewing the source. Missing \”
The PHP was OK, but it did not write good HTML. Missing quote in HTML