<?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>Greymass - Exploring Everything &#187; Operators</title>
	<atom:link href="http://www.greymass.com/tag/operators/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.greymass.com</link>
	<description>Web Development, Design, PHP and random musings</description>
	<lastBuildDate>Wed, 25 Aug 2010 16:22:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>PHP Basics &#8211; The Operators (Part 2)</title>
		<link>http://www.greymass.com/php-basics-the-operators-part-2/</link>
		<comments>http://www.greymass.com/php-basics-the-operators-part-2/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 18:59:20 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[algebra]]></category>
		<category><![CDATA[comparison operators]]></category>
		<category><![CDATA[Operators]]></category>
		<category><![CDATA[simple logic]]></category>

		<guid isPermaLink="false">http://jesta.us/?p=72</guid>
		<description><![CDATA[In our first section, we talked about the multiple ways that you can use the equals sign to assign variables or compare them, based on value and type. In part 2 of this series, we are going to dive deeper into the comparison operators, and exactly how they function. There is a lot of basic [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.greymass.com/php-basics-the-operators-part-1/">our first section</a>, we talked about the multiple ways that you can use the equals sign to assign variables or compare them, based on value and type. In part 2 of this series, we are going to dive deeper into the comparison operators, and exactly how they function. There is a lot of basic programming concepts here, but where better to start than the basics.</p>
<p><span id="more-72"></span><br />
Starting off, we will look at the greater than and less than operators, which are going to be extremely simple and to the point.</p>
<pre name="code" class="php">
$a = 1;
$b = 2;

$result = $a > $b;
echo "a > b = ";
echo $result;
echo "<br/>";
$result = $a < $b;
echo "a < b = ";
echo $result;
</pre>
<p>The above lines of code will output that "a > b = false" (false doesn't echo), and "a < b = true". What does this mean? It means that the values returned by greater than and less than statements are either true or false. The above would be an extremely strange way of finding your results, so I am going to use a more common example next.</p>
<pre name="code" class="php">
 if($a > $b) {
   echo "a is greater than b";
 } else {
   echo "a is less than b";
 }
</pre>
<p>Essentially what you are saying, is IF <strong>A</strong> is greater than <strong>B</strong>, then display the echo, else move on down to the bottom portion. Pretty simple, so moving on, here are two new operators.</p>
<pre name="code" class="php">
 if($a >= $b) { }
 if($a <= $b) { }
</pre>
<p>It's just what it looks like, greater than <strong>OR</strong> equal to. Now your mixing and matching operators, stating maybe that you want something above/equal to, or less than/equal to. It's extremely simple logic/algebra here, just to check the values of these variables.</p>
<p>Onto a little something more complicated, the <strong>not</strong> operator.</p>
<pre name="code" class="php">
 $a = 1;
 $b = 2;
 if($a != $b) { echo "not equal"; }
</pre>
<p>The exclamation point in front of the equals means we are making sure they are <strong>not</strong> the same value. This is extremely useful when you are building something that cannot have identical values. The "!=" statement checks the two variables to make sure they are not equal to each other, but does not care whether or not they are the same data type. Get where this is going from the "triple equals" from the first article?</p>
<pre name="code" class="php">
 $a = 1;
 $b = "1";
 if($a !== $b) { echo "not equal or not the same type"; }
</pre>
<p>By using the single exclamation and the double equals, it is now also checking the variable types to make sure they are not the same. Even though technically both of the variables are equal to 1, $b is a string, and $a is an integer, so this statement will return and tell you they are "not equal or not the same type".</p>
<p>Well, these are all the basic operators. The next set of operators won't come up until the more advanced articles, where we will talk about Ternary Operators.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.greymass.com/php-basics-the-operators-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Basics &#8211; The Operators (Part 1)</title>
		<link>http://www.greymass.com/php-basics-the-operators-part-1/</link>
		<comments>http://www.greymass.com/php-basics-the-operators-part-1/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 17:09:48 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[Operators]]></category>

		<guid isPermaLink="false">http://jesta.us/?p=20</guid>
		<description><![CDATA[Coming into PHP fresh, you may notice a few things that don&#8217;t exactly line up with some of the other programming languages out there. In this initial &#8220;Operators&#8221; post, I am going to talk a little bit about the different operators that are available for people programming in PHP. There are different ways to compare [...]]]></description>
			<content:encoded><![CDATA[<p>Coming into PHP fresh, you may notice a few things that don&#8217;t exactly line up with some of the other programming languages out there. In this initial &#8220;Operators&#8221; post, I am going to talk a little bit about the different operators that are available for people programming in PHP. There are different ways to compare and assign variables, as well as the reasons you might want to use each of these. It&#8217;s a good springboard or starting point for starting to understand PHP.</p>
<p><span id="more-20"></span><br />
<strong>The Equals (=) Operator</strong></p>
<p>The primary role of the equals operator I would consider is setting variables. The single equals role is to set the variable on the left, to the value on the right.</p>
<pre name="code" class="php">$foo = "bar";</pre>
<p>This will set the variable named $foo to contain a string value of &#8220;bar&#8221;. Simple eh?</p>
<p>Well, now what happens if you double up your equals?</p>
<pre name="code" class="php">$foo == "bar";</pre>
<p>It no longer will set the variable $foo equal to &#8220;bar&#8221;, instead what it is doing is comparing the variable on the left to the value on the right. So really, the above line of code is totally impractical, because really it isn&#8217;t doing anything. Lets write something that makes a little bit more sense.</p>
<pre name="code" class="php">if($foo == "bar") {
  echo $foo;
}
</pre>
<p>So the &#8220;IF Statement&#8221; above now is a better idea of exactly what is going on here. The statement is saying:</p>
<blockquote><p>&#8220;IF <strong>$foo</strong> is set to &#8216;bar&#8217;, lets display the value of <strong>$foo</strong> on the page.&#8221;</p></blockquote>
<p>The Double Equals expression doesn&#8217;t care what type of variable $foo is, as its moving through, which leads to the next point. The Triple Equals operator. This ones a little bit trickier and is very, very specific. In the following example, we are no longer going to use a string for the value of $foo, but instead use a number as a string and as an integer.</p>
<pre name="code" class="php">
Example 1:
$foo = 1;

if($foo === 1) {
  echo $foo;
}

Example 2:
$foo = "1";

if($foo === 1) {
  echo $foo;
}
</pre>
<p>Example #1 will display the value of $foo, while Example #2 will not.</p>
<p>The two above samples are almost identical, except for the fact that the value of $foo in Example #2 is encased in quotes, telling it that the variable is a string instead of an integer. The Triple Equals Operator not only compares the values of the variable, but it also compares the <strong>type</strong> of variable being passed in. This is extremely useful when you have code that will only function against a specific type of variable.</p>
<p>One last thing, and this is veering off topic a little bit, but you can cast the variables in the above statement if your really want to use quotes.</p>
<pre name="code" class="php">
$foo = (int) "1";

if($foo === 1) {
  echo $foo;
}
</pre>
<p>By placing the <strong>(int)</strong> in front of the value you are setting, you are casting that variable as a specific type. It&#8217;s useful when you want to specifically set a variable as well, but you might have some problems between variable type conversions if you&#8217;re always casting every variable.</p>
<p>But that&#8217;s a topic for another day, another post. Perhaps the next operator post, we will do some &#8220;not&#8221; functionality.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.greymass.com/php-basics-the-operators-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

