<?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; operator</title>
	<atom:link href="http://www.earn-web-cash.com/tag/operator/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>
	</channel>
</rss>

