<?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; simple logic</title>
	<atom:link href="http://www.greymass.com/tag/simple-logic/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; Syntax for Dummies</title>
		<link>http://www.greymass.com/php-basics-syntax-for-dummies/</link>
		<comments>http://www.greymass.com/php-basics-syntax-for-dummies/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 19:58:36 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[functionality]]></category>
		<category><![CDATA[simple logic]]></category>
		<category><![CDATA[syntax]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://jesta.us/?p=25</guid>
		<description><![CDATA[There is a lot of PHP specific syntax that every PHP programmer uses in their day to day work, that everyone has just learned that it is the way it is meant to be. But, to truly be able to go through, read someone&#8217;s code and truly understand it, you need to know exactly what [...]]]></description>
			<content:encoded><![CDATA[<p>There is a lot of PHP specific syntax that every PHP programmer uses in their day to day work, that everyone has just learned that it is the way it is meant to be. But, to truly be able to go through, read someone&#8217;s code and truly understand it,  you need to know exactly what the syntax means, and why it&#8217;s formatted that way. Throughout this small post, there are just some thoughts and practices for coding using the PHP syntax.</p>
<p><span id="more-25"></span><br />
<strong>The Semicolon</strong></p>
<p>The basic syntax of PHP, which is the first thing every PHP developer needs to know, is the semicolon. Most lines of code end with a semicolon, which essentially means, to execute the code that has been loaded before this semicolon and after the last one.</p>
<pre class="php">$foo = "bar";
$fooArray = "Bar",
            "This",
            "Something";</pre>
<p>You can break your code into multiple lines in some cases, but technically the code above is still only two lines of code. The first line of code assigns the variable of $foo, and the second line sets an array called $fooArray. Even though the array is broken into three separate lines, it still executes as one line of code.</p>
<p>Notice how still, each line of code ends with a semicolon. You cannot split every line up like this, but when assigning arrays, building long strings, or even working with objects, you can drop down and create a new line. The reasoning behind this will be covered more in the &#8220;Making your code Pretty&#8221; article.</p>
<p><strong>The period as a &#8220;String Connector&#8221;</strong></p>
<p>Some languages use the plus symbol (+), some languages use variations of quotes, and PHP uses the period (.) as a string connector.</p>
<pre class="php">// --- Long Method
echo "Your IP address is: ";
echo $_SERVER['REMOTE_ADDR'];
echo " &lt;br/&gt;";
echo "The current date/time is: ";
echo date("Y-m-d H:i:s");
echo " &lt;br/&gt;";

// --- Short Method
echo "Your IP address is: ".$_SERVER['REMOTE_ADDR']." &lt;br/&gt;";
echo "The current date/time is: ".date("Y-m-d H:i:s")." &lt;br/&gt;";</pre>
<p>The two above examples will yield the exact same results, example #1 not using any string connectors, example #2 joining strings using the period. You can use this syntax to set variables, within functions, or nested inside of loops. Essentially what you are doing is appending to the current string with what is after the period. By joining multiple strings together with the &#8220;something&#8221;.$something.&#8221;something&#8221; method, you can easily assemble long strings without having to write an incredible amount of code.</p>
<p><strong>(Encapsulating with Parenthesis)</strong></p>
<p>This is just like algebra folks&#8230; things within parenthesis will be evaluated before the rest of the equation.</p>
<pre class="php"> 	echo 1 + 6 * (( 8 - 5 ) / 2 );

	// Order of Operations:
	// 8 - 5 = 3
	// 3 / 2 = 1.5
	// 6 * 1.5 = 9
	// 1 + 9 = 10
	// echo 10</pre>
<p>The comment above shows the order of operations, which is identical to that of an algebra equation. Evaluate the values inside the parenthesis first, work your way outwards, then use the standard order.</p>
<blockquote><p><strong>Order of Operations</strong><br />
Source: <a href="http://www.math.com/school/subject2/lessons/S2U1L2GL.html">http://www.math.com/school/subject2/lessons/S2U1L2GL.html</a></p>
<p>1. First do all operations that lie inside parentheses.<br />
2. Next, do any work with exponents or radicals.<br />
3. Working from left to right, do all multiplication and division.<br />
4. Finally, working from left to right, do all addition and subtraction.</p></blockquote>
<p><strong>Brackets and Uses</strong></p>
<p>Brackets, as in [], are primarily used for assigning or calling values from an array.</p>
<pre class="php">	// Example #1
	$array = array("red","green","blue");
	echo $array[0]; // Will display "red"
	echo $array[1]; // Will display "green"
	echo $array[2]; // Will display "blue"

	// Example #2
	$array = array("color1"=&gt;"red");
	echo $array['color1'];</pre>
<p>By default an array will populate its keys with numbers, in the above example, it does just that. When passing values into an array, each value is assigned an auto-incremented number, starting at zero. To reference these values in an array in the most simple fashion, you simply put the key you wish to call on within brackets.</p>
<p>These keys do not have to be numbers, they can be strings as well, and the values of the array can be pretty much any type of variable or object. The second example shows that you can pass in a string within the brackets to pull out the value for that specified key.</p>
<p>For more information on arrays, please see: <a href="http://www.greymass.com/php-basics-the-array/">PHP Basics &#8211; The Array</a>.</p>
<p><strong>What are those &#8220;Curly Brackets&#8221; used for?</strong></p>
<p>The { and }? They are actually called &#8220;Braces&#8221; and are used to define the starting and ending of a structure of code. The most common use of them while starting is to start a loop or if-statement. The &#8220;Open Brace&#8221; or {, signifies we are starting an structure of code that will continue until the &#8220;Close Brace&#8221;, or }. This is easily seen in an if-statement as seem below.</p>
<pre class="php">if($foo == "bar") { // This brace starts the structure
	// Do something!
} // This brace ends it</pre>
<p>The code encased in the braces will only execute if the condition of the if-statement evaluates to TRUE. There are also other methods of starting and stopping an if-statement or loop, but we will wait to get into those later, as to not confuse people.</p>
<p>Braces are also used to define function, class and namespace contents. These are a little further along, but still solid examples.</p>
<pre class="php">	function doWork($foo) {
		// Perform Actions on $foo
		return $bar;
	}

	class foo extends bar {
		private $var = "test";

		public function doWork($var) {
			// Do Work!
			$this-&gt;var = $mod;
		}
	}</pre>
<p>Notice in the second example, there are nested braces within. Each brace has a start and a stop, telling the system which pieces belong in which places. When your code starts getting extremely messy or complex, its always a good idea to keep the braces lined up with the same spacing for ease of reading. Yet another topic for another day.</p>
<p><strong>The Key=&gt;Value Syntax</strong></p>
<p>The last syntax covered in this post is the $key=&gt;$value assignment syntax, which can be a little confusing at first. This is primarily used in arrays and is meant to set specific keys to specific values.</p>
<pre class="php">  $key = "key";
  $value = "value";

	$array = array($key=&gt;$value);
	$array = array("key"=&gt;"value");

	echo $</pre>
<p>The two above examples will both assign the $array variable to contain the same array; a single key named &#8220;key&#8221; will be assigned the value of &#8220;value&#8221;. The =&gt; syntax states that you are using a key=&gt;value combo to pull this off. This can also be used in a foreach() loop to iterate through an array of values, as seen below.</p>
<pre class="php">$array = array("color1"=&gt;"red", "color2"=&gt;"blue");

foreach($array as $key=&gt;$value) {
	echo $key." = ".$value."&lt;br/&gt;";
}

// Outputs:
// color1 = red
// color2 = blue</pre>
<p>The loop will go through every item in the $array variable, and display every key=&gt;value combo within it.</p>
<p>Well, that&#8217;s it for the basics of syntax. Stick around for more info at another time!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.greymass.com/php-basics-syntax-for-dummies/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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>
	</channel>
</rss>

