Not too sure about template.php
The template.php file should contain the presentation code; the "logic" code, such as the theme functions, or other code that computes the value of a variable, should be in the template.php file.

To make an example, code similar to the following one should not be in the template file, but it should be put in a function contained in the template.php file, and which return a value to the calling code, which then uses the returned code:

  if ($user-&gt;uid) {
    $class[] = 'authenticaded-user';

    if (user_access('administer content')) {
      $class[] =  'content-administrator';
    }
  }
  else {
    $class[] = 'anonymous-user';
    $class[] = 'anonymous-' . strtr(ip_address(), '.', '-'); 
  }

  if (isset($class)) {
    print ' class="' . implode(' ', $class) . '"';
  }


The code in the template file should just print (or render) values, but not compute those values; clearly, it's perfectly fine to use code similar to the following one (the code is taken from a Drupal 7 theme):

    &lt;?php if ($logo || $site_title): ?&gt;
      &lt;?php if ($title): ?&gt;
        &lt;div id="branding"&gt;&lt;strong&gt;&lt;a href="&lt;?php print $front_page ?&gt;"&gt;
        &lt;?php if ($logo): ?&gt;
          &lt;img src="&lt;?php print $logo ?&gt;" alt="&lt;?php print $site_name_and_slogan ?&gt;" title="&lt;?php print $site_name_and_slogan ?&gt;" id="logo" /&gt;
        &lt;?php endif; ?&gt;
        &lt;?php print $site_html ?&gt;
        &lt;/a&gt;&lt;/strong&gt;&lt;/div&gt;
      &lt;?php else: /* Use h1 when the content title is empty */ ?&gt;
        &lt;h1 id="branding"&gt;&lt;a href="&lt;?php print $front_page ?&gt;"&gt;
        &lt;?php if ($logo): ?&gt;
          &lt;img src="&lt;?php print $logo ?&gt;" alt="&lt;?php print $site_name_and_slogan ?&gt;" title="&lt;?php print $site_name_and_slogan ?&gt;" id="logo" /&gt;
        &lt;?php endif; ?&gt;
        &lt;?php print $site_html ?&gt;
        &lt;/a&gt;&lt;/h1&gt;
    &lt;?php endif; ?&gt;
    &lt;?php endif; ?&gt;


The difference is that, in this case, the code is verifying if the variable contains a useful value, and eventually avoid to output the markup, when it's not necessary.