<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Use as little code as possible when writing PHP</title>
	<atom:link href="http://www.clubosc.com/use-as-little-code-as-possible-when-writing-php.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.clubosc.com/use-as-little-code-as-possible-when-writing-php.html</link>
	<description>Showcasing osCommerce...the good, the bad and the ugly!</description>
	<lastBuildDate>Mon, 30 Jan 2012 16:32:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
	<item>
		<title>By: Gary</title>
		<link>http://www.clubosc.com/use-as-little-code-as-possible-when-writing-php.html/comment-page-1#comment-3532</link>
		<dc:creator>Gary</dc:creator>
		<pubDate>Thu, 17 Sep 2009 15:08:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.clubosc.com/?p=394#comment-3532</guid>
		<description>Excellent - love it when a blog post of mine leads to more (and better) solutions.  Now we have 4 solutions doing the same thing.  And that means more choidce, more ideas and more learning for all of us.  Sweet.

Cheers, Guys</description>
		<content:encoded><![CDATA[<p>Excellent &#8211; love it when a blog post of mine leads to more (and better) solutions.  Now we have 4 solutions doing the same thing.  And that means more choidce, more ideas and more learning for all of us.  Sweet.</p>
<p>Cheers, Guys</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim</title>
		<link>http://www.clubosc.com/use-as-little-code-as-possible-when-writing-php.html/comment-page-1#comment-3517</link>
		<dc:creator>Tim</dc:creator>
		<pubDate>Wed, 16 Sep 2009 22:24:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.clubosc.com/?p=394#comment-3517</guid>
		<description>How about:

preg_replace(&#039;/([0-9]{4})([0-9]{3,4})([0-9]{3,4})([0-9]{3,})/&#039;, &#039;$1-$2-$3-$4&#039;, $order-&gt;info[&#039;cc_number&#039;]);

That will match a 16-digit number to 4-4-4-4, a 13 digit number to 4-3-3-3, and will fill the spaces up including a 19 digit number as 4-4-4-7

And it&#039;s only 1 PHP function!</description>
		<content:encoded><![CDATA[<p>How about:</p>
<p>preg_replace(&#039;/([0-9]{4})([0-9]{3,4})([0-9]{3,4})([0-9]{3,})/&#039;, &#039;$1-$2-$3-$4&#039;, $order-&gt;info['cc_number']);</p>
<p>That will match a 16-digit number to 4-4-4-4, a 13 digit number to 4-3-3-3, and will fill the spaces up including a 19 digit number as 4-4-4-7</p>
<p>And it&#039;s only 1 PHP function!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://www.clubosc.com/use-as-little-code-as-possible-when-writing-php.html/comment-page-1#comment-3485</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Tue, 15 Sep 2009 02:38:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.clubosc.com/?p=394#comment-3485</guid>
		<description>Ooh, can I play?  I choose:

echo implode(&#039;-&#039;, str_split($order-&gt;info[&#039;cc_number&#039;], 4));

which I find easier to read (we are splitting the string every four characters and then joining it back together with - between the pieces).  It also only requires the separator to be specified once (consider what happens if you change the separator for the chunk_split but not the rtrim).  

I find the chunk_split behavior of adding a character at the end which then needs to be removed counter-intuitive.  The substr version is actually easier for me to read, albeit verbose.  The chunk_split version might be faster though.  

Using minimal code isn&#039;t just a PHP thing.  It&#039;s true of pretty much any coding language.  The more code, the more likely that there is a bug (potentially a typo) and the harder it is to debug or change behavior.  

Note that in this particular case, the substr code is not robust in the face of credit cards of more than sixteen characters.  All it would take would be one provider with a non-standard credit card length over sixteen characters and it will silently drop part of the credit card info. Certainly fixable:  remove the ,4 after the 12 or 

$cc_output = &#039;&#039;;
$cc_temp = (string)$order-&gt;info[&#039;cc_number&#039;];
while ( strlen($cc_temp) &gt; 4 ) {
  $cc_output .= substr($cc_temp, 0, 4) . &#039;-&#039;;
  $cc_temp = substr($cc_temp, 4);
}
echo $cc_output . $cc_temp;

would probably do it but is even more code.  

None of these solutions would really work well for a thirteen character credit card number.  Note that the thirteen character split is normally 4 3 3 3.  You&#039;d pretty much have to handle that case specially to produce the changing chunk length.  E.g. something like

if ( strlen($order-&gt;info[&#039;cc_number&#039;]) == 13 ) {
  // since we want 4 3 3 3, split the number into 
  // a first digit and a twelve digit string
  $first_digit = substr($order-&gt;info[&#039;cc_number&#039;], 0, 1);
  $rest_of_number = substr($order-&gt;info[&#039;cc_number&#039;], 1);

  // then split the twelve into four groups of three
  // then join the groups with - between them
  // then put the first digit back in front
  // and we have four digits in the first group
  // and three in each of the other groups
  echo $first_digit . implode(&#039;-&#039;, str_split($rest_of_number, 3));
} else {
  echo implode(&#039;-&#039;, str_split($order-&gt;info[&#039;cc_number&#039;], 4));
}

I used temporary variables with the substr to make the code more self commenting.  The only real advantage of using implode and str_split in the 13 case is that it allows us to specify the separator and piece length (3) only once.  With substr, we&#039;d have to rewrite it multiple times.  Because the piece length is three, we also could use number_format for the 13 case, as so:  

echo $first_digit . number_format($rest_of_number, 0, &#039;&#039;, &#039;-&#039;);

but I don&#039;t really like that as it relies on the thousands separator being used between groups of three digits.  It also converts the number to a float, which is not necessary here.  Not robust in the face of letters in the credit card &quot;number&quot;.</description>
		<content:encoded><![CDATA[<p>Ooh, can I play?  I choose:</p>
<p>echo implode(&#039;-&#039;, str_split($order-&gt;info['cc_number'], 4));</p>
<p>which I find easier to read (we are splitting the string every four characters and then joining it back together with &#8211; between the pieces).  It also only requires the separator to be specified once (consider what happens if you change the separator for the chunk_split but not the rtrim).  </p>
<p>I find the chunk_split behavior of adding a character at the end which then needs to be removed counter-intuitive.  The substr version is actually easier for me to read, albeit verbose.  The chunk_split version might be faster though.  </p>
<p>Using minimal code isn&#039;t just a PHP thing.  It&#039;s true of pretty much any coding language.  The more code, the more likely that there is a bug (potentially a typo) and the harder it is to debug or change behavior.  </p>
<p>Note that in this particular case, the substr code is not robust in the face of credit cards of more than sixteen characters.  All it would take would be one provider with a non-standard credit card length over sixteen characters and it will silently drop part of the credit card info. Certainly fixable:  remove the ,4 after the 12 or </p>
<p>$cc_output = &#034;;<br />
$cc_temp = (string)$order-&gt;info['cc_number'];<br />
while ( strlen($cc_temp) &gt; 4 ) {<br />
  $cc_output .= substr($cc_temp, 0, 4) . &#039;-&#039;;<br />
  $cc_temp = substr($cc_temp, 4);<br />
}<br />
echo $cc_output . $cc_temp;</p>
<p>would probably do it but is even more code.  </p>
<p>None of these solutions would really work well for a thirteen character credit card number.  Note that the thirteen character split is normally 4 3 3 3.  You&#039;d pretty much have to handle that case specially to produce the changing chunk length.  E.g. something like</p>
<p>if ( strlen($order-&gt;info['cc_number']) == 13 ) {<br />
  // since we want 4 3 3 3, split the number into<br />
  // a first digit and a twelve digit string<br />
  $first_digit = substr($order-&gt;info['cc_number'], 0, 1);<br />
  $rest_of_number = substr($order-&gt;info['cc_number'], 1);</p>
<p>  // then split the twelve into four groups of three<br />
  // then join the groups with &#8211; between them<br />
  // then put the first digit back in front<br />
  // and we have four digits in the first group<br />
  // and three in each of the other groups<br />
  echo $first_digit . implode(&#039;-&#039;, str_split($rest_of_number, 3));<br />
} else {<br />
  echo implode(&#039;-&#039;, str_split($order-&gt;info['cc_number'], 4));<br />
}</p>
<p>I used temporary variables with the substr to make the code more self commenting.  The only real advantage of using implode and str_split in the 13 case is that it allows us to specify the separator and piece length (3) only once.  With substr, we&#039;d have to rewrite it multiple times.  Because the piece length is three, we also could use number_format for the 13 case, as so:  </p>
<p>echo $first_digit . number_format($rest_of_number, 0, &#034;, &#039;-&#039;);</p>
<p>but I don&#039;t really like that as it relies on the thousands separator being used between groups of three digits.  It also converts the number to a float, which is not necessary here.  Not robust in the face of letters in the credit card &#034;number&#034;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carine</title>
		<link>http://www.clubosc.com/use-as-little-code-as-possible-when-writing-php.html/comment-page-1#comment-3471</link>
		<dc:creator>Carine</dc:creator>
		<pubDate>Mon, 14 Sep 2009 15:38:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.clubosc.com/?p=394#comment-3471</guid>
		<description>and as much whitespace as possible too ;) 
makes it all much easier to read when alignment is kept ...</description>
		<content:encoded><![CDATA[<p>and as much whitespace as possible too <img src='http://www.clubosc.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
makes it all much easier to read when alignment is kept &#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

