Check out the module developers guide:
http://drupal.org/developing/modules
And the drupal “installing modules” pages:
http://drupal.org/documentation/install/modules-themes
You can also add code to “theme” your output by using template.php i theme directory:
http://drupal.org/node/173880
solved How to add my own PHP code in Drupal [closed]