browse archive

18 January 2010

!doctype

!doctype

The doctype is always the first thing to appear on any HTML document, even before the HTML tag. It isn’t really a tag and has no effect on what the user sees, but it tells the browser what version of HTML (or markup) the page is written in.

The doctype points to a DTD file, this file is called a Document Type Definition. The DTD sets the rules for the markup version so the browsers can render the content correctly.

Example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Available doctypes
This article on w3schools shows all modern doctypes to use.

http://www.w3schools.com/tags/tag_DOCTYPE.asp

13 January 2010

<!– –> HTML Comments

HTML Comments are used throughout an HTML document to explain something. Anything between the open and closing tags are completely ignored by the browser, except in special cases. Use comments to explain what a certain piece of code does, or file information. No one can see these notes you make, unless the user views the source of the page. Most people delete all the comment they make when they have done coding, this makes the file size much smaller depending on how many comments you use.

Examples
Single line

Multi-line / Block

Sometimes it is used to talk to certain browsers, this came about because people wanted to use different style sheets for different browsers, because Internet Explorer generally mucks everything up…

Example

Comments can be placed anywhere in your HTML document.

6 January 2010

Project Euler puzzle 17, How many letters would be needed to write all the numbers in words from 1 to 1000?

How many letters would be needed to write all the numbers in words from 1 to 1000?

WARNING: The code must be American because of the spelling, it is Forty not Fourty etc.

I’d like to credit this site for the numbers to words script, I changes the spellings though.

function convert_number($number)
{
    if (($number < 0) || ($number > 999999999))
    {
    throw new Exception("Number is out of range");
    } 

    $Gn = floor($number / 1000000);  /* Millions (giga) */
    $number -= $Gn * 1000000;
    $kn = floor($number / 1000);     /* Thousands (kilo) */
    $number -= $kn * 1000;
    $Hn = floor($number / 100);      /* Hundreds (hecto) */
    $number -= $Hn * 100;
    $Dn = floor($number / 10);       /* Tens (deca) */
    $n = $number % 10;               /* Ones */ 

    $res = ""; 

    if ($Gn)
    {
        $res .= convert_number($Gn) . " Million";
    } 

    if ($kn)
    {
        $res .= (empty($res) ? "" : " ") .
            convert_number($kn) . " Thousand";
    } 

    if ($Hn)
    {
        $res .= (empty($res) ? "" : " ") .
            convert_number($Hn) . " Hundred";
    } 

    $ones = array("", "One", "Two", "Three", "Four", "Five", "Six",
        "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
        "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
        "Nineteen");
    $tens = array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty",
        "Seventy", "Eighty", "Ninety"); 

    if ($Dn || $n)
    {
        if (!empty($res))
        {
            $res .= " and ";
        } 

        if ($Dn < 2)
        {
            $res .= $ones[$Dn * 10 + $n];
        }
        else
        {
            $res .= $tens[$Dn]; 

            if ($n)
            {
                $res .= "-" . $ones[$n];
            }
        }
    } 

    if (empty($res))
    {
        $res = "zero";
    } 

    return $res;
} 

$i = 1;
$total = '';
while($i < 1001){
	$total = $total . convert_number($i);
	$i++;
}
// take out spaces and hyphens
$total = str_replace(' ', '', $total);
$total = str_replace('-', '', $total);
#echo $total;
echo strlen($total);
6 January 2010

Project Euler puzzle 16, What is the sum of the digits of the number 21000?

What is the sum of the digits of the number 21000?

$var = 2;
$i = 0;
while($i < 999){
    $var = $var * 2;
    $i++;
}
$result = number_format($var);
$result = str_replace(',', '',$result);
$array = str_split($result);
$final = 0;
foreach($array as $char){
    $final = $final + $char;
}
echo $final;
4 January 2010

A HTML Tag

The a tag is the element that is generally used to link to another page or file, it is also used to link to an anchor. An anchor is a set place in the page that you can link to, I’m sure you have all seen a link saying “top” at the bottom of some pages that takes you to the top of the page.

Example: Link to another web page

<a href="http://example.com/">Example Link</a>

Example: Link to a file

<a href="http://example.com/files.zip">Download File</a>

Example: Set an anchor and link to it.

<a name="label">Content</a>
<a href="#label">Go to Content</a>

Control the link with the target attribute

This defines where the links is open, for example, open it in a new window, open it in an iFrame, or if the link is in a iFrame, break out of the frame etc.

<a href="http://example.com" target="_bank">Open link in new window</a>

Other targets you can use are:

  • _blank (open in new window)
  • _parent (to break out of a frame)
  • _self (default, open in same window)
  • _top (top of the page)
  • name (link to the name of an iFrame)

browse archive