gsnedders

The longer anything goes on for, the less it makes sense.

3, 2, 1, we have liftoff

Tags: February 19, 2005 (0 comments)

Countdown Scripts, we all need one somewhere, even if it just until our next birthday :P So, here's mine:

<?phpfunction countdown ($count) {$difference = $count - time();$abs_difference = abs($difference); $days = floor($abs_difference/60/60/24);$hours = floor(($abs_difference - $days*60*60*24)/60/60); $minutes = floor(($abs_difference - $days*60*60*24 - $hours*60*60)/60); if ($difference != $abs_difference) {return "It was $days days$hours hours and $minutes minutes ago!";} else {return "Only $days days$hours hours and $minutes minutes to go!";} }?>

To use it, you pass a *nix timestamp in the function call, eg. countdown (1108774476);
Remember you can also use the mktime function, eg. countdown (mktime (3, 20, 0, 4, 20, 1992));
Lastly, it returns the string, it doesn't output it, so to output it you'll need to use echo or print, eg. echo countdown (1108774867);

The forgotten command: switch

Tags: February 4, 2005 (15 comments)

Everyday, I see lots of people using code looking something like this:

<?php $rand = mt_rand(1,10); if ($rand == 1) {echo 'What ever you want to do if $rand == 1';} elseif ($rand == 2) {echo 'What ever you want to do if $rand == 2';} elseif ($rand == 3) {echo 'What ever you want to do if $rand == 3';} elseif ($rand == 4) {echo 'What ever you want to do if $rand == 4';} elseif ($rand == 5) {echo 'What ever you want to do if $rand == 5';} elseif ($rand == 6) {echo 'What ever you want to do if $rand == 6';} elseif ($rand == 7) {echo 'What ever you want to do if $rand == 7';} elseif ($rand == 8) {echo 'What ever you want to do if $rand == 8';} elseif ($rand == 9) {echo 'What ever you want to do if $rand == 9';} elseif ($rand == 10) {echo 'What ever you want to do if $rand == 10';} ?>

When, surely, it is easier to use the switch command, which will require less code (although more whitespace is common) and will be more readable, it'd look something like this:

<?php $rand = mt_rand(1,10); switch ($rand) { case 1:echo 'What ever you want to do if $rand == 1';break; case 2:echo 'What ever you want to do if $rand == 2';break; case 3:echo 'What ever you want to do if $rand == 3';break; case 4:echo 'What ever you want to do if $rand == 4';break; case 5:echo 'What ever you want to do if $rand == 5';break; case 6:echo 'What ever you want to do if $rand == 6';break; case 7:echo 'What ever you want to do if $rand == 7';break; case 8:echo 'What ever you want to do if $rand == 8';break; case 9:echo 'What ever you want to do if $rand == 9';break; case 10:echo 'What ever you want to do if $rand == 10';break; } ?>

Of course, use of switch is not limited to number generated by the mt_rand command, it's just what I've chosen to use in my example here, partly because I've been using mt_rand quite a lot recently...

Relevent PHP Manual Pages:

XHTML/HTML

Tags: , January 7, 2005 (20 comments)

There has always been a lot of discussion about which one to use, and people have attacked this from different angles, with arguments including:

  • Internet Explorer enters "Quirks Mode" when an XHTML document has the XML prologue, yet has a 70% marketshare
  • If there's a slight error in a XHTML page, being correctly served as application/xhtml+xml browsers which support it fail to render it
  • XHTML is the future, so why stay with HTML?

So, basically, if we were able to hand out equally valid XHTML 1.1 and HTML 4.01 Strict depending on whether the UA supports application/xhtml+xml, all our problems have gone? Wrong. Browsers that don't support CSS will still fail, so make sure you use structured, semantic mark-up, then, even without styles, it will 99% of the time, look alright, and be readable, and understandable :)

But, how are we meant to sniff wheather the UA supports application/xhtml+xml, and through that XHTML? Javascript? No, it can be disabled. So what? PHP, or another server side language.

If your server supports PHP, here's the code:

<?php
// Charset
$charset = 'utf-8';
 
function fix_code($buffer) {
   $str = (str_replace(" />", ">", $buffer));
   return (str_replace("xml:lang", "lang", $str));
}
if ((stristr($_SERVER["HTTP_ACCEPT"], 'application/xhtml+xml'))  || (stristr($_SERVER["HTTP_USER_AGENT"], 'W3C_Validator')) || (stristr($_SERVER["HTTP_USER_AGENT"], 'WDG_Validator'))) {
$mime = 'application/xhtml+xml';
} else {
$mime = 'text/html';
}
header ("Content-type$mime");
If ($mime == "application/xhtml+xml") {
echo '<?xml version="1.0" encoding="' . $charset . '"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
} else {
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">';
ob_start("fix_code");
}
?>
 
<html <?php if ($mime == "application/xhtml+xml") { echo'xmlns="http://www.w3.org/1999/xhtml"'; } ?> xml:lang="en">
 <head>
  <meta http-equiv="content-type" content="<?php echo $mime; ?>; charset=<?php echo $charset; ?>" />

I cleaned out this version, putting the settings at the top, making easier for someone who doesn't know PHP to use, but compromising a couple of milliseconds :P

If anyone can convert this to ASP they will be thanked, get the code put up in the post and be creditied for it...

The above represented my views at the time, however, things change, so please read XHTML/HTML Followup.

[Updated 10th January 2005 - Adding xml:lang to lang support]

[Updated 10th January 2005 - Bug Fix]

[Updated 2nd March 2005 - Mistakes pointed out by Mithoric]

[Updated 21st August 2005 - New Views]

Page:  1 2 3