<?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; PHP Tutorials</title>
	<atom:link href="http://www.earn-web-cash.com/category/php-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Fri, 16 Jul 2010 17:32:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</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>Moving WordPress: Individual 301 Redirects with PHP</title>
		<link>http://www.earn-web-cash.com/2008/03/23/moving-wordpress-individual-301-redirects-with-php/</link>
		<comments>http://www.earn-web-cash.com/2008/03/23/moving-wordpress-individual-301-redirects-with-php/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 01:51:58 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Redirect]]></category>
		<category><![CDATA[seo]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/23/moving-wordpress-individual-301-redirects-with-php/</guid>
		<description><![CDATA[I recently decided to move part of an old blog to an independent site. In order to maintain the meager search engine and referral traffic that the old pages had, I planned on using 301 Redirects. I figured that I would re-create each of the old articles (~10 total articles) on the new site and [...]]]></description>
			<content:encoded><![CDATA[<p>I recently decided to move part of an <a href="http://walkere.frih.net/issues">old blog</a> to an <a href="http://rolling-horde.net">independent site</a>.</p>
<p>In order to maintain the meager search engine and referral traffic that the old pages had, I planned on using 301 Redirects.  I figured that I would re-create each of the old articles (~10 total articles) on the new site and set up an individual 301 Redirect to send users and search engine spiders to the new location.</p>
<p>The problem, however, was that the standard .htaccess rewrite that WordPress uses was conflicting with the 301 Redirects.  For some reason, they just weren&#8217;t working &#8211; so I turned to a PHP-based solution.<br />
<span id="more-180"></span></p>
<h4>301 Redirects In htaccess</h4>
<p>Normally, you can handle a 301 Redirect with a .htaccess file.  You can do this with a single line in your .htaccess file.</p>
<pre>redirect 301 /path/to/old/file.html http://newdomain.com/file.html</pre>
<p>The first part &#8220;redirect 301&#8243; indicates the <strong>type</strong> of redirect.  This tells the browser (or the search engine spider) that the page has <strong>permanently</strong> moved to the new location.  HTTP has several other options.  The 302 Redirect, for example, indicates that the move is <strong>temporary</strong>.</p>
<p>The next two parts indicate the old path to the file and the URL of the new location.</p>
<p>Normally, that&#8217;s all there is to it.  However, WordPress adds its own layer to the .htaccess file which makes your pretty URLs work.  The URL that you request is really just a bunch of variables that tell WordPress what article to pull out of the database and display in the template.</p>
<p>Although you should be able to get the two to work together, I couldn&#8217;t.  The server was ignoring my 301 Redirect and going directly to the other rewrite rules.</p>
<h4>Using PHP to 301 Redirect a WordPress Page</h4>
<p>The solution?  PHP.</p>
<p>Creating a 301 Redirect in PHP is fairly simple.  I&#8217;d read about it before, and just the other day I came across a <a href="http://www.electrictoolbox.com/php-301-redirect/">brief article about it</a> in one of my regular RSS feeds.</p>
<p>You use the header() function to send custom headers to the user&#8217;s browser.  With these, you can tell the browser that you want to initiate a 301 Redirect.  You can then tell the browser where to go to find the article.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;HTTP/1.1 301 Moved Permanently&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: http://newdomain.com/article.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Simply replace the URL with the URL you want to redirect to.</p>
<h4>Making It Work With WordPress</h4>
<p>If you look in your site&#8217;s directory structure, you may realize that the path to your article doesn&#8217;t really exist.</p>
<p>Let&#8217;s say we want to redirect the article located at <a href="http://www.earn-web-cash.com/2008/03/20/useful-wordpress-plugins/">http://www.earn-web-cash.com/2008/03/20/useful-wordpress-plugins/</a>.  To use PHP, we&#8217;d need to create a PHP file to replace the article that is being redirected, and include the snippet of code above.</p>
<p>The problem is that the directory /2008/03/20/useful-wordpress-plugins/ doesn&#8217;t exist.  Those are parameters sent to the WordPress script to fetch information out of the database.</p>
<p>To make this type of redirect work, we need to <strong>create</strong> this directory structure.  Start at your root, and create the directory &#8220;2008.&#8221;  Create a directory &#8220;03&#8243; inside that.  Create a directory &#8220;20&#8243; inside that.  Finally, create the directory &#8220;useful-wordpress-plugins&#8221; inside that.</p>
<p>You&#8217;ll notice that there is a trailing slash in the URL.  In a normal directory structure, &#8220;useful-wordpress-plugins&#8221; is a directory, even though it would seem to represent the file where your article resides.  To get the browser to access a PHP file at this location, we place an index.php file in that directory.  Then, accessing the URL will automatically pull up index.php.</p>
<p>In short, create /2008/03/20/useful-wordpress-plugins/index.php, add the snippet of code above, and your redirect should work flawlessly.</p>
<p>This is a good way to move a few articles on a site to a new site.  If you&#8217;re moving an entire site, you&#8217;d definitely want to look into using the .htaccess method, or creating a PHP file that dynamically chooses what page to redirect the user to.  I only had to move 10 articles, so I created ten php files to do the work for me.</p>
<p>There is one caveat to keep in mind.  If your installation of WordPress uses the URL http://www.earn-web-cash.com/2008 to access the yearly archives, you&#8217;ll run into a problem.  /2008 will now be an actual directory &#8211; so the server will try to serve up the directory.  You&#8217;ll need to change your settings so that the archives are located in something like http://www.earn-web-cash.com/archives/2008.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/23/moving-wordpress-individual-301-redirects-with-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to Time Delay Posts in WordPress</title>
		<link>http://www.earn-web-cash.com/2008/03/08/time-delay-posts-wordpress/</link>
		<comments>http://www.earn-web-cash.com/2008/03/08/time-delay-posts-wordpress/#comments</comments>
		<pubDate>Sat, 08 Mar 2008 17:06:28 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/08/time-delay-posts-wordpress/</guid>
		<description><![CDATA[One reason I&#8217;ve heard people say that they prefer a full-blown CMS to WordPress is that WordPress doesn&#8217;t allow time delayed posting. I was about to figure out a way to mod WordPress to make this possible &#8211; until I realized that there is some decent built-in functionality for time delaying. You can easily delay [...]]]></description>
			<content:encoded><![CDATA[<p>One reason I&#8217;ve heard people say that they prefer a full-blown CMS to WordPress is that WordPress doesn&#8217;t allow time delayed posting.  I was about to figure out a way to mod WordPress to make this possible &#8211; until I realized that there is some decent built-in functionality for time delaying.</p>
<p>You can easily delay the release of your posts into the main indexes (front page, archives, &#8220;Recent Posts&#8221; list, etc).  With a little bit of code, you can also restrict people from viewing the page before its appointed &#8220;Published&#8221; date.<br />
<span id="more-173"></span></p>
<h4>Delaying Publication: Setting a Date</h4>
<p>If all you want to do is delay the date and time that an article is &#8220;released&#8221; on your page&#8217;s index, you don&#8217;t have to do a whole lot of work.</p>
<p>Every article you post is &#8220;stamped&#8221; with a time by WordPress.  When you hit the &#8220;Publish&#8221; button, it checks the current time on the server and attaches that to your post.</p>
<p>You may not have noticed, but there&#8217;s an option for you to edit this time while you are working on the article.  For example, you could set the date back a week and backdate one of your posts.  For our purposes, we&#8217;ll set the date <strong>ahead</strong> &#8211; and WordPress won&#8217;t display your post in any indexes until that date arrives.</p>
<p><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/03/edit-timestamp-ss.png' title='Screenshot of the Edit Timestamp Widget'><img class='alignright' src='http://www.earn-web-cash.com/wp-content/uploads/2008/03/edit-timestamp-ss.thumbnail.png' alt='Screenshot of the Edit Timestamp Widget' /></a>In the &#8220;Write&#8221; section of your Dashboard, you should have a column of widgets on the righthand side (Categories, Discussion, Post Slug, etc).  If you scroll down a bit, there should be one titled &#8220;Post Timestamp.&#8221;  If you don&#8217;t see anything, click the &#8220;+&#8221; button in the title.</p>
<p>To set a custom release date, select &#8220;Edit Timestamp.&#8221;  Then use the inputs to enter the date and time you want the article released.  Voila &#8211; you&#8217;re done.</p>
<h4>Restricting Access to the Page</h4>
<p>For most people, this is good enough.  Your readers aren&#8217;t going to be able to find the article without a link to it appearing on your front page or in your RSS feed.</p>
<p>However, if your readers <strong>do</strong> know the link to the page, somehow, they can still access it directly.  If you want to prevent that, you&#8217;ll need to add a few lines of code to your template files.</p>
<p>We&#8217;re going to edit the &#8220;single.php&#8221; template file &#8211; the file responsible for formatting and displaying a single post.  After the page retrieves the post, we&#8217;re going to perform a check to see if the published date of the article is in the future.</p>
<p>If it is, we&#8217;ll display an alternative message to tell the user the article isn&#8217;t published yet.  Otherwise, the article will be displayed as normal.</p>
<h4>Editing Single.php</h4>
<p>Your template file probably doesn&#8217;t look exactly like mine, but there should be some very basic similarities.</p>
<p>If you open single.php, the page is displayed within &#8220;the loop.&#8221;  It&#8217;s how every WordPress template works.  The loop always begins with a line similar to this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This checks to see if a page was found in the database.  If it was, it loads the page into memory and prepares to output it.  The loop will eventually end with a line like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>We&#8217;re going to create a new if statement and place it in between these two lines.  The new template will look something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pubDate</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;h3&gt;Article Not Published Yet&lt;/h3&gt;
  &lt;p&gt;Sorry!  The article you request is not published 
    yet.  It should be available soon, so check back in a few days.&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  ...  Regular post processing goes here ...
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>Creating the Conditional Statement</h4>
<p>The only thing we have to do now is create a functioning conditional statement that checks to see if the published date of the article is in the past or the future.</p>
<p>To do that, we&#8217;ll use a few functions &#8211; get_the_time (a WordPress function to get the post&#8217;s timestamp), strtotime (a PHP function to convert a date string to a timestamp), and time (a php function to get the current tie).</p>
<p>get_the_time doesn&#8217;t return the actual time stamp &#8211; it returns a formatted date string, like &#8220;March 9, 2008, 11:13:35.&#8221;  You can specify how it&#8217;s formatted, just like you would with PHP&#8217;s date function.</p>
<p>To get the timestamp, we&#8217;re going to format the time string in a special way so that strtotime() can convert it back to a full timestamp (preserving the exact time the article should be published).  Then we compare that timestamp to the system time and see if the date has passed or not.</p>
<p>Use this if statement in the code above, and you should be good to go.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span>get_the_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'F j, Y, H:i:s'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If you time delay an article and someone tries to access the article directly, they will now see your alternate message.  You should of course personalize that to something appropriate for your site.</p>
<p>You can also apply the same concept to &#8220;Pages&#8221; in WordPress.  You&#8217;ll need to edit the page.php template file and insert the same snippet of code there.</p>
<p>What are you waiting for?  Go write some articles for the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/08/time-delay-posts-wordpress/feed/</wfw:commentRss>
		<slash:comments>5</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>0</slash:comments>
		</item>
		<item>
		<title>Add Custom Loops (Queries) to WordPress</title>
		<link>http://www.earn-web-cash.com/2008/03/02/wordpress-custom-loops-queries/</link>
		<comments>http://www.earn-web-cash.com/2008/03/02/wordpress-custom-loops-queries/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 21:38:06 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/02/wordpress-custom-loops-queries/</guid>
		<description><![CDATA[If you look at your WordPress template, the thing that drives your page is called &#8220;the loop.&#8221; That&#8217;s WordPress speak for looping through the result set (the posts) retrieved from the database. If you wanted to make custom queries, you could certainly do that by diving straight into the database. However, WordPress comes with some [...]]]></description>
			<content:encoded><![CDATA[<p>If you look at your WordPress template, the thing that drives your page is called &#8220;the loop.&#8221;  That&#8217;s WordPress speak for looping through the result set (the posts) retrieved from the database.</p>
<p>If you wanted to make custom queries, you could certainly do that by diving straight into the database.  However, WordPress comes with some built in functions for helping you query the database and display the results.  By using these template tags, you can add custom loops in your template to display other post information.<br />
<span id="more-167"></span></p>
<h4>Looking At the Main Loop</h4>
<p>If you look in any of your templates, you&#8217;ll see a loop similar to the one below.  This basic loop just loops through all the articles, outputs the title and content.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;h2&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/h2&gt;
&lt;div&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>WordPress has a nasty habit of using that weird while/if syntax, where things aren&#8217;t enclosed in brackets.  Aside from the weird sytnax, this is a very simple while loop.</p>
<p>First, if (have_posts()) checks to see if there is any data in the queue to be outputted.  If there is, it starts a while loop that uses the same check &#8211; have_posts() &#8211; to see if it should continue.</p>
<p>The thing that moves this along is the_post().  This loads the next post in the result set into memory and prepares it for outputting.</p>
<p>The next few lines do our basic output.  the_title() is the template tag for outputting the title of the current article, while the_content() outputs the text of the article.</p>
<p>Finally, we wrap up the while loop and the if statement.  If there were more articles in the queue (like on an index/archive page), it would loop through for each article.</p>
<h4>Where Do the Articles Come From?</h4>
<p>When you load a page in WordPress, you use a query string.  The most basic one would be <code>index.php?p=5</code>.  That tells WordPress to load the article where the post id is 5.</p>
<p>You may not realize it if you&#8217;re using pretty URLs, but every URL you request on your blog has one of these query strings built-in.  It tells WordPress to load a list of articles (for an archive, category, or index) or to simply load one article or page.</p>
<p>There&#8217;s nothing stopping you from utilizing this process for other purposes, though.  Rather than simply looping through the result-set created by the query string, you can use the query_posts() function to create your own custom queries &#8211; and then loop through them.</p>
<h4>Example Please&#8230;?</h4>
<p>Here&#8217;s how it works.  You call query_posts() with a query string.  That loads the database results and prepares for the loop.  Then, you loop through like we did above to output information on the articles.</p>
<p>Let&#8217;s say you want to fetch the latest article that was tagged with both &#8220;foo&#8221; and &#8220;bar.&#8221;  Here&#8217;s how you do it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">query_posts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tag=foo+bar&amp;showposts=1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//  Create the loop here</span></pre></div></div>

<p>We passed two parameters here.  &#8220;tag=foo+bar&#8221; tells WordPress to fetch articles that have both of those tags.  &#8220;showposts=1&#8243; tells WordPress to only fetch one article.  By default, they&#8217;re sorted by date and time, so we automatically fetch the latest one.</p>
<h4>You Don&#8217;t Always Need Plug-ins</h4>
<p>This is such a great tool because it lets you avoid using some basic plug-ins.  Plug-ins essentially help you perform these extra queries &#8211; like the latest posts, similar posts, etc.  But they can also be restricting if they don&#8217;t do exactly what you want.</p>
<p>By using query_posts() to fetch your own information, you can get WordPress to do exactly what you want.</p>
<p>For example, let&#8217;s say you write movie reviews and you want to display the latest ones in your sidebar.  Rather than look for a plug-in that displays exactly what you want it to, you can tag all of your movie reviews with &#8220;Movie Review&#8221; and query for those specific posts. </p>
<p>Then you can easily create the output you want, rather than the output someone else designed.</p>
<p>This is just a taste of what you can do with query_posts() and custom loops in WordPress.  You&#8217;ll need to try it out for yourself to fully understand it, and I&#8217;d suggest you take a look at the <a href="http://codex.wordpress.org/Template_Tags/query_posts">documentation on query_posts()</a> for a list of parameters and the <a href="http://codex.wordpress.org/Template_Tags/">documentation on template tags</a> for a full list of output tags.</p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/02/wordpress-custom-loops-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Enable Client-Side Caching of Resized Images</title>
		<link>http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/</link>
		<comments>http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 00:37:44 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[resize]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/</guid>
		<description><![CDATA[About a month ago, I wrote a tutorial on dynamically resizing images in PHP. Despite my best intentions, I never found the time to go back and rewrite the script to include server-side caching of the images created. However, one reader asked if it was possible to enable a client-side cache of a dynamically resized [...]]]></description>
			<content:encoded><![CDATA[<p>About a month ago, I wrote a tutorial on <a href="http://www.earn-web-cash.com/2008/01/30/resize-images-php/">dynamically resizing images in PHP</a>.  Despite my best intentions, I never found the time to go back and rewrite the script to include server-side caching of the images created.</p>
<p>However, one reader asked if it was possible to enable a client-side cache of a dynamically resized image.  It surely is, and it&#8217;s very simple.<br />
<span id="more-166"></span></p>
<h4>Dynamic Image Resizing: Revisited</h4>
<p>If you&#8217;ve got no clue how to resize an image in PHP, then you ought to go back and re-read the <a href="http://www.earn-web-cash.com/2008/01/30/resize-images-php/">original article</a>.  For the rest of you, here&#8217;s a quick refresher on what happens.</p>
<p>Our script takes a filename as a $_GET parameter.  If the file exists, the script loads it in as an image resource.  Using the GD library, we create a new image resource that is the proper size.</p>
<p>Finally, we use a content-type header to tell the browser to interpret the data as an image and we output the image data.  Like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;content-type: image/png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Enabling a Client Side Cache</h4>
<p>In order to enable a client-side cache, all we need to do is add an extra header to the mix.</p>
<p>There is a set of headers in HTTP to handle this sort of need &#8211; Cache-Control.  You can use this to enable caching, disable caching, and set some restrictions on what can be cached for how long.</p>
<p>The option we&#8217;re most interested in is max-age=[seconds].  This response header is sent along with the data to tell the browser that it can re-use a cached copy as long as it is no older than the specified age.</p>
<p>To enable this with PHP, add another header() call before the imagepng() call, and set a Cache-Control header with an appropriate max-age.  Like so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: max-age=3600&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;content-type: image/png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Keep Request Queries Consistent</h4>
<p>With this system, you could set a pretty long max-age for the cache.  Assuming that the original image never changes, the user could cache a resized copy for months and your script would be happy.</p>
<p>However, you need to keep your query strings consistent for this to work.  Since your request includes a query string, that full string is included in the location that is cached.</p>
<p>According to your browser, it&#8217;s not the image that is cached, it&#8217;s the location&#8230;</p>
<pre>http://www.earn-web-cash.com/scripts/resize-image.php?image=http://www.earn-web-cash.com/wp-content/uploads/2008/01/dzone-screenshot.png</pre>
<p>If you were to change the query string in any way &#8211; leaving out the www, for example &#8211; the browser will think it is fetching a new resource and it won&#8217;t consult the cache.</p>
<p>This is especially important if you include size parameters in the query string to dynamically choose the new size.  You would need to be careful to keep the parameters in the same order every time for the cache to work properly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/feed/</wfw:commentRss>
		<slash:comments>10</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>1</slash:comments>
		</item>
		<item>
		<title>How to Generate a List of Possible Combinations</title>
		<link>http://www.earn-web-cash.com/2008/02/26/get-all-combinations/</link>
		<comments>http://www.earn-web-cash.com/2008/02/26/get-all-combinations/#comments</comments>
		<pubDate>Tue, 26 Feb 2008 23:16:53 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></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/26/get-all-combinations/</guid>
		<description><![CDATA[In statistics and probability, the combination is something special. There are tons of problems that revolve around how many combinations you can get choosing a certain number of random items from a set. We&#8217;ve developed mathematical formulas to help us determine the number of possible combinations &#8211; and therefore the possibility of getting any single [...]]]></description>
			<content:encoded><![CDATA[<p>In statistics and probability, the combination is something special.  There are tons of problems that revolve around how many combinations you can get choosing a certain number of random items from a set.</p>
<p>We&#8217;ve developed mathematical formulas to help us determine the number of possible combinations &#8211; and therefore the possibility of getting any single combination &#8211; but how can we generate a list of the actual combinations?<br />
<span id="more-161"></span></p>
<h4>Find the Number of Combinations: the Binomial Coefficient</h4>
<p>Previously, we looked at <a href="http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/">how to find the total number of combinations</a>.</p>
<p>Mathematically, we can use the binomial coefficient to figure this out.  With a given set size (i.e. 52 cards) and a given sample size (i.e. 7 cards per hand), we can write the equation &#8211; (52 Choose 7).  We could then re-write the equation as (52! / (7! * 45!) ).</p>
<p>In order to calculate this, we found the most efficient way was to construct two loops.  The first loop calculated the numerator &#8211; after having canceled out all of the numbers contained in 45!.  We then calculated the numerator by looping through 7!.  With simple division, we come up with the answer.</p>
<h4>Listing the Actual Combinations</h4>
<p>A more interesting task, though, is to figure out how to generate a list of these combinations.  To do that, let&#8217;s take a look at a manually generated list &#8211; (6 Choose 3) &#8211; and look for a pattern.</p>
<p>Here&#8217;s the first few combinations&#8230;</p>
<pre>123
12 4
12  5
12   6
1 34
1 3 5
1 3  6
1   45
1   4 6
1    56
 234
 23 5</pre>
<p>You&#8217;ll notice that there&#8217;s a pattern and that the combinations seem to be developing sequentially.  If we can understand that pattern and write a script to repeat it, we&#8217;ll have done our job.</p>
<p>Here&#8217;s the gist of the pattern.  We look at the last number.  If we can increment that, we do.  If not, we look back one number.  If we can increment, we do.  If we increment that number, we &#8220;rewind&#8221; the next number to be equal to it plus one.</p>
<p>For example, look at this series.</p>
<pre>12   6
1 34</pre>
<p>We started by incrementing the last number until it maxed out at 6.  Then we moved back one space, incremented that number, and set the other number equal to one greater than that.  The same thing happens when the last two numbers max out.</p>
<pre>1   56
 234</pre>
<p>You move back to the number that can be incremented, increment it, and then set the other numbers to one greater than the number incremented.</p>
<h4>Using Hard-coded Loops</h4>
<p>The easy (and cheating) way to do this is to hard code a series of nested loops that follow this pattern.</p>
<p>The general formula is (k Choose n) &#8211; where k is the total number of items in the set and n is the sample size being taken.  We will need to create <strong>n</strong> nested loops &#8211; represent each item being chosen in our sample set.</p>
<p>The outer loop will go from <strong>1</strong> to <strong>k &#8211; (n &#8211; 1)</strong>.  Why?  Because at <strong>k &#8211; (n &#8211; 1)</strong>, we can&#8217;t increment the outer number anymore.  Imagine the real numbers are (6 Choose 3).  Once the outer loop gets to 4, we won&#8217;t be able to increment it any further &#8211; we&#8217;ll be at the final combination, 4, 5, 6.</p>
<p>The inner loop will have a beginning value that depends on the outer loop.  If the outer loop is in its first iteration, the first inner loop starts at 2.  When the outer loop is in its second iteration, the first inner loop starts at 3.  It&#8217;s always one greater.  It&#8217;s maximum value is always the same &#8211; <strong>k &#8211; (n &#8211; 2)</strong> &#8211; for the same reason we created the upper limit for the outer loop.</p>
<p>This continues until we get to the inner-most loop.</p>
<p>Here&#8217;s what these nested loops might look like in PHP, generating the combinations for (6 Choose 3).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You&#8217;ll see that that script outputs the correct 20 combinations for (6 Choose 3).</p>
<h4>Can We Improve On That?</h4>
<p>Surely we can.  The main problem here is that we&#8217;ve hard-coded the loops.  This is ok for a short-term solution and a small number of items being chosen.</p>
<p>What if you had to choose twenty items from a larger set?  You&#8217;d have to hard-code twenty loops.  It makes the code a bit more unseemly.  This is also inflexible &#8211; you&#8217;ll have to change the code every time you have a different problem.</p>
<p>Instead, we need to look for a generalizable solution.  This is always the best bet, but sometimes it can be tempting to look for a one-off short term solution because it&#8217;s more readily visible.</p>
<p>There is, however, a generalizable solution to this problem.  The key lies in creating a function that finds the next combination using the sequential method outlined above.  It takes a combination and returns a combination &#8211; and you can continuously call the function to iterate through all of the combinations.</p>
<p>Think on that for a while.  It&#8217;s an interesting challenge.  I&#8217;ll explain a <a href="http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/">solution to it later in the week</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/26/get-all-combinations/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to Spoof a Form, or Why Your Form Isn&#8217;t Safe</title>
		<link>http://www.earn-web-cash.com/2008/02/25/spoof-forms-security/</link>
		<comments>http://www.earn-web-cash.com/2008/02/25/spoof-forms-security/#comments</comments>
		<pubDate>Tue, 26 Feb 2008 00:54:03 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spoof]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/25/spoof-forms-security/</guid>
		<description><![CDATA[Forms can create all kinds of security holes in PHP applications. The biggest reason for this is that you can never be sure just what kind of input you&#8217;ll be getting. There are some steps you can take later on to protect your applications from this unknown input. But first, let&#8217;s take a look at [...]]]></description>
			<content:encoded><![CDATA[<p>Forms can create all kinds of security holes in PHP applications.  The biggest reason for this is that you can never be sure just what kind of input you&#8217;ll be getting.</p>
<p>There are some steps you can take later on to protect your applications from this unknown input.  But first, let&#8217;s take a look at <strong>why</strong> you can never trust a user&#8217;s input and just how easy it is for someone to spoof a form.<br />
<span id="more-160"></span></p>
<h4>What Does It Mean to Spoof a Form?</h4>
<p>It could mean a number of related things, but generally speaking it means submitting form information in a manner other than the original form was intended.  The form processing script, completely unaware of the situation, processes the input as if it came from the original form.</p>
<p>For example, if a form generates a URL query string, one way to spoof that form would be to modify the query string and re-visit the URL.  The new page would think that my query came from the original form &#8211; even though I really crafted the query myself.</p>
<p>Why would someone want to do this?  It may be curiosity.  &#8220;I wonder what happens if I change this variable&#8230;&#8221;  Or it may be malicious.  A hacker can spoof a form processing script to send dangerous input to your PHP script.</p>
<h4>The Problem With the &#8220;Get&#8221; Method</h4>
<p>It is easiest to spoof a form if the information is transmitted through the &#8220;Get&#8221; method.  This means that the variables are placed into a query string and appended to the form&#8217;s action URL.</p>
<p>Let&#8217;s say you have a form with three elements &#8211; a textarea element, a hidden input, and a select element.  Logically, you might think that the hidden input is restricted to whatever value you set it to and the select element is restricted to one of the options you provided.</p>
<p>To make this a little more concrete, let&#8217;s assume the textarea contains a post to a multi-user blog, the hidden input contains the author&#8217;s name, and the select element includes the category number.</p>
<p>When the user enters the values and clicks &#8220;submit,&#8221; he&#8217;s sent to a URL that looks something like this.</p>
<pre>processing.php?text=Bla+bla+bla&#038;author=Walkere&#038;catid=2</pre>
<p>What if I wanted to pretend to be someone else &#8211; like Bob?  I could simply change the &#8216;author&#8217; variable.</p>
<pre>processing.php?text=Bla+bla+bla&#038;author=Bob&#038;catid=2</pre>
<p>Maybe I want to post my message in a special category &#8211; one that isn&#8217;t normally open to me.  Again, it&#8217;s as simple as changing the catid variable to another number.</p>
<p>Now, a well designed script will notice these data flukes and either ignore them or spit out an error.  However, if you have too much faith in form input &#8211; and you blindly used the values from your form &#8211; you&#8217;d soon be in a mess of trouble.</p>
<h4>But Post Is More Secure&#8230; Right?</h4>
<p>The &#8220;Post&#8221; method is somewhat more secure.  Why?  Because all of the information is transmitted behind the scenes.</p>
<p>When I was younger, I tried to meddle with the $_GET variables of an online game that I played.  I never would have known enough about the workings of the script to spoof a &#8220;post&#8221; form &#8211; but it was so blatantly obvious with &#8220;get.&#8221;</p>
<p>However, someone &#8220;in the know&#8221; can easily spoof a form using the post method as well.  One way to do this is&#8230; write your own form.</p>
<p>If I look at the HTML source of your form, I can easily see what values you&#8217;re submitting to the processing script.  I can recreate those input elements on a form on <strong>my</strong> site &#8211; and then set the action to your processing script.</p>
<p>In this way, I could make the hidden and select statements regular text inputs &#8211; and input whatever values I want.  If you&#8217;re form processing script isn&#8217;t savvy, my custom values will get passed on to the database.</p>
<p>One thing you can do to prevent this is to check the referring URL coming into your script.  This will prevent someone from simply creating a form off-site to spoof your script.</p>
<h4>Sending an Actual HTTP Request</h4>
<p>The final, and most devious way, to spoof a form is to craft your own HTTP request in PHP.  This is essentially what a form does &#8211; but you can manually craft the request to your liking instead of letting a form do it.</p>
<p>How do we do this?</p>
<p>First, we create a stream context.  To do that we create a simple array and then call the function stream_context_create.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$params</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;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'method'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'headers'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Referer: http://www.google.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// A regular query string with parameters</span>
&nbsp;
<span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_context_create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$processingUrl</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fpassthru</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The actual form values are placed in the $data variable.  This is in a typical query string format &#8211; key=var&#038;key2=var2, etc.  The values should also be url encoded.</p>
<p>The nifty thing here is that you can manually specify HTTP headers to be sent with your request.  In this example, we set the referring URL to Google.  If someone knew your processing script checked the referrer&#8217;s address &#8211; they could simply spoof it with an HTTP header.</p>
<h4>Never Trust Input</h4>
<p>The bottom line is &#8211; never trust input that you haven&#8217;t thoroughly tested, validated, and cleaned.</p>
<p>Even if an input item is supposed to be hidden or supposed to come from a specific set of values &#8211; you can&#8217;t be sure that the form processing script is going to receive the correct value.  The script needs to check the info and make sure that it got what it was looking for.</p>
<p>It&#8217;s pretty simple for someone to spoof your form and send any input they like to your processing script.  If you don&#8217;t close the security holes, then someone will eventually blow them open for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/25/spoof-forms-security/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting Combinations: Calculating a Binomial Coefficient</title>
		<link>http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/</link>
		<comments>http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 20:52:50 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Sundry Musings]]></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/24/combinations-binomial-coefficients/</guid>
		<description><![CDATA[I came across this problem on the DigitalPoint forums the other day. How can we write a script to calculate the total number of random combinations in a set (i.e. the binomial coefficient), and how can we generate a list of these combinations? Today, we&#8217;ll tackle the first part. We&#8217;ll start with a quick primer [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this problem on the DigitalPoint forums the other day.  How can we write a script to calculate the total number of random combinations in a set (i.e. the binomial coefficient), and how can we generate a list of these combinations?</p>
<p>Today, we&#8217;ll tackle the first part.  We&#8217;ll start with a quick primer on math &#8211; what is a binomial coefficient?  Then, we&#8217;ll look into the most efficient way to calculate that and get the total number of possible combinations.<br />
<span id="more-159"></span><br />
<h4>What Is a Binomial Coefficient?</h4>
<p>If it&#8217;s been a while since you took statistics or calculus, you might be scratching your head thinking about what a binomial coefficient is and what it has to do with combinations.</p>
<p>If you&#8217;re looking at a set of random items &#8211; let&#8217;s say 52 playing cards &#8211; and you want to pick out a number of them &#8211; let&#8217;s say a 7 card poker hand &#8211; the binomial coefficient tells you how many possible combinations you can make.</p>
<p>The general formula for that is usually written:</p>
<p><code>(x Choose y) = (x!) / (y! * (x-y)!)</code></p>
<p>In our example above, the total number of combinations you can get in a 7-card poker game is (52! / (7! * 45!)) or somewhere around 130,000,000.</p>
<h4>How Do We Calculate That (in PHP)?</h4>
<p>These methods of calculation will work for any language, really.  We&#8217;ll just use PHP as an example.</p>
<p>The simplest way to calculate the binomial coefficient would be to loop through, calculate each factorial, and then evaluate the equation.  Notice I said simple, not efficient.</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;">52</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  This is the numerator, 52!</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$num</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$denOne</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  This is the first part of the denominator, 7!</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$denOne</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$denTwo</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  This is the second part of the denominator, 45!</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$denTwo</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$combinations</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$denOne</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$denTwo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>PHP will evaluate this properly because it&#8217;s nice and flexible with data types.  Do you know how large 52! is?  Try 8.06581751709E+67 large.</p>
<p>This is clearly too big to be an integer value, but PHP quietly converts the values to floats and keeps running.  There should be a better way &#8211; one where we don&#8217;t have to calculate all of the factorials.</p>
<h4>Method Two &#8211; Using array_diff</h4>
<p>This method is a bit more along the lines of human logic.  However, it&#8217;s still not going to be efficient.  Although it&#8217;ll work with smaller numbers than the first method, it&#8217;ll take more time and memory to compute.</p>
<p>Basically, we&#8217;ll create three arrays.  One for each set &#8211; {1, 2, 3, &#8230; 52}, {1, 2, 3, &#8230; 7}, {1, 2, 3, &#8230; 45}.</p>
<p>Since we know that the third set (45!) will be in the denominator and the first set (52!) will be in the numerator, we can use a nifty function called array_diff to cancel out all of the common values and shrink the numerator for us.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$newNumerator</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_diff</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$oldNumerator</span><span style="color: #339933;">,</span> <span style="color: #000088;">$oldDenominator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then we can calculate the factorials by multiplying all of the values in the array.  We can also make use of another PHP function here &#8211; array_product.  It does more or less what it sounds like &#8211; it finds the product of all the values in an array.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$num</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: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$num</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// the numerator array, 52!</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$denOne</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: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$denOne</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  The first denominator array, 7!</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$denTwo</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: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$denTwo</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// The second denominator array, 45!</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Use array_diff to cancel out the common values in 52! and 45!</span>
<span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_diff</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #339933;">,</span> <span style="color: #000088;">$denTwo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$combinations</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_product</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #990000;">array_product</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$denOne</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This keeps the numbers much smaller.  Therefore we can work with bigger combinations before PHP putts out.  However, the arrays can eat up a lot of memory and processing power&#8230; so there still should be a better way.</p>
<h4>Combine the Two Concepts</h4>
<p>The more efficient way is to combine the two concepts.  We&#8217;ll use the first method of multiplying through a loop to generate the factorials.  This is far more efficient than iterating an array.</p>
<p>However, we&#8217;ll still cancel out the common values between 52! and 45!.  To do this, we modify the first loop so that it counts down from $x (52) to $x &#8211; $y (45).  Then we simply ignore the third loop &#8211; since it&#8217;s values were essentially counted when we calculated the first loop.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$num</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$den</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$den</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$combinations</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$den</span><span style="color: #339933;">;</span></pre></div></div>

<p>At this point, our script is pretty efficient.  It doesn&#8217;t use up a lot of memory, it executes quickly, and it&#8217;s not calculating very many redundant values.</p>
<p>However, there&#8217;s still a limit to what we can do.  The biggest number PHP seems to be able to calculate is 170!.  After PHP sees the value 7.25741561531E+306 it just seems to give up.  Geez, like 300 zeroes is a lot.</p>
<p>You could improve this a bit more, and increase the number of combinations it could compute.  Rather than calculating the $numerator and $denominator separate, as we did here, you&#8217;d want to calculate the $denominator first and then cancel out values as you multiply the numerator.</p>
<p>In other words, calculate the $denominator as normal.  Then, while you&#8217;re looping through to calculate the numerator, you factor out any common values in the $denominator and the number about to be multiplied into the numerator.</p>
<h4>Who Cares About How Many Combinations&#8230; I Want to See Them</h4>
<p>This is really the boring part.  Who cares how many combinations there are?  We can easily find that with a calculator.</p>
<p>The interesting part &#8211; where we definitely need a computer to help us &#8211; is to iterate through the set and find all of the possible combinations.  We&#8217;ll come back to this later in the week.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
