<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Cash &#187; coding</title>
	<atom:link href="http://www.earn-web-cash.com/tag/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Binary Code and Bitwise Operators (in PHP)</title>
		<link>http://www.earn-web-cash.com/2008/04/06/binary-code-and-bitwise-operators-in-php/</link>
		<comments>http://www.earn-web-cash.com/2008/04/06/binary-code-and-bitwise-operators-in-php/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 02:30:17 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Redirected]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/04/06/binary-code-and-bitwise-operators-in-php/</guid>
		<description><![CDATA[At it&#8217;s core, all of the information on your computer is made up of bits &#8211; or 0&#8242;s and 1&#8242;s. There&#8217;s quite a bit of interpretation that goes on between that basic binary code and the information as it is displayed on your screen. However, you may find a time to work with data at [...]]]></description>
			<content:encoded><![CDATA[<p>At it&#8217;s core, all of the information on your computer is made up of bits &#8211; or 0&#8242;s and 1&#8242;s.  There&#8217;s quite a bit of interpretation that goes on between that basic binary code and the information as it is displayed on your screen.</p>
<p>However, you may find a time to work with data at the binary level in PHP (and many other programming languages).  Here&#8217;s a quick guide to understanding binary numbers and the common operators for handling them.<br />
<span id="more-185"></span></p>
<h4>&#8220;Normal&#8221; Numbers &#8211; Base Ten, Decimal System</h4>
<p>When you deal with numbers, you are probably using the &#8220;Base Ten&#8221; or &#8220;Decimal&#8221; number system.  These are numbers made up of a set of ten different digits &#8211; zero through nine.</p>
<p>A given quantity &#8211; like 512 &#8211; is created with a series of digits.  The position of the digit tells us how much the digit actually represents.  The &#8217;2&#8242; here is in the units place, so it simply represents 2.  The 1 is in the tens place, so it actually represents 10.  The 5 is in the hundreds place, so it represents 500.</p>
<p>The number could be re-written as:</p>
<pre>500 + 10 + 2 = 512</pre>
<p>You&#8217;ll notice a pattern here.  Each new place is worth ten times the previous place.  The third digit (the hundreds place) is worth 10 * 10.  If we added a fourth place, it would be worth 10 * 100 (1000).</p>
<p>This gives us a nifty little mathematical formula &#8211; which is why this is called &#8220;base ten&#8221; numbering.  The nth digit (where zero is the right-most digit) is worth 10^n.  Here&#8217;s the number 1024 re-written to illustrate this.</p>
<pre>(1 * 10^3) + (0 * 10^2) + (2 * 10^1) + (4 * 10^0) = 1024</pre>
<p>This is a lot of work to read a number that we intuitively know is worth 1,024.  However, it helps to understand how binary numbers work.</p>
<h4>Binary Numbers &#8211; The Base Two System</h4>
<p>While base ten numbers are simple enough for humans to work with, computers are designed to use a binary or base two numbering system.</p>
<p>The simplest unit of information is a binary bit &#8211; a 0 or a 1.</p>
<p>To create complex numbers, we can string together 0&#8242;s and 1&#8242;s.  For example, a number could be written as 0010 0101.</p>
<p>To understand what this value means, we can apply the same rules as we did above.  However, since this is a base two system instead of a base ten system, each place is multiplied by 2^n.</p>
<p>Let&#8217;s start with a smaller example &#8211; 1111.</p>
<pre>(1 * 2^3) + (1 * 2^2) + (1 * 2^1) + (1 * 2^0)
    = 8 + 4 + 2 + 1
    = 15</pre>
<p>These rules can be expanded to represent byte-sized numbers (8-bits, or 2^8, or 255) or even larger numbers.</p>
<h4>Manipulating Bits and Bytes &#8211; Bitwise Operators</h4>
<p>Most programming languages come with some built in operators to handle bits.  These help make very basic comparisons and manipulations of numbers.</p>
<p>These typically include the &#8216;and&#8217; or intersection operator, the &#8216;or&#8217; or union operator, and the &#8216;xor&#8217; or exclusive-or operator.  We&#8217;ll just take a look at the &#8216;and&#8217; and &#8216;or&#8217; operators right now.  [Note:  I use the terms intersection and union because I think they make it much easier to understand, if you are at all familiar with math sets and statistics.  They aren&#8217;t typical &#8216;coding&#8217; terms).</p>
<p>The &#8216;and&#8217; or intersection operator &#8211; the &#8216;&amp;&#8217; character in PHP &#8211; compares two binary numbers and returns a new binary number with a &#8217;1&#8242; in every unit in which <strong>both</strong> numbers have a &#8217;1&#8242;.  If both numbers have a value in the second place, then the return number will have a value in the second place.  If only one number has a value in the third place, then the return number will have no value in the third place.</p>
<pre>0111 &amp; 0101 = 0101
0101 &amp; 1010 = 0000
1111 &#038; 1111 = 1111</pre>
<p>If you imagine &#8217;0111&#8242; and &#8217;0101&#8242; as sets of numbers ({4, 2, 1} and {4, 1} respectively), then the result of a bitwise &#8216;and&#8217; operation is the simple intersection of the two sets.</p>
<p>The bitwise &#8216;or&#8217; operator &#8211; a | in PHP &#8211; returns a value in each place that <strong>either</strong> number has a value.  So if only one number has a value in the first place, the result will have a value in the first place.  A place will only have no value if <strong>neither</strong> number had a value there.</p>
<pre>0111 | 0101 = 0111
0101 1110 | 1010 0001 = 1111 1111
0000 1111 | 1111 0000 = 1111 1111</pre>
<p>These operators aren&#8217;t used very much, but it is important to recognize them.  If you don&#8217;t know what they are, it is easy to confuse them with the standard <a href="http://www.earn-web-cash.com/2008/02/16/php-logical-operator/">logical operators</a> &#8211; || and &amp;&amp;.</p>
<p>For example, the following two lines of code have <strong>very</strong> different meanings.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">124</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">36</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bitwise</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&amp;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$logical</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the first comparison ($x &amp; $y), we&#8217;re finding the bitwise intersection of the two values.  This would be&#8230;</p>
<pre>0111 1100 &amp; 0010 0100 = 0010 0100</pre>
<p>In base ten terms, $bitwise would be equal to 36.  In the second example ($x &amp;&amp; $y), we&#8217;re looking for a simple boolean comparison.  In this case, both $x and $y have a value, so the comparison will return &#8217;1&#8242; or &#8216;true&#8217;.</p>
<h4>Is This Useful&#8230;?  Comparing Constant Flags</h4>
<p>You may be thinking to yourself, &#8220;When will I ever use this?&#8221;</p>
<p>While anyone working with computer code should understand how binary numbers work, there will probably be very few cases in which you manipulate numerical values in a binary format.  The rest of the world works with decimal numbers &#8211; and your programming language is designed to do the same thing.</p>
<p>However, there are some cases where you <strong>will</strong> want to use bitwise numbers and comparisons.  A perfect example is the use of constants and flags.</p>
<p>Let&#8217;s say that a function takes 16 parameters.  Each of these parameters is a simple &#8220;True&#8221; or &#8220;False.&#8221;</p>
<p>You could write the function take sixteen different parameters and use sixteen different variables.  However, this would make function calls quite long &#8211; especially if you usually only make two or three parameters true.</p>
<p>Instead, you can pass one value as a parameter.  This value &#8211; 16 bits long &#8211; includes 16 individual flags to tell the function whether a specific parameter is true or false.</p>
<p>If every parameter is set to true, this value would be &#8220;1111 1111 1111 1111&#8243;.  If only the first parameter should be set to true, this value would be &#8220;0000 0000 0000 0001&#8243;.</p>
<p>Each flag is represented by a specific place in this binary number.  The first flag is &#8220;0001,&#8221; the second flag is &#8220;0010,&#8221; the third flag is &#8220;0100,&#8221; etc.</p>
<p>Your code wouldn&#8217;t be very readable if you referred to each flag by its numerical value.  Instead, you can define constant values.  For example, FUNCTION_FLAG_ONE would represent &#8220;0001,&#8221; &#8220;FUNCTION_FLAG_TWO would represent &#8220;0010,&#8221; etc.  These constants would follow whatever naming convention you usually use.</p>
<p>The value of this is that you can easily set any number of flags with the use of the bitwise &#8216;or&#8217; operator.</p>
<p>For example, let&#8217;s say you want to set flags one, seven, and fifteen to true.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$flags</span> <span style="color: #339933;">=</span> FUNCTION_FLAG_ONE <span style="color: #339933;">|</span> 
    FUNCTION_FLAG_SEVEN <span style="color: #339933;">|</span> FUNCTION_FLAG_FIFTEEN<span style="color: #339933;">;</span></pre></div></div>

<p>The result would be &#8217;0100 0000 0100 0001.&#8217;</p>
<p>Inside the function, you could then use the bitwise &#8216;and&#8217; operator to see which flags were set.  For example, this would let you check to see if the fifteenth flag was set to true.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fifteen</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$flags</span> <span style="color: #339933;">&amp;</span> FUNCTION_FLAG_FIFTEEN<span style="color: #339933;">;</span></pre></div></div>

<p>Although your function&#8217;s code will have to do some legwork to find out which flags were set and which ones were not, this makes the function call itself <strong>much</strong> more efficient and readable.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/04/06/binary-code-and-bitwise-operators-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Work With Directories in PHP</title>
		<link>http://www.earn-web-cash.com/2008/03/06/php-directory-functions/</link>
		<comments>http://www.earn-web-cash.com/2008/03/06/php-directory-functions/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 21:16:37 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[file system]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/06/php-directory-functions/</guid>
		<description><![CDATA[PHP offers a number of different functions to work with the local directory structure. There are at least three fairly simple ways to create and output a list of files in a directory. Like the file functions, each method is slightly different. Knowing how each operates will help you choose which function to use when [...]]]></description>
			<content:encoded><![CDATA[<p>PHP offers a number of different functions to work with the local directory structure.  There are at least three fairly simple ways to create and output a list of files in a directory.</p>
<p>Like the file functions, each method is slightly different.  Knowing how each operates will help you choose which function to use when you want to work with directories in your PHP script.<br />
<span id="more-172"></span></p>
<h4>PHP 4 &#8211; opendir, readdir, closedir</h4>
<p>The traditional method of working with directories is very similar to the traditional method of working with files.  You open a directory and save it as a resource in a variable.  Then, you read through it until the end.  Finally, you close it to tidy things up.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dirName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'images'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$dir</span> <span style="color: #339933;">=</span> <span style="color: #990000;">opendir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">readdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$filename</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">closedir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That simple script will open a directory (&#8216;images&#8217;), read every filename in the directory, and then clean up the directory resource.</p>
<p>The readdir() function goes through the files as they are stored in the filesystem.  There&#8217;s no real logic to their order &#8211; they just come the way they are.  Each time you use readdir(), the pointer in the $dir resource is moved to the next file.</p>
<h4>PHP 5 &#8211; A Convenience Function, scandir</h4>
<p>In PHP 5, a new convenience function was introduced to handle all of the major directory functions with one call.  Similar to the way file_get_contents handles all of the file writing, scandir handles all of the directory listing.</p>
<p>With a single function, scandir opens a given directory, reads all of the files into an array, and closes the directory.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dirname</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'images'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$filenames</span> <span style="color: #339933;">=</span> <span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$filenames</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This slightly shorter snippet of code accomplishes the same thing as the original example.  As an added bonus, the filenames are already stored in an array ($filenames).  The filenames are also sorted alphabetically.</p>
<p>You could of course achieve the same end with the original directory functions &#8211; but you&#8217;d have to add more lines of code to store the data in an array and then sort it.  One of the biggest reasons to use scandir() is that it&#8217;s a convenience.</p>
<p>There&#8217;s also one functional difference between the two.  scandir() never actually accesses a file &#8211; so it&#8217;s &#8220;Last Accessed Time&#8221; will remain the same.  When you use readdir() to cycle through a directory, each file is accessed &#8211; resetting the &#8220;Last Accessed Time.&#8221;</p>
<p>This often won&#8217;t matter, but it could play an important role in a script that works directly with a file&#8217;s &#8220;Last Accessed Time.&#8221;</p>
<h4>The Versatile Choice &#8211; glob</h4>
<p>The most versatile choice is glob().  Like scandir, it returns an array of filenames.  However, glob allows you to specify a pattern to match &#8211; so only the matching filenames are returned.</p>
<p>The pattern follows all of the rules for wildcards when using a command like <code>dir</code>.  For example, this will get all of the files in the &#8216;images&#8217; directory.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dirName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'images'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$filenames</span> <span style="color: #339933;">=</span> <span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/*.*'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$filenames</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There is a very important difference between this and the other functions, though.</p>
<p>glob() returns paths, not filenames.  In this case, the output will be something like &#8216;images/image.jpg,&#8217; not just &#8216;image.jpg.&#8217;  The directory listing is based on the current working directory &#8211; so specifying a path (like we did) will cause that directory to be included in the returned path.</p>
<p>If you don&#8217;t want the full path shown, you can use some other functions to change the working directory &#8211; like using &#8216;cd&#8217; in the command line.  By default, the working directory is where the script is executing.  In this case, we could change the working directory to &#8216;images&#8217; to get filenames without the directory as part of the path.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">chdir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can also use the GLOB_BRACE flag to pass multiple file extensions.  For example, this would allow you to find all images with extension .jpeg, .png, or .gif.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">chdir</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$filenames</span> <span style="color: #339933;">=</span> <span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'*.{jpeg,png,gif}'</span><span style="color: #339933;">,</span> GLOB_BRACE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//  Notice there are NO spaces after the commas</span></pre></div></div>

<p>glob() can be quite a powerful tool, indeed.  If all you want is a list of files in a directory, you&#8217;ll probably want to stick to scandir() &#8211; so you don&#8217;t have to go messing with the working directory. </p>
<p>For anything else &#8211; like a selective list of files &#8211; you should make use of glob&#8217;s matching functionality rather than sorting through the filenames yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/06/php-directory-functions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Combinations: General Function to Get All Possible Combinations</title>
		<link>http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/</link>
		<comments>http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 00:15:54 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[combinations]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[probability]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/</guid>
		<description><![CDATA[Previously, we looked at how to calculate a binomial coefficient (to find the total number of possible combinations) and an example of how to generate a list of combinations with hard-coded loops. A function would be much more useful to us if it was flexible &#8211; if it could generate a list of combinations for [...]]]></description>
			<content:encoded><![CDATA[<p>Previously, we looked at <a href="http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/">how to calculate a binomial coefficient</a> (to find the total number of possible combinations) and an example of <a href="http://www.earn-web-cash.com/2008/02/26/get-all-combinations/">how to generate a list of combinations with hard-coded loops</a>.</p>
<p>A function would be much more useful to us if it was flexible &#8211; if it could generate a list of combinations for any size set of numbers.  We can&#8217;t do this with the hard coded loop solution, so we need to come up with a better method.<br />
<span id="more-163"></span></p>
<h4>The Function Concept</h4>
<p>Logically, the best way to do this is to create a function that takes a set of numbers (one combination), the size of the set being chosen from (x in [xCy]), and the size of the set being chosen (y in [xCy]).  The function would then use a sequential pattern to calculate the next possible combination and return it.</p>
<p>With this type of function, we could create a while loop that iterates through the whole set of possible combinations.  We initialize it with the first possible combination, continue to execute while the function returns a combination, and finish when the function returns 0 &#8211; in which case there are no more combinations.</p>
<p>If we&#8217;re finding the possible combinations for (11 Choose 4), the main loop would look something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$combo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$choose</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$combo</span> <span style="color: #339933;">=</span> getNextCombo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  Process the combo here</span>
  <span style="color: #666666; font-style: italic;">//  Store it in a file, an array, or something</span>
  <span style="color: #666666; font-style: italic;">//  Or just out put it</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$number</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This initializes an empty array &#8211; $combo.  Our function takes the array and populates it with the next possible combination.</p>
<p>If the array is empty, it&#8217;ll create the first combination &#8211; {1, 2, 3, 4}.  Otherwise, it will increment that set by one to get the next possible combination set &#8211; {1, 2, 3, 5}.  When the function receives a combination set it can&#8217;t possibly increment &#8211; {8, 9, 10, 11} &#8211; it returns false and the loop ends.</p>
<h4>Ok&#8230; How Do We Build the Function?</h4>
<p>Let&#8217;s start by creating the function, checking for an empty array, and returning the initial set.  Then we can dive into how we go about incrementing one combination set to get the next one.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getNextCombo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Return the first set, i.e. {1, 2, 3, 4}</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$choose</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$combo</span><span style="color: #339933;">;</span>    
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Here we'll calculate the next combination</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//  If we didn't return before, no combination was found</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That should be pretty self-explanatory.  Now we can look at our pattern and develop a method of calculating the next possible combination.</p>
<h4>The Pattern</h4>
<p>In the last article on <a href="http://www.earn-web-cash.com/2008/02/26/get-all-combinations/">getting combinations</a>, we identified the basic pattern to follow.</p>
<ul>
<li>Find the &#8220;deepest&#8221; number that can be incremented</li>
<li>Increment it by one</li>
<li>Set any &#8220;deeper&#8221; numbers to one plus the previous number</li>
</ul>
<p>The &#8220;depeest&#8221; number would be the last digit.  In the set {1, 2, 3, 4}, the fourth position would be considered deepest.  Here&#8217;s the first step &#8211; and the most basic increment to do.</p>
<pre>1 2 3 4
1 2 3 5</pre>
<p>The fourth position, &#8217;4&#8242;, is the deepest.  It can be incremented.  We increment it by one.  There are no deeper numbers, so we&#8217;re done.</p>
<p>Let&#8217;s say the fourth position is maxed out.  What happens?</p>
<pre>1 2 3 11
1 2 4 5</pre>
<p>We move back on position to the third position.  This one, &#8217;3&#8242;, is the deepest number that can be incremented.  We increment it by one to 4.  Then we go down the line and set each number after it to one more than the previous.  In this case we only have to set the fourth digit to one more than the third, which is 5.</p>
<p>Let&#8217;s take a look at one more example.</p>
<pre>1 2 10 11
1 3 4 5</pre>
<p>Same thing here.  We move back to the second digit to find one we can increment.  We increment it.  We then drag the others back to be one greater than it.</p>
<h4>Writing This in Code</h4>
<p>The following code should go in the <code>else { }</code> area of the function above.</p>
<p>The first thing we need to do is find the deepest number that can be incremented.  The deepest digit can&#8217;t be incremented when it equals $total.  The second deepest digit can&#8217;t be incremented when it equals ($total &#8211; 1).  The next one at ($total &#8211; 2), then ($total &#8211; 3), etc.</p>
<p>This loop puts that pattern into practice.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$total</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#41;</span> 
             <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$moveBack</span><span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>At the end, $moveBack will hold the number of spaces from the end of the deepest digit.  If $moveBack == 0, then the deepest digit can be incremented.  If $moveBack == 1, we have to move up one digit to increment.  So on and so forth.</p>
<p>If $moveBack == $choose, we can stop counting.  That means that no more digits can be incremented &#8211; we&#8217;ve counted all the way back to digit #0.</p>
<h4>Incrementing to the Next Set</h4>
<p>Now that we know which digit can be incremented, we can increment the whole set and return the array.</p>
<p>First, we should check if $moveBack == $choose (to see if we&#8217;re done).  If it doesn&#8217;t, we start incrementing.</p>
<p>We start by incrementing $combo[$choose - $moveBack].  Then we loop through, decrementing $moveBack until it is 0 &#8211; and we update the deepest digit.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  Increment the deepest digit that can be incremented</span>
  <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//  Loop through and set all other numbers based on this</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$moveBack</span><span style="color: #339933;">--;</span>
    <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>			
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//  $combo now holds the next combination</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$combo</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  We couldn't increment.  The set passed</span>
  <span style="color: #666666; font-style: italic;">//    to the function was the last possible set.</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>And&#8230; We&#8217;re Done</h4>
<p>If you put all the pieces together correctly, you should now have a function that iterates through all of the possible combinations given a binomial coefficient.</p>
<p>If you were handling large sets (millions of possible combinations), you&#8217;d probably want to re-write this in a compiled language.  However, the logic is simple and based simply on arrays &#8211; so it should be easily portable to any language.</p>
<p>A word of warning about storing the information.  Don&#8217;t try to store all of the possible combinations in a giant array.  It&#8217;ll choke up all your RAM and eventually kill the script.  I forget how many combinations I tried to calculate that way, but PHP maxed out at 130mb of RAM and died.</p>
<p>Your best bet would be to write the combinations to a data file or a database as you go.  This dumps the storage requirements into a more capable format.</p>
<p>So what do you do with this?  I&#8217;m not really sure.  I wrote the script for a guy who was working on a statistical program he was using for his doctoral thesis.  I&#8217;m sure there are other specialized purposes for the script &#8211; statistics and probability.</p>
<p>If nothing else, it&#8217;s an interesting look at how to write a computer program to tackle a complex math problem.</p>
<p>Here&#8217;s the full source of the script, if you had trouble putting the pieces together.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getNextCombo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Return the first set, i.e. {1, 2, 3, 4}</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$choose</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$combo</span><span style="color: #339933;">;</span>    
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$moveBack</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$total</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#41;</span> 
             <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$moveBack</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Increment the deepest digit that can be incremented</span>
    <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//  Loop through and set all other numbers based on this</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$moveBack</span><span style="color: #339933;">--;</span>
      <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>			
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">//  $combo now holds the next combination</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$combo</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  We couldn't increment.  The set passed</span>
    <span style="color: #666666; font-style: italic;">//    to the function was the last possible set.</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>	
  <span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">//  If we didn't return before, no combination was found</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$combo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$choose</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$combo</span> <span style="color: #339933;">=</span> getNextCombo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  Process the combo here</span>
  <span style="color: #666666; font-style: italic;">//  Store it in a file, an array, or something</span>
  <span style="color: #666666; font-style: italic;">//  Or just out put it</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$number</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

