Senin, 14 November 2011

PHP Fundamental Syntax & Semantics

Specifying the DOCTYPE
Problem
You want to create an HTML5 page.
Solution
Specify the HTML5 DOCTYPE at the very beginning of your page:
       <!DOCTYPE html>
         <html>
        <head>
           <title>HTML5, for Fun &amp; Profit</title>
       </head>
            <body>
            </body>
      </html>
Note that the DOCTYPE is not case-sensitive. Feel free to go CaMeL
cAsE with the characters..

Discussion
The Document Type Definition, or DOCTYPE, tells browsers and validators what version of HTML the page is written in. Previous versions of HTML specified the version number, such as the DOCTYPE for XHTML 1.0 Strict:
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.With HTML5, the version is dropped from the DOCTYPE.
This allows HTML5 to be backwards compatible in terms of syntax and, hopefully, makes the transition to
HTML5 easier.

Let’s say you have a site that is valid HTML 4.0, but you want to transition to HTML5. Start with just the DOCTYPE change, and you not only have a valid site, but also a valid HTML5 site.
Additionally, all browsers recognize the shortened DOCTYPE and render in strict standards mode. There are some elements that have changed between HTML4 and HTML5, so some now removed or deprecated parts of your HTML4 document.
For example, <center>) might not technically validate as HTML5.



See Also
The W3C Working Draft discussion on differences between HTML 4 and HTML5 includes DOCTYPE at http://www.w3.org/TR/html5-diff/#doctype.

Specifying the Character Set
Problem
You need to define the character encoding of your web page.
Solution
In your document head, add a meta declaration for the character set:
<meta charset="UTF-8" />
Discussion
Character encoding tells browsers and validators what set of characters to use when rendering web pages. If you do not declare the character set in your HTML, browsers first try to determine the character set from your server’s HTTP response headers (specifically, the “Content- Type” header).
The character set declared in the response headers is generally taken in preference over the character set specified in your document, and is thus the preferred method. However, if you cannot control what headers your server sends, declaring the character set in your HTML document is the next best option. If a character set is neither declared in the document nor in the response headers, the browser might choose one for you and it may be the wrong one for your site’s needs. This could not only cause issues with rendering, but also poses a security risk.
Several years ago, a cross-site scripting vulnerability was discovered at Google that demonstrates the importance of character encoding: http://shiflett.org/blog/2005/dec/googles-xss-vulnerability.In previous versions of HTML, the character set needed to be declared with additional attributes and values:
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
But, like the DOCTYPE, HTML5 only needs the minimum information required by
browsers. Again, this helps with backwards compatibility and makes it easier for authors
to implement.

Special Characters
In this recipe, the page was specified to Unicode (UTF-8) because it is a versatile encoding that covers most web builders’ needs. Sometimes, though, you need to include a character that is outside the encoding.
A great resource for character entities is at http://www.digitalmediami nute.com/reference/entity/. It includes the numeric, named and Unicode references for many of the more common characters allowed in HTML. For these characters, you specify them with a Numeric Character Reference (NCR) or as a named entity in order to help browsers render them correctly. If you wanted a copyright symbol, for example, you could include it in your HTML as an NCR:
         &#169;
     Or you could include it as a named entity:
        &copy;

Tidak ada komentar:

Posting Komentar