Most coders and designers will tell you Internet Explorer (Especially IE6 below) is a pain to work with, but I tend to disagree. What most people forget is that even though IE is full of bugs and oddities everything can be fixed, with just a few lines of code.
How to use Conditional Comments
Conditional Comments let you execute sections of code in just IE, so you can import custom CSS sheets (to deal with IE’s weird rendering engine) or special JavaScript; here is how to use conditional comments:
<!--[if IE]>
This will only show for Internet Explorer (IE)
<![endif]-->
<!--[if !IE]>
This will only show if the client is not using IE.
<!--[endif]>
<![if IE 6]-->
Only shows on IE6
<!--[endif]>
<![if lte IE 6]-->
Any IE browser less than or equal to version 6.
<!--[endif]>
<![if gte IE 7]-->
Any IE browser higher or equal to IE 7
<!--[endif]>
Just place snippets of the above code anywhere in your code. If you want more information conditional commenting take a look at the Conditional Comment Wikipedia page.
PHP is lovely, though if you ever work with other people having good code is important. Here are some tips to improve your code.
Comment & Document
It’s really important that people understand why you are doing certain things in certain ways. Adding a quick comment above sections of code should be adequate, but documenting classes and functions (Even if it’s in a Wiki) is fantastic.
Give Variables, Functions and Classes Meaningful names
Nothing is worse than trying to figure out what a function called “SIDFE()” does. Give everything a name that if someone else looked at it, they could figure out what it does. The above is a real example I have come across while adjusting a clients website, if the other programmer had called it something like Scan_Incomming_Data_For_Evil() it would have been a lot more straightforward.
White space
As you can see, a little space here and there makes life a lot easier.
No one likes to have to search for the start of a function. Make sure you indent your code and keep it easy to read quickly.
Never Delete – Comment out
This one is a little hard to grasp, but imagine you just fixed a bug (say 100 lines of code to fix it) and something else has broken. It makes sense to be able to go back and see the old code without modification of the new code. Also, doing this helps people see where an old bug was (assuming you comment that the section of code is evil) for future reference.
Use Braces
Braces are those neat } and { things. If you don’t use them on various functions it’s a pain to figure out where a loop starts and finishes. This is especially important when programming on a large scale because no one likes debugging fugly code. Here is an example of good and bad code:
<?php
/* Examples of annoying code */
if ( $coder === 'Silly' ) bang_head();
while ( $coder === 'Silly' )
bang_head();
/* Examples of good code*/
if ( $coder !== 'Silly' ){ Drink_Beer(); }
while ( $coder !== 'Silly' ){
Drink_Beer();
}
?>
StumbleUpon is a funky web 2.0 community where it recommends websites you may be interested in based on other people similar interests. Unfortunately they do not have an API for easy integration. Luckily though, they do offer RSS Feeds for user’s activity. Here is the code you need to show your recent stumbles:
<?php # File created on 3rd April 2009 by Mike Rogers (http://www.fullondesign.co.uk/).
/*
function - recent_stumbles(string $username [, string $type= NULL [, int $limit = 5]])
$username - The stumbleupon username, such as rogem002
$type - Default: NULL - What you want to limit your rss to show. Can be NULL, blog, comments, favorites or reviews
$limit - Default: 5 - how many tweets you wish to show, must be numeric.
*/
function recent_stumbles($username, $type=NULL, $limit=5){
if(!is_numeric($limit)){$limit = 5;}
if($type !== NULL && $type !== 'blog' && $type !== 'comments' && $type !== 'favorites' && $type !== 'reviews'){$type = NULL;}
$xml = simplexml_load_file('http://rss.stumbleupon.com/user/'.urlencode($username).'/'.$type);
$items_count= count($xml->channel->item);
if($items_count < $limit){$limit = $items_count;}
$i = 0;
$return .= '
<ul>';
while($i < $limit){
$return .= '
<li title="'.$xml->channel->item[$i]->title.'"><!-- '.$xml->channel->item[$i]->pubDate.' -->
<a href="'.$xml->channel->item[$i]->link.'" title="'.$xml->channel->item[$i]->title.'"><img src="'.$xml->channel->item[$i]->enclosure["url"].'" alt="'.$xml->channel->item[$i]->title.'" border="0" />
'.$xml->channel->item[$i]->title.'</a></li>
';
$i++;
}
$return .= '</ul>
';
return $return;
}
echo recent_stumbles('Rogem002', 'favorites', 5);
/*
You are free to share, modify and use this code for commercial uses. Please give a link back (to http://www.fullondesign.co.uk/ ) if you can, but you don't have you.
*/
?>
Twitter has sprung to fame in the last few months, mostly thanks to its fantastic API. Here is a really quick and easy way to display your recent tweets on your website.
<?php # File created on 1st April 2009 by Mike Rogers (http://www.fullondesign.co.uk/).
/*
function - recent_tweets(string $username [, int $limit = 5])
$username - Your twitter username, such as rogem002
$limit - Default: 5 - how many tweets you wish to show, must be numeric.
*/
function recent_tweets($username, $limit=5){
if(!is_numeric($limit)){$limit = 5;}
$xml = simplexml_load_file('http://search.twitter.com/search.atom?q=from%3A'.urlencode($username));
$items_count= count($xml->entry);
if($items_count < $limit){$limit = $items_count;}
$i = 0;
$return .= '
<ul>';
while($i < $limit){
$return .= '
<li title="'.$xml->entry[$i]->title.'"><!-- '.$xml->entry[$i]->published.' -->'.$xml->entry[$i]->content.'</li>
';
$i++;
}
$return .= '</ul>
';
return $return;
}
echo recent_tweets('rogem002', 5);
/*
You are free to share, modify and use this code for commercial uses. Please give a link back (to http://www.fullondesign.co.uk/ ) if you can, but you don't have you.
*/
?>
Edit: This only works for Twitterers who have privacy settings open (thanks Dan from XDnet.co.uk for the heads up).
CSS is incredible, however its implementation in browsers has not been standardised, meaning that every browser has a slightly different default settings for how elements should be rendered. Luckily there is an easy fix in CSS to reduce the level of dissimilarity.