<?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; variables</title>
	<atom:link href="http://www.greymass.com/tag/variables/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 Array</title>
		<link>http://www.greymass.com/php-basics-the-array/</link>
		<comments>http://www.greymass.com/php-basics-the-array/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 21:16:17 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[variable types]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://jesta.us/?p=60</guid>
		<description><![CDATA[One of the fundamental variable types, yet one of the more complex when you are beginning, is the Array. The array just takes a little bit to wrap your mind around, or maybe a good analogy as to exactly what it is doing. You can pretty much think of an array as a mini-storage unit [...]]]></description>
			<content:encoded><![CDATA[<p>One of the fundamental variable types, yet one of the more complex when you are beginning, is the <strong>Array</strong>. The array just takes a little bit to wrap your mind around, or maybe a good analogy as to exactly what it is doing.</p>
<p>You can pretty much think of an array as a mini-storage unit for variables, someplace to order them, keep them in one space, and reference them by name dynamically. There are more advanced ways to do some of this as well but arrays are a good starting point.</p>
<p><span id="more-60"></span><br />
Just as a starter, here is a basic array, the first code snippet is the PHP to write the array, and the second is a visual representation of the variable.</p>
<pre name="code" class="php"> $variable = array("red","green","blue");</pre>
<pre name="code" class="php">$variable = Array
(
    [0] =&gt; red
    [1] =&gt; green
    [2] =&gt; blue
)</pre>
<p>Now, a brief overview at exactly what you are looking at here, in case you have never worked in an array before. The variable named $variable is an array, and it contains 3 values, &#8220;Red&#8221;, &#8220;Green&#8221; and &#8220;Blue&#8221;. Each of these values now has dynamically been assigned a number on the left hand column. These are the <strong>keys</strong> to access the data in the array.</p>
<p>If you were to make an analogy to understand it better, we could say an array is a book, and each variable in the array is a page. Each page has a page number, and those page numbers would be the keys in the array. Let&#8217;s run with that example, and create a bit more complex of an array, so and pretend our array is indeed a book.</p>
<pre name="code" class="php"> $book = array(
     "The quick brown dog...",
     "Jumped over the big red fence...",
     "To get to his food..."
  );</pre>
<p>Don&#8217;t let the change of how I typed the code throw you off, this is the exact same sample as above, instead we have a &#8220;page&#8221; of the &#8220;book&#8221; stored within each &#8220;page number&#8221;. Notice how the variable looks now:</p>
<pre name="code" class="php">Array
(
    [0] =&gt; The quick brown dog...
    [1] =&gt; Jumped over the big red fence...
    [2] =&gt; To get to his food...
)</pre>
<p>The variable is still pretty much the same, just with different values stored within its keys. So now, if we were going to print out the first value of the array to the screen, we would echo it out, with slightly different syntax than just a standard echo.</p>
<pre name="code" class="php"> echo $book[0];</pre>
<p>You would echo it just like a standard variable, except directly after,  you would use brackets to specify which key you want to retrieve from the array. The above code would output &#8220;The quick brown dog&#8230;&#8221; onto the screen. You can simply change the number at the end of the variable to specify which portion of the array you are looking for.</p>
<p>The above syntax you can also set values in an array with as well!</p>
<pre name="code" class="php"> $book[4] = "This is another page!";
 $book[1000] = "This is WAY out there...";</pre>
<p>These two lines of code will write their values into the key of 4 and key of 1000. There&#8217;s nothing telling you that you have to use numbers in order, or even numbers for that example!</p>
<pre name="code" class="php"> $book['index'] = "This is the index of the book";</pre>
<p>You can store keys with words as well, if that helps you organize how your data is stored. PHP uses a lot of arrays for sessions, server variables and many other things. Here&#8217;s an example of pulling the IP address of your client, from a $_SERVER variable that is automagically set whenever a script on your server is called.</p>
<pre name="code" class="php"> echo $_SERVER['REMOTE_ADDR']; // Will print the IP address accessing the script</pre>
<p>Arrays can be an amazing storage tool to keep your variables in check, instead of assigning every single variable its own name. It also is nice if you want to pass multiple variables into a function or an object&#8217;s method at once. That&#8217;s a little bit more advanced, and a subject for another time, but just understanding the basics of how an array looks, visually and in concept, will take you a long ways.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.greymass.com/php-basics-the-array/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

