<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users132</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/132" rel="self"/>
  <title>Mike Cochrane</title>
  <updated>Mon Oct 08 05:13:31 -0700 2007</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor326</id>
    <published>2007-10-08T05:13:31-07:00</published>
    <title>[PHP] On Copyright Year</title>
    <content type="html">&lt;p&gt;@JWvdV: Close, but you missed the ' - ' between then years. And the syntax error.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
echo &amp;quot;Copyright &amp;amp;copy; 2007&amp;quot; . ((intval(date('Y')) != 2007) ? date(' - Y') : '') . &amp;quot; - All Rights Reserved - Design by scott2010_h&amp;quot;;
?&amp;gt;
&lt;/pre&gt;</content>
    <author>
      <name>Mike Cochrane</name>
      <email>mikec@resnet.net.nz</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/68-copyright-year/refactors/326" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor325</id>
    <published>2007-10-08T05:07:05-07:00</published>
    <title>[PHP] On Bytes to Readable</title>
    <content type="html">&lt;p&gt;&amp;gt; Make this simpler?&lt;/p&gt;

&lt;p&gt;No, I can't make it simpler (from a human point of view) but I can make it faster.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Mike Cochrane</name>
      <email>mikec@resnet.net.nz</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/67-bytes-to-readable/refactors/325" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor322</id>
    <published>2007-10-08T02:17:03-07:00</published>
    <title>[PHP] On Bytes to Readable</title>
    <content type="html">&lt;p&gt;Okay, there was a bug in my rounding code. Here's the corrected version. Completes in 51% of the time of the original.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
function getsize($size) {
    $si = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
    $remainder = $i = 0;
    while ($size &amp;gt;= 1024 &amp;amp;&amp;amp; $i &amp;lt; 8) {
        $remainder = (($size &amp;amp; 0x3ff) + $remainder) / 1024;
        $size = $size &amp;gt;&amp;gt; 10;
        $i++;
    }
    return round($size + $remainder, 2) . ' ' . $si[$i];
}&lt;/pre&gt;</content>
    <author>
      <name>Mike Cochrane</name>
      <email>mikec@resnet.net.nz</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/67-bytes-to-readable/refactors/322" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor321</id>
    <published>2007-10-08T01:52:53-07:00</published>
    <title>[PHP] On Bytes to Readable</title>
    <content type="html">&lt;p&gt;My code does everything with bit shifts. Also doesn't output a divide by zero error when size = 0. In my trials it completes in 49% of the time of the original. My code is not 100% equivalent - when size = 0 my code returns '0 B', the original returns '0 '.&lt;/p&gt;

&lt;pre&gt;function getsize($size) {
    $si = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
    $remainder = $i = 0;
    while ($size &amp;gt;= 1024 &amp;amp;&amp;amp; $i &amp;lt; 8) {
        $remainder = $size &amp;amp; 0x3ff;
        $size = $size &amp;gt;&amp;gt; 10;
        $i++;
    }
    return round($size + ($remainder/1024), 2) . ' ' . $si[$i];
}
&lt;/pre&gt;</content>
    <author>
      <name>Mike Cochrane</name>
      <email>mikec@resnet.net.nz</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/67-bytes-to-readable/refactors/321" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor257</id>
    <published>2007-10-03T17:25:58-07:00</published>
    <title>[PHP] On ISBN10 to ISBN13</title>
    <content type="html">&lt;p&gt;Thanks James. You're right on the ISBN-10 check sum range. For ISBN-13 is ranges from 0-9. There is no A according to &lt;a href="http://www.isbn-international.org/" target="_blank"&gt;http://www.isbn-international.org/&lt;/a&gt; or Wikipedia. Thanks for pointing this out.&lt;/p&gt;

&lt;p&gt;So my previous solution was incorrect. Here is a correct solution. Performance hit in the fix though.&lt;/p&gt;

&lt;p&gt;So final result is 45% time saving on leightmac's but it doesn't reject legit numbers and the check digit is always a single digit, not '10' like it could have previously been.&lt;/p&gt;

&lt;pre&gt;function isbn10_to_13($isbn) {
    if (!preg_match('{^([0-9]{9})[0-9xX]$}', $isbn, $matches)) {
        # number is not 10 digits
        return false;
    }

    # sum the digits with their weights and add the checksum for the 978 prefix
    $sum_of_digits = 38 + 3 * ($isbn{0} + $isbn{2} + $isbn{4} + $isbn{6} + $isbn{8}) +
                               $isbn{1} + $isbn{3} + $isbn{5} + $isbn{7};

    # divide the sum_of_digits by the modulus number (10) to find the remainder
    # and then minus 10 to get the check digit
    $check_digit = (10 - ($sum_of_digits % 10)) % 10;

    # return isbn with check_digit
    return '978' . $matches[1] . $check_digit;
}&lt;/pre&gt;</content>
    <author>
      <name>Mike Cochrane</name>
      <email>mikec@resnet.net.nz</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/33-isbn10-to-isbn13/refactors/257" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor238</id>
    <published>2007-10-03T03:45:00-07:00</published>
    <title>[PHP] On Content Page Include</title>
    <content type="html">&lt;p&gt;I did a quick performance test. Using the two functions below - eliminating the file operations and returning the string instead. Run once timed, change function, timed, changed back, timed, changed to v2 again, timed.
&lt;br /&gt;For two pages, the switch took 49.9% of the time of my code - switch wins by heaps (15,000 calls - two runs avg of both).
&lt;br /&gt;For 101 pages, the switch took 49.5% of the time of my code - (255,000 calls - one run each)&lt;/p&gt;

&lt;p&gt;So there you have it. The switch is twice as fast as my code. Take your pick :-)&lt;/p&gt;

&lt;p&gt;FYI in 101 page test: 
&lt;br /&gt;  v3 was 107% of v2 (the ternary construct is faster in this case).
&lt;br /&gt;  v4 was 52%  of v2 (in_array is the killer in v2).
&lt;br /&gt;  v5 was identical to v2 (v2 with array_search($_GET['id'], $pages) !== false instead of in_array)&lt;/p&gt;

&lt;pre&gt;### Test one
function v1() {
    switch($_GET['id'])
    {
       default:
       case &amp;quot;news&amp;quot;:
          return &amp;quot;news.php&amp;quot;;
       break;

       case &amp;quot;about&amp;quot;:
          return &amp;quot;about.php&amp;quot;;
       break;
    }
}

function v2() {
    $pages = array('news', 'about');
    return (isset($_GET['id']) &amp;amp;&amp;amp; in_array($_GET['id'], $pages)) ? $_GET['id'] . '.php' : 'default.php';
}

$_GET['id'] = 'news';
for ($i = 0; $i &amp;lt; 5000; $i++) v1();
$_GET['id'] = 'about';
for ($i = 0; $i &amp;lt; 5000; $i++) v1();
$_GET['id'] = '';
for ($i = 0; $i &amp;lt; 5000; $i++) v1();

### Test 2
function v1() {
    switch($_GET['id'])
    {
       default:
       case &amp;quot;news&amp;quot;:
          return &amp;quot;news.php&amp;quot;;
       break;

       case &amp;quot;page1&amp;quot;:
          return &amp;quot;page1.php&amp;quot;;
       break;

       case &amp;quot;page2&amp;quot;:
          return &amp;quot;page2.php&amp;quot;;
       break;

...

       case &amp;quot;page99&amp;quot;:
          return &amp;quot;page99.php&amp;quot;;
       break;

       case &amp;quot;about&amp;quot;:
          return &amp;quot;about.php&amp;quot;;
       break;
    }
}

function v2() {
    $pages = array('news', 'page1', 'page2', .... 'page99', 'about');
    return (isset($_GET['id']) &amp;amp;&amp;amp; in_array($_GET['id'], $pages)) ? $_GET['id'] . '.php' : 'default.php';
}

function v3() {
    $pages = array('news', 'page1', 'page2', .... 'page99', 'about');
    if (isset($_GET['id']) &amp;amp;&amp;amp; in_array($_GET['id'], $pages)) {
        return  $_GET['id'] . '.php';
    } else {
        return 'default.php';
    }
}

function v4() {
    if (!isset($_GET['id'])) {
        return &amp;quot;news.php&amp;quot;;
    }
    switch($_GET['id'])
    {
       default:
       case &amp;quot;news&amp;quot;:
          return &amp;quot;news.php&amp;quot;;
       break;

       case &amp;quot;page1&amp;quot;:
          return &amp;quot;page1.php&amp;quot;;
       break;

       case &amp;quot;page2&amp;quot;:
          return &amp;quot;page2.php&amp;quot;;
       break;

....

       case &amp;quot;page98&amp;quot;:
          return &amp;quot;page98.php&amp;quot;;
       break;

       case &amp;quot;page99&amp;quot;:
          return &amp;quot;page99.php&amp;quot;;
       break;

       case &amp;quot;about&amp;quot;:
          return &amp;quot;about.php&amp;quot;;
       break;
    }
}

function v5() {
    $pages = array('news', 'page1', 'page2',. ... 'page99', 'about');
    return (isset($_GET['id']) &amp;amp;&amp;amp; array_search($_GET['id'], $pages) !== false) ? $_GET['id'] . '.php' : 'default.php';
}

$_GET['id'] = 'news';
for ($i = 0; $i &amp;lt; 2500; $i++) v1();
$_GET['id'] = 'about';
for ($i = 0; $i &amp;lt; 2500; $i++) v1();
$_GET['id'] = '';
for ($i = 0; $i &amp;lt; 2500; $i++) v1();

for ($p = 1; $p &amp;lt; 100; $p++) {
    $_GET['id'] = 'page' . $p;
    for ($i = 0; $i &amp;lt; 2500; $i++) v1();
}&lt;/pre&gt;</content>
    <author>
      <name>Mike Cochrane</name>
      <email>mikec@resnet.net.nz</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/42-content-page-include/refactors/238" rel="alternate"/>
  </entry>
</feed>

