<?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; programming</title>
	<atom:link href="http://www.earn-web-cash.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Which PHP Logical Operator Is Better &#8211; &#8216;and&#8217; or &#8216;&amp;&amp;&#8217;?</title>
		<link>http://www.earn-web-cash.com/2008/02/16/php-logical-operator/</link>
		<comments>http://www.earn-web-cash.com/2008/02/16/php-logical-operator/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 20:07:47 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Sundry Musings]]></category>
		<category><![CDATA[logical]]></category>
		<category><![CDATA[operator]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[speed]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/16/php-logical-operator/</guid>
		<description><![CDATA[I was browsing the Sitepoint forum today and somebody raised this question &#8211; is it better to use &#8216;and&#8217; or &#8216;&#038;&#038;&#8217; as a logical operator in PHP? In most cases, they evaluate the same way. For example, these two statements are equivalent. if &#40;$x &#38;&#38; $y&#41; &#123; ... &#125; if &#40;$x and $y&#41; &#123; ... [...]]]></description>
			<content:encoded><![CDATA[<p>I was browsing the Sitepoint forum today and somebody raised this question &#8211; is it better to use &#8216;and&#8217; or &#8216;&#038;&#038;&#8217; as a logical operator in PHP?  </p>
<p>In most cases, they evaluate the same way.  For example, these two statements are equivalent.</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;">$x</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> and <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>So what&#8217;s the difference?<br />
<span id="more-119"></span></p>
<h5>&#038;&#038; vs And &#8211; Is One Quicker?</h5>
<p>Initially, I thought there may have been a speed factor.  &#038;&#038; and || are the traditional logical operators &#8211; the ones I used when I learned C++.</p>
<p>Therefore, it would be logical to assume that &#8216;and&#8217; and &#8216;or&#8217; are simple conveniences added to PHP.  If these have to be re-routed through &#8216;&#038;&#038;&#8217; and &#8216;||&#8217; to actually evaluate, there would be a slight performance hit if &#8216;and&#8217; was used instead of &#8216;&#038;&#038;&#8217;.</p>
<p>I did some benchmark testing to see if this turned out to be true, and there was no appreciable difference in execution time.</p>
<p>I designed a script (<a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/log.txt' title='Logical Operator Benchmark Test Results'>here&#8217;s the source code if you want to try it yourself</a>) to test &#8216;&#038;&#038;&#8217; verse &#8216;and&#8217;.</p>
<p>The basic timed task is to compare a set of numbers.  In order to ensure that each time the script executed it did the same comparisons, I hard coded five sets of numbers.</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: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This was executed 100,000 times and timed.  The time was recorded and the entire script was run over 100 times.  At the end, an average was taken.  The script was then modified to replace &#8216;&#038;&#038;&#8217; with &#8216;and&#8217; in the statement, and an average of 100 times was taken again.</p>
<p>The results showed no appreciable difference between using &#8216;&#038;&#038;&#8217; and &#8216;and.&#8217;  Using &#8216;&#038;&#038;&#8217; took an average of 0.152 seconds, while &#8216;and&#8217; took an average of .155 seconds.  At a little under 2% variation, I doubt that&#8217;s statistically significant.</p>
<p>I did notice that there were a few more outliers in the &#8216;and&#8217; set &#8211; a couple times were slightly faster and a couple times were slightly longer than any of the &#8216;&#038;&#038;&#8217; times.</p>
<p>Regardless, it appears that there&#8217;s no appreciable difference in execution time or efficiency between &#8216;and&#8217; and &#8216;&#038;&#038;&#8217;.</p>
<h5>Precedence in Evaluation</h5>
<p>The only difference between the two operators, then, turns out to be their <a href="http://us3.php.net/manual/en/language.operators.php#language.operators.precedence">precedence</a>.</p>
<p>As the documentation describes, &#8220;&#038;&#038;&#8221; has greater precedence than &#8220;and&#8221;.  This is somewhat misleading.  An example and some more accurate wording would help illustrate what this means.</p>
<p>This is the example in the documentation.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$g</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// $g will be assigned to (true &amp;&amp; false) which is false</span>
<span style="color: #000088;">$h</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span> and <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// $h will be assigned to true</span></pre></div></div>

<p>The level of precedence between &#8216;&#038;&#038;&#8217; and &#8216;and&#8217; is rather irrelevant here.  However, they both have different levels of precedence relative to the &#8216;=&#8217; operator.</p>
<p>&#8216;&#038;&#038;,&#8217; like most operators, is evaluated before the &#8216;=&#8217; operator.  Therefore, the entire expression <code>true &#038;&#038; false</code> is evaluated and stored in $g.</p>
<p>&#8216;and&#8217; on the other hand is evaluated <strong>after</strong> the &#8216;=&#8217; operator.  Therefore &#8216;true&#8217; is stored in $h and <strong>then</strong> the expression <code>$h and false</code> can be evaluated.</p>
<h5>Is There a Lesson Here?</h5>
<p>Yes.  Precedence of operators in PHP can be a bit strange.</p>
<p>Normal mathematical operations are normal enough.  As expected, multiplication and division are evaluated before plus and minus.  The PHP developers remembered the order of operations from middle school math.</p>
<p>However, other operators (like these logical operators) can have unpredictable consequences.  Instead of relying on PHP&#8217;s hard-coded levels of precedence, you should use parentheses often to make sure that your logic is being evaluated the way <strong>you</strong> want it to be.</p>
<p>For example, you could fix the &#8216;&#038;&#038;&#8217; &#8211; &#8216;and&#8217; discrepancy with a set of parentheses.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$g</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$h</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span> and <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now we have explicitly told PHP to evaluate the &#8216;and&#8217;/'&#038;&#038;&#8217; expression first and <strong>then</strong> assign a value to our variable.  Likewise, you could use a set of parantheses around <code>$h = true</code> to revert to the other order of evaluation.</p>
<p>While you may understand these obscure rules, not everyone who comes along will.  To prevent confusion in the future and needless headaches, tell PHP how you want expressions to be evaluated.  Don&#8217;t let it decide for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/16/php-logical-operator/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Five PHP Coding Mistakes to Avoid</title>
		<link>http://www.earn-web-cash.com/2008/02/04/php-coding-mistakes/</link>
		<comments>http://www.earn-web-cash.com/2008/02/04/php-coding-mistakes/#comments</comments>
		<pubDate>Tue, 05 Feb 2008 01:06:21 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Sundry Musings]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/04/php-coding-mistakes/</guid>
		<description><![CDATA[While browsing through dZone the other day, I stumbled on this article &#8211; 40 signs you really are a lousy PHP programmer. While I don&#8217;t agree with all of the author&#8217;s points, it&#8217;s an interesting list of php coding habits to avoid &#8211; but that most of us tend to pick up anyway. Here&#8217;s a [...]]]></description>
			<content:encoded><![CDATA[<p>While browsing through <a href="http://www.dzone.com">dZone</a> the other day, I stumbled on this article &#8211; <a href="http://reinholdweber.com/?p=19">40 signs you really are a lousy PHP programmer</a>.</p>
<p>While I don&#8217;t agree with all of the author&#8217;s points, it&#8217;s an interesting list of php coding habits to avoid &#8211; but that most of us tend to pick up anyway.  Here&#8217;s a list of my five biggest pet peeves.<br />
<span id="more-99"></span></p>
<h5>5.  Output Information Directly in Functions</h5>
<p>This is pretty annoying when it&#8217;s unexpected.  I installed a WordPress plugin (wp_time_since), and I expected it to return a value to me.  I ran into some weird outputting difficulties when I tried to echo the plugin output.</p>
<p>After a few minutes I figured out that the plugin was echo&#8217;ing the output for me &#8211; instead of letting me store it and output it myself.  Give me the information, and let me worry about outputting it.</p>
<h5>4.  Use $_GET When $_POST Would Be Better</h5>
<p>There are appropriate places to use $_GET &#8211; such as search queries and simply fetching information from a database.  However, any time you&#8217;re modifying something in the database you should be using a post method and the $_POST array.</p>
<p>This isn&#8217;t 100% safe since people can still spoof your script with their own form or a simple http header request, but at least it prevents the average user from accidentally putting random stuff in the URL and screwing up your day.  Use the request superglobal arrays wisely.  They&#8217;re divided into two separate arrays for a reason.</p>
<h5>3.  Return HTML, not Data, Strings, or Objects</h5>
<p>This kind of goes along with #5.  It&#8217;d be nice if your function gave me raw output, so that I could style and structure it the way I want to.  If nothing else, I&#8217;d like to wrap the text in my own paragraph tag, in case I want to give it a class.</p>
<h5>2.  Misuse Object Oriented Programming</h5>
<p>For a lot of web-script programmers, OOP is a strange beast.  Why?  Because it&#8217;s new to PHP.</p>
<p>I come from a C++ background, and OOP is pretty natural to me.  Therefore it&#8217;s annoying when OOP is misused and abused.  It&#8217;s not simply a container for functions.  You should appropriately use private/public methods.  It should make some kind of sense, in an &#8220;object&#8221; kind of way.</p>
<p>Otherwise, just stick with procedural programming.  It&#8217;s still a valid way of creating your script &#8211; and it may just be simpler.</p>
<h5>1.  Not Clearly Commenting Code</h5>
<p>I <strong>hate</strong> this.  I really, really hate this.</p>
<p>Code should be clearly commented and documented.  If I&#8217;m wading through the API of a large project, I want a narrative of what the function is doing, what the parameters should look like, and what I&#8217;m getting in the end.  I don&#8217;t have all day to figure out your script &#8211; so tell me what it&#8217;s doing.</p>
<p>Lastly, there&#8217;s one thing he mentions that I really don&#8217;t agree with.</p>
<p>The author suggests that templating engines (like smarty) are a waste of time.  I originally wondered what the purpose of them were, until I used it to build a module for CMS Made Simple.</p>
<p>Templating engines can be very helpful tools to structure and style your output.  They&#8217;re also terrific if you want to break down the developer/designer tasks.  The developer is responsible for fetching and creating the output, and the designer takes the pre-defined Smarty (or template) variables and places them in the template where they need to be.</p>
<p>I can see why some people wouldn&#8217;t like them for small tasks, but they can be immensely helpful.  Work with them for a while before you pass judgement.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/04/php-coding-mistakes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Time Well Spent, Learning Programming</title>
		<link>http://www.earn-web-cash.com/2008/02/01/time-learning-programming/</link>
		<comments>http://www.earn-web-cash.com/2008/02/01/time-learning-programming/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 02:15:21 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Sundry Musings]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/01/time-learning-programming/</guid>
		<description><![CDATA[Browsing through DZone, I stumbled on an interesting illustration - the <a href="http://osteele.com/archives/2008/01/programmers-pyramid">Programmer's Food Pyramid</a>.

The Pyramid illustrates the different activities that someone generally engages in while learning to program.  While looking at the pyramid, I thought, "Do I do all these things?"]]></description>
			<content:encoded><![CDATA[<p>Browsing through DZone, I stumbled on an interesting illustration &#8211; the <a href="http://osteele.com/archives/2008/01/programmers-pyramid">Programmer&#8217;s Food Pyramid</a>.</p>
<p>The Pyramid illustrates the different activities that someone generally engages in while learning to program.  While looking at the pyramid, I thought, &#8220;Do I do all these things?&#8221;<br />
<span id="more-94"></span><br />
For the most part, I do on a daily basis.</p>
<h5>Writing About Code and Writing Code</h5>
<p>I spend a decent amount of timing writing about code &#8211; or thinking about writing about code.  That&#8217;s a lot of what this website is about, and it&#8217;s also why I spend a lot of time on forums like <a href="http://www.daniweb.com">DaniWeb</a> and <a href="http://www.digitalpoint.com">Digital Point</a>.</p>
<p>As a high school teacher, I know all too well that you learn best by teaching.  So I spend a lot of timing writing about what I&#8217;m trying to figure out &#8211; so I can help others learn it, but also so I can figure it out myself.</p>
<p>On the pyramid, &#8220;Writing About Code&#8221; and &#8220;Writing Code&#8221; are separated into two different categories &#8211; but for me they&#8217;re pretty synonymous.  If I spend time writing some code, I usually devote some time to writing about it.  It helps me gain a deeper understanding of what I did.</p>
<h5>Reading, Revising, and Reading About Code</h5>
<p>Likewise, the pyramid divides these into three different tasks &#8211; but I&#8217;d say they mostly occur at the same time for me.</p>
<p>I spend a lot of time trolling around the PHP sections of webmaster forums (like DaniWeb, DigitalPoint, etc).  I like to see what problems other people are having&#8230; and then try to solve them.</p>
<p>In the process, I generally end up reading someone&#8217;s code, fixing errors in it, and searching the internet to solutions to deeper problems.  A small problem in a forum post can easily drive me to learn more about a topic I didn&#8217;t know a lot about.</p>
<p>For example, someone posted a question about using preg_replace().  I never used the function much, because I never had a reason to use it over str_replace().  Instead of directing the person to str_replace() like most other respondents did, I spent some time learning how to use preg_replace() and I fixed the error in the original poster&#8217;s code.</p>
<h5>Reading Environmental Code</h5>
<p>If there&#8217;s one thing I hate, out of all the bits included in the pyramid, it&#8217;s reading environmental code.</p>
<p>For example, I operate another site using CMS Made Simple (<a href="http://www.teachbabel.com">Babel</a>).  I wrote some modules for the website to make it do exactly what I wanted&#8230; and that meant spending a lot of time mucking through the CMS&#8217; source code.</p>
<p>Like most projects, the source code was not well commented or documented.  The code didn&#8217;t follow the highest standards of coding, either, and I found myself wanting to stop and fix little problems in it as I went.</p>
<p>No, I definitely find it an annoying chore to read through someone else&#8217;s code when it&#8217;s not well-written and well-documented.  That&#8217;s part of the reason I always try to make my own code as clear as possible, with documentation throughout.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/01/time-learning-programming/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

