<?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>Mashups, Marketing and Me &#187; Learning PHP</title>
	<atom:link href="http://www.cfg.no/category/learning-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cfg.no</link>
	<description>The blog project, a journey!</description>
	<lastBuildDate>Wed, 02 Sep 2009 17:51:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Learning PHP (6) &#8211; Connecting to a database</title>
		<link>http://www.cfg.no/2009/06/learning-php-6-connecting-to-a-database/</link>
		<comments>http://www.cfg.no/2009/06/learning-php-6-connecting-to-a-database/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 20:13:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Learning PHP]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.cfg.no/?p=221</guid>
		<description><![CDATA[		
		
		
		Everyone needs to store and get data from a database every now and then. MySQL can be an excellent choice for some.
Creating a table
This is how you create a simple table from the CLI, you can recognize the values if you use systems like phpmyadmin for modifying database structures.
mysql&#62; CREATE TABLE mytable (
-&#62; id INT [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		digg_url = "http://www.cfg.no/2009/06/learning-php-6-connecting-to-a-database/";
		digg_bgcolor = "";
		digg_skin = "";
		digg_window = "";
		digg_title = "Learning+PHP+%286%29+%26%238211%3B+Connecting+to+a+database";
		digg_media = "";
		digg_topic = "";
		digg_bodytext = "";
		//-->
		</script>
		<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div><p><em>Everyone needs to store and get data from a database every now and then. MySQL can be an excellent choice for some.</em></p>
<p><strong>Creating a table</strong><br />
This is how you create a simple table from the CLI, you can recognize the values if you use systems like phpmyadmin for modifying database structures.</p>
<blockquote><p>mysql&gt; CREATE TABLE mytable (<br />
-&gt; id INT NOT NULL AUTO_INCREMENT,<br />
-&gt; name VARCHAR(255) NOT NULL,<br />
-&gt; PRIMARY KEY(id)<br />
-&gt; );<br />
Query OK, 0 rows affected (0.02 sec)</p></blockquote>
<p>The MySQL reserved are upper cased, all the text in lower case are things<br />
that you can change. This basically creates a table named &#8216;mytable&#8217; with<br />
two columns; &#8220;id&#8221; and &#8220;name&#8221;, id is an INT(eger) and name is a varchar of<br />
max 255 characters.</p>
<p>AUTO_INCREMENT means that id will be automatically incremented if not defined.</p>
<p><strong>Connecting to MySQL from PHP</strong><br />
<em>Disclaimer: This is a very basic example, if you are coding for production use please at least look at the function mysql_real_escape_string() or a library<br />
that will handle insertion and such.</em></p>
<p>The example above inserts a record in MySQL, the id will be automatically set to 1 (for the first row you insert, 2 for the next, etc.). To read this record out from the database again you can do something like this:</p>
<pre lang='php'>

<b>Warning</b>:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Unknown MySQL server host 'mysqlhost' (1) in <b>/home/espen/virtualhosts/www.cfg.no/htdocs/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code</b> on line <b>26</b>

<b>Warning</b>:  mysql_select_db(): supplied argument is not a valid MySQL-Link resource in <b>/home/espen/virtualhosts/www.cfg.no/htdocs/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code</b> on line <b>27</b>
Data from MySQL
<b>Warning</b>:  mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in <b>/home/espen/virtualhosts/www.cfg.no/htdocs/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code</b> on line <b>31</b>
</pre>
<p>This piece of code will perform a &#8220;SELECT * FROM mytable&#8221; query (the * means every column from the table, you could get only the name by doing &#8220;<em>SELECT name FROM mytable</em>&#8220;.<br />
Since there can be multiple rows we perform a while loop, so while $row gets fed with data from the mysql_fetch_assoc() function it will print the &#8216;id&#8217; and &#8216;name&#8217; columns from the table.</p>
<p>Select specific row(s) only (match on name)</p>
<pre lang='php'>

<b>Warning</b>:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Unknown MySQL server host 'mysqlhost' (1) in <b>/home/espen/virtualhosts/www.cfg.no/htdocs/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code</b> on line <b>43</b>

<b>Warning</b>:  mysql_select_db(): supplied argument is not a valid MySQL-Link resource in <b>/home/espen/virtualhosts/www.cfg.no/htdocs/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code</b> on line <b>44</b>
Data from MySQL
<b>Warning</b>:  mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in <b>/home/espen/virtualhosts/www.cfg.no/htdocs/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code</b> on line <b>48</b>
</pre>
<p>Now we used the LIKE keyword in MySQL, the percent (<strong>%</strong>) sign is a wildcard.<br />
This will get every row from the table where name begins with an E.</p>
<p>Homework: Now try to combine $_GET and $_POST + MySQL. <img src='http://www.cfg.no/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.cfg.no/2009/06/learning-php-6-connecting-to-a-database/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Learning PHP (5) &#8211; Using built-in functions</title>
		<link>http://www.cfg.no/2009/06/learning-php-5-using-built-in-functions/</link>
		<comments>http://www.cfg.no/2009/06/learning-php-5-using-built-in-functions/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 20:18:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Learning PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[built-in]]></category>
		<category><![CDATA[explode]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[learn php]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[stristr]]></category>
		<category><![CDATA[strstr]]></category>

		<guid isPermaLink="false">http://www.cfg.no/?p=212</guid>
		<description><![CDATA[		
		
		
		I had promised myself that I would post every second day, but I didn&#8217;t know exactly where to go from what we have built up and I also became engaged, she said yes! So it has been crazy days.
But I&#8217;m happy, at least  
I am going to continue with showing how to take use [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		digg_url = "http://www.cfg.no/2009/06/learning-php-5-using-built-in-functions/";
		digg_bgcolor = "";
		digg_skin = "";
		digg_window = "";
		digg_title = "Learning+PHP+%285%29+%26%238211%3B+Using+built-in+functions";
		digg_media = "";
		digg_topic = "";
		digg_bodytext = "";
		//-->
		</script>
		<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div><p><em>I had promised myself that I would post every second day, but I didn&#8217;t know exactly where to go from what we have built up and I also became engaged, she said yes! So it has been crazy days.<br />
But I&#8217;m happy, at least <img src='http://www.cfg.no/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> </em></p>
<p>I am going to continue with showing how to take use of some of the <a rel="nofollow" href="http://no.php.net/manual/en/ref.strings.php">built-in string fuctions</a>. These can be used to manipulate text variables that you might receive from the $_GET and $_POST variables.</p>
<p><strong>Find occurrence of text in strings</strong><br />
You can for example use the function <a rel="nofollow" href="http://no.php.net/manual/en/function.strstr.php">strstr()</a> or <a rel="nofollow" href="http://no.php.net/manual/en/function.stristr.php">stristr()</a> if you want a cast-insensitive matching style.</p>
<p><em>Example</em></p>
<pre class="php">$mystring = 'this websites owner is nice';
if (strstr($mystring, 'website')) {
 echo 'I found the word "website" in the string.';
}</pre>
<p>I can use double quotes in the string because I have it enclosed in single quotes.<br />
If I were to use single quotes it would unquote the string and generate a syntax error.</p>
<p>If you replace <em>strstr</em> with <em>stristr</em> in the example, it would<br />
also give a match if $mystring contains the word &#8216;Website&#8217; with an upper case W.</p>
<p><strong>explode() strings</strong><br />
If you get a string such as &#8220;one,two,three,four,five,six&#8221; and you would rather have<br />
it indexed in an array you can use the function <a rel="nofollow" href="http://no.php.net/manual/en/function.explode.php">explode()</a> to achieve this.</p>
<p>The build-in function explode() will parse a string and split it based on the<br />
argument you give the function. An example will follow:</p>
<pre class="php">$mystring = "one,two,three,four;five,six,seven";
$myarray = explode(",", $mystring);

var_dump($myarray);</pre>
<p>The first argument to explode is the delimiting character, while the other<br />
is the string that will be split into an array. The result of this would be:</p>
<blockquote><p>array(6) {<br />
[0]=&gt;<br />
string(3) &#8220;one&#8221;<br />
[1]=&gt;<br />
string(3) &#8220;two&#8221;<br />
[2]=&gt;<br />
string(5) &#8220;three&#8221;<br />
[3]=&gt;<br />
string(9) &#8220;four;five&#8221;<br />
[4]=&gt;<br />
string(3) &#8220;six&#8221;<br />
[5]=&gt;<br />
string(5) &#8220;seven&#8221;<br />
}
</p></blockquote>
<p>Notice that $myarray[3] contains four;five, this is due to a common<br />
mistake.. it can be hard to overlook, so can you figure out why it does that? <img src='http://www.cfg.no/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> </p>
<p>Anyways, you should check out the <a rel="nofollow" href="http://no.php.net/manual/en/ref.strings.php">documentation</a> and try out some<br />
of the functions, because they are really easy to use and implement.</p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cfg.no/2009/06/learning-php-5-using-built-in-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning PHP Part Four &#8211; If and else</title>
		<link>http://www.cfg.no/2009/05/learning-php-part-four-if-and-else/</link>
		<comments>http://www.cfg.no/2009/05/learning-php-part-four-if-and-else/#comments</comments>
		<pubDate>Sat, 30 May 2009 14:13:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Learning PHP]]></category>
		<category><![CDATA[else]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[learn php]]></category>

		<guid isPermaLink="false">http://www.cfg.no/?p=203</guid>
		<description><![CDATA[		
		
		
		Have you been playing around with the GET and POST arrays now?
If not, learn about foreach in PHP here.
What can I do now?
You can go ahead and use if to check the contents of variables.
Check this out.
If
The syntax for if goes like this:

if (statement) {
  Insert your code here
}
The statement will be checked, and [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		digg_url = "http://www.cfg.no/2009/05/learning-php-part-four-if-and-else/";
		digg_bgcolor = "";
		digg_skin = "";
		digg_window = "";
		digg_title = "Learning+PHP+Part+Four+%26%238211%3B+If+and+else";
		digg_media = "";
		digg_topic = "";
		digg_bodytext = "";
		//-->
		</script>
		<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div><p>Have you been playing around with the GET and POST arrays now?<br />
If not, learn about <a href="http://www.cfg.no/2009/05/learning-php-part-three-reserved-variables-and-loops/">foreach in PHP here</a>.</p>
<p><strong>What can I do now?</strong><br />
You can go ahead and use if to check the contents of variables.<br />
Check this out.</p>
<p><strong>If</strong><br />
The syntax for if goes like this:</p>
<pre lang="PHP">
if (statement) {
  Insert your code here
}</pre>
<p>The statement will be checked, and if it is true the code within the { and } will be executed.<br />
Take for example a script where you get a variable from the url, take the previous<br />
example where you used <strong>$_GET['name']</strong> to retrieve the <strong>?name=</strong> part of the URL.</p>
<p>Now, if your name is John and you want the page to say hello to you but not to anyone else.</p>
<pre lang="PHP">
</pre>
<p>The <em>$_GET['name'] == &#8216;John&#8217;</em> part is called the statement,<br />
you probably understand it but it checks to see if the $_GET array variable<br />
with the key &#8216;name&#8217; contains a string &#8216;John&#8217;.</p>
<p>You can do similar tests with numbers, but then you won&#8217;t need the quotes<br />
like the ones I have around &#8216;John&#8217;.</p>
<p>The example will then be:</p>
<pre lang="PHP">
</pre>
<p>Let us say you want to check if the name is NOT John.</p>
<pre lang="PHP">
You're not my master.</pre>
<p><strong>Else</strong><br />
Now, take the example where you output &#8216;Hi master&#8217;, what if you want<br />
to print something to the ones not being the master? You can use <strong>else</strong></p>
<pre lang="PHP">
You're not my master</pre>
<p>That&#8217;s all for today, I don&#8217;t have so much time today so I am going to keep it quite short.<br />
I will get back with a new article in these series in a couple of days, hope I&#8217;ll see you back here then!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cfg.no/2009/05/learning-php-part-four-if-and-else/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning PHP (3); Reserved vars and loops</title>
		<link>http://www.cfg.no/2009/05/learning-php-part-three-reserved-variables-and-loops/</link>
		<comments>http://www.cfg.no/2009/05/learning-php-part-three-reserved-variables-and-loops/#comments</comments>
		<pubDate>Thu, 28 May 2009 14:49:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Learning PHP]]></category>
		<category><![CDATA[$_GET]]></category>
		<category><![CDATA[$_POST]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[loops]]></category>

		<guid isPermaLink="false">http://www.cfg.no/?p=185</guid>
		<description><![CDATA[		
		
		
		In my previous article I explained arrays and their use. But have you ever wondered what happens to those variables set in the url?  They are contained within an array which is prefilled for your scripts.
The array variables $_GET, $_POST and $_SERVER are some of the interesting ones you can use.
The reserved $_GET variable
An [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		digg_url = "http://www.cfg.no/2009/05/learning-php-part-three-reserved-variables-and-loops/";
		digg_bgcolor = "";
		digg_skin = "";
		digg_window = "";
		digg_title = "Learning+PHP+%283%29%3B+Reserved+vars+and+loops";
		digg_media = "";
		digg_topic = "";
		digg_bodytext = "";
		//-->
		</script>
		<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div><p>In my <a href="http://www.cfg.no/2009/05/learning-php-part-two-arrays/">previous article</a> I explained arrays and their use. But have you ever wondered what happens to those variables set in the url?  They are contained within an array which is prefilled for your scripts.</p>
<p>The array variables $_GET, $_POST and $_SERVER are some of the interesting ones you can use.</p>
<p><strong>The reserved $_GET variable</strong><br />
An example would be this type of URL.<br />
<em>http://www.example.domain/myphpscript.php?name=Espen</em></p>
<p>To catch the variable &#8216;name&#8217; you can do this piece of PHP</p>
<blockquote><p>
&lt;?php<br />
echo &#8220;Hi &#8221; . $_GET['name'];<br />
?&gt;
</p></blockquote>
<p>This will output &#8220;Hi Espen&#8221;.</p>
<p><strong>Using the reserved $_POST variable</strong><br />
To get the variables from a form you can use $_POST['<em>key</em>'].<br />
Look at this example:</p>
<p>Create this HTML form, change the path to <em>myhelloscript.php</em> to a place where you test your scripts.</p>
<blockquote><p>
&lt;form action=&#8221;http://my.domain/myhelloscript.php&#8221; method=&#8221;post&#8221;&gt;<br />
&lt;input type=&#8221;text&#8221; name=&#8221;name&#8221;&gt;<br />
&lt;input type=&#8221;submit&#8221; value=&#8221;send it&#8221;&gt;<br />
&lt;/form&gt;
</p></blockquote>
<p>Then create the file <em>myhelloscript.php</em> with the following contents</p>
<blockquote><p>
&lt;?php<br />
echo &#8220;hi &#8221; . $_POST['name'];<br />
?&gt;
</p></blockquote>
<p>You see? The same result here, only now we&#8217;re using <strong>POST</strong> instead of GET (you can also notice that the URL does not contain the variables).</p>
<p><strong>Looping over an array</strong><br />
Looping over an array can be very usefull in the case where you don&#8217;t know what the reference keys are.<br />
Let us say you didn&#8217;t know that the key for the name in the previous post example was not called  &#8216;name&#8217;, how would you find it?</p>
<p>Here is an example of looping over an array, looping through all the key and value pairs.</p>
<blockquote><p>
&lt;?php<br />
$myarray = array(&#8216;Espen&#8217;, &#8216;John&#8217;, &#8216;Lisa&#8217;, &#8216;Mark&#8217;);</p>
<p>foreach ($myarray as $value) {<br />
echo $value . &#8220;&lt;br&gt;&#8221;;<br />
}<br />
?&gt;
</p></blockquote>
<p>Now we&#8217;re saying for each &#8216;entry&#8217; in $myarray get the entry as $value and do something with it between the { and }<br />
So for each $myarray as $value: echo $value and a &#8216;&lt;br&gt;&#8217; (to get a newline).</p>
<p>This example will output</p>
<blockquote><p>
Espen<br />
John<br />
Lisa<br />
Mark
</p></blockquote>
<p><strong>Getting the key</strong><br />
But that&#8217;s not the key, is it?  How do you find the keys?<br />
This example will use the foreach command in another way to get the keys also</p>
<blockquote><p>
&lt;?php<br />
foreach ($myarray as $key =&gt; $value) {<br />
echo $key . &#8216; is &#8216; . $value . &#8216;&lt;br&gt;&#8217;;<br />
}<br />
?&gt;
</p></blockquote>
<p>Now you can also use the variable $key within the loop for each value to get the corresponding key.</p>
<p>This is an example of looping over the $_GET array</p>
<blockquote><p>
&lt;?php<br />
foreach ($_GET as $key =&gt; $value) {<br />
echo $key . &#8216; is set to &#8216; . $value . &#8216;&lt;br&gt;&#8217;;<br />
}<br />
?&gt;
</p></blockquote>
<p><strong>A Challenge</strong><br />
The $_SERVER variable contains a lot of variables about the request, can you figure out how to show them all?</p>
<p>If you get any weird syntax errors or such, post them as a comment and I will try to help you out!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cfg.no/2009/05/learning-php-part-three-reserved-variables-and-loops/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Learning PHP &#8211; Part two; Arrays</title>
		<link>http://www.cfg.no/2009/05/learning-php-part-two-arrays/</link>
		<comments>http://www.cfg.no/2009/05/learning-php-part-two-arrays/#comments</comments>
		<pubDate>Tue, 26 May 2009 09:59:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Learning PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[learn php]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.cfg.no/?p=178</guid>
		<description><![CDATA[		
		
		
		Here&#8217;s my follow up on my PHP articles, this time I am going to talk a bit about arrays. They are so important that you should learn about them sooner than later.
What are arrays?
This quote is from wikipedia.org
An array is a systematic arrangement of objects, usually in rows and columns. Specifically, it may refer to [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		digg_url = "http://www.cfg.no/2009/05/learning-php-part-two-arrays/";
		digg_bgcolor = "";
		digg_skin = "";
		digg_window = "";
		digg_title = "Learning+PHP+%26%238211%3B+Part+two%3B+Arrays";
		digg_media = "";
		digg_topic = "";
		digg_bodytext = "";
		//-->
		</script>
		<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div><p>Here&#8217;s my follow up on my PHP articles, this time I am going to talk a bit about arrays. They are so important that you should learn about them sooner than later.</p>
<p><strong>What are arrays?</strong><br />
This quote is from <a href="http://wikipedia.org">wikipedia.org</a></p>
<blockquote><p>An array is a systematic arrangement of objects, usually in rows and columns. Specifically, it may refer to several things.</p></blockquote>
<p>That&#8217;s exactly what it is, it&#8217;s a variable (<a href="http://www.cfg.no/2009/05/learning-php-part-one/">see previous post</a>) containing several values (like a shelf with several compartments) that can be retrieved by a key (the number on the compartment).  Array keys are per default a number, but it can also be an associative array with text strings for keys.</p>
<p><strong>So make an array and fill it with data</strong><br />
Here is an example on how to declare an array:</p>
<blockquote><p>
&lt;?php<br />
$bookshelf = array(<br />
&#8216;old books&#8217;,<br />
&#8216;new books&#8217;,<br />
&#8216;cooking books&#8217;<br />
);<br />
?&gt;
</p></blockquote>
<p>The variable name is $bookshelf, it is set to be an array(). The contents is put in between the (), but you can also declare an empty array by doing this:</p>
<blockquote><p>
$bookshelf = array();
</p></blockquote>
<p>By default, the first value will have the key 0, the second will have 1, etc.<br />
To refer to a value within an array and echo it out to display:</p>
<blockquote><p>
&lt;?php<br />
$bookshelf = array(<br />
&#8216;old books&#8217;,<br />
&#8216;new books&#8217;,<br />
&#8216;cooking books&#8217;<br />
);</p>
<p>echo <strong>$bookshelf[0]</strong>;</p>
<p>?&gt;
</p></blockquote>
<p>The result outputted from this script will be:</p>
<blockquote><p>
<em>old books</em>
</p></blockquote>
<p>By using the [ and ] signs behind a variable you are referring to it as an array, what you put between them is the key you are using. <em>Try to change the value between them to 1 and check the result.</em></p>
<p><strong>Adding values to a PHP array</strong><br />
You can make use of the array_push() function to insert more variables to a previously declared array.</p>
<blockquote><p>
&lt;?php<br />
$bookshelf = array(<br />
&#8216;old books&#8217;,<br />
&#8216;new books&#8217;,<br />
&#8216;cooking books&#8217;<br />
);</p>
<p>array_push($bookshelf, &#8216;dvds&#8217;);</p>
<p>echo <strong>$bookshelf[3]</strong>;</p>
<p>?&gt;
</p></blockquote>
<p>You can also set a variable directly by using the key</p>
<p>&lt;?php<br />
$bookshelf = array(<br />
&#8216;old books&#8217;,<br />
&#8216;new books&#8217;,<br />
&#8216;cooking books&#8217;<br />
);</p>
<p>$bookshelf[3] = &#8216;dvds&#8217;;</p>
<p>echo <strong>$bookshelf[3]</strong>;</p>
<p>?&gt;
</p></blockquote>
<p><strong>Deleting a value from a PHP array</strong><br />
Several ways to this also, you can use array_pop() to unset the last value in the array.<br />
The unset() function can be on the array value to unset it.<br />
You can set it to null by referring to the key</p>
<blockquote><p>
array_pop($bookshelf);<br />
unset($bookshelf[3]);<br />
$bookshelf[3] = null;
</p></blockquote>
<p>While these are regular arrays, there are other possible ways to use this. A string variable like $name = &#8220;Espen&#8221;; in my previous article is actually an array of characters. $name[0] will refer to &#8216;E&#8217;, 1 will refer to &#8217;s&#8217; so on and so forth.</p>
<p><strong>Associative Arrays in PHP</strong><br />
You can use keys which are not numbers in an array, those are called associative arrays.<br />
Here is an example, I bet you understand most of the basics now, and in case NOT &#8211; drop me a comment!</p>
<blockquote><p>
$bookshelf['favourite'] = &#8216;Marketing 911&#8242;;<br />
echo $bookshelf['favourite'];
</p></blockquote>
<p>In my next article you will get magic behind the array variables $_GET, $_POST and $_SERVER to finally be able to handle user input from forms and every secret of the request. Want to know when I post it? <a href="/feed">Get Learning by RSS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cfg.no/2009/05/learning-php-part-two-arrays/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Learning PHP &#8211; Part one</title>
		<link>http://www.cfg.no/2009/05/learning-php-part-one/</link>
		<comments>http://www.cfg.no/2009/05/learning-php-part-one/#comments</comments>
		<pubDate>Sun, 24 May 2009 17:07:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Learning PHP]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[learn php]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[teach php]]></category>

		<guid isPermaLink="false">http://www.cfg.no/?p=169</guid>
		<description><![CDATA[		
		
		
		Do you want to learn PHP?
I&#8217;ve been coding (or scripting if you want) PHP actively for myself for probably 10 years now, over the years I&#8217;ve had a lot of questions about it and have done a lot of small favours for people. I would be happy to give away some of my knowledge, so [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		digg_url = "http://www.cfg.no/2009/05/learning-php-part-one/";
		digg_bgcolor = "";
		digg_skin = "";
		digg_window = "";
		digg_title = "Learning+PHP+%26%238211%3B+Part+one";
		digg_media = "";
		digg_topic = "";
		digg_bodytext = "";
		//-->
		</script>
		<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div><p><strong>Do you want to learn PHP?</strong></p>
<p>I&#8217;ve been coding (or scripting if you want) PHP actively for myself for probably 10 years now, over the years I&#8217;ve had a lot of questions about it and have done a lot of small favours for people. I would be happy to give away some of my knowledge, so please ask if there is anything.</p>
<p>I am going to teach PHP from basic to advanced, I will try to keep it to one article every other day.<br />
<a href="http://www.cfg.no/feed/">Subscribe to my feed</a> if you don&#8217;t want to miss out on this chance.</p>
<p><strong>Basic Introduction</strong><br />
What is PHP? It is a scripting language that resides on the server-side as opposed to javascript which is downloaded and ran by the client (which is called client-side).</p>
<p>To be able to execute PHP scripts, you need access to a webserver that runs PHP scripts &#8211; there are a lot of alternatives.<br />
I am not going to explain them here, but I am sure it is possible to arrange something if you need a place and is into learning.<br />
It is also possible to run an <a href="http://httpd.apache.org">Apache</a> server locally with PHP.</p>
<p><strong>My First PHP Script</strong><br />
Create a file named first.php and this is the contents</p>
<blockquote><p>
&lt;?php<br />
  echo &#8220;hello (word)&#8221;;<br />
?&gt;
</p></blockquote>
<p>Save this file somewhere on the webserver and try to go to the url.<br />
It will display: hello (world)</p>
<p><strong>Explanation</strong><br />
<strong>The first line</strong> is <strong>&lt;?php</strong>, this tells the webserver that it is about to parse PHP scripting. You need this before every piece PHP script for it to be executed as PHP.</p>
<p><strong>The second line</strong> is <strong>echo &#8220;hello (world)&#8221;;</strong>, this is actually the PHP script line.<br />
It uses the built-in function &#8216;echo&#8217; to display a string, the string needs to be contained within quotes.<br />
And <strong>notice</strong> that the ending of the line is <strong>;</strong>, this finishes off the statement.<br />
If you were to forget the <strong>;</strong> and had several lines of code the server will continue to feed the rest of the code into the echo function, which would probably not work or give really really unexpected results.</p>
<p><strong>The third line</strong> is <strong>?&gt;</strong>, this tells the webserver that the PHP script is over.<br />
When it reaches this tag, it will continue to output the rest of the page to the browsing user. This means that you can use PHP in between HTML to generate dynamic responses.</p>
<p><strong>My Second PHP Script</strong><br />
Heck, since it&#8217;s the first post and the first script was so easy.. I will show you another script and teach how variables work.<br />
I will move on to more advanced things later, so keep watching if this is way too basic for you. There will be more blood! <img src='http://www.cfg.no/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<blockquote><p>
  &lt;?php<br />
     $name = &#8220;Espen&#8221;;<br />
     $age = 23;<br />
     $twice = $age * 2;<br />
     echo &#8220;My name is &#8221; . $name . &#8221; and my age is &#8221; . $age . &#8220;, if I had lived twice as long I&#8217;d be &#8221; . $twice;<br />
  ?&gt;
</p></blockquote>
<p><strong>Explanation</strong><br />
The <strong>first line of PHP script</strong> is <strong>$name = &#8216;Espen&#8217;;</strong>.<br />
The dollar sign ($) indicates that this is a variable, variables are used to contain dynamic information like if you want to display it at several places in your document.</p>
<p>The variable name is actually &#8216;name&#8217;, and it is set to contain anything behind the equal (=).<br />
The string behind the equal sign is &#8220;Espen&#8221;, notice the <strong>;</strong> ending again.<br />
Because it is contained within quotes, PHP automatically identifies it as a string, it will not work without quotes.<br />
I will show some examples between the difference in double and single quotes later.</p>
<p>The <strong>second line</strong> is <strong>$age = 23;</strong>.<br />
As before, notice the dollar sign and also notice the <strong>;</strong> ending.<br />
Because age is an integer, you don&#8217;t need quotes. If you use quotes around the age like &#8216;23&#8242;, PHP will identify it as a string and doing calculations on it can fail.</p>
<p>The <strong>third line</strong> is <strong>$twice = $age * 2;</strong><br />
This sets the variable named <strong>$twice</strong> to contain whatever is in the variable $age and multiply it with two.<br />
We know from before that $age is set to 23, so this will yield a result of 46. $twice now contains 46.</p>
<p>The <strong>fourth line</strong> is:<br />
echo &#8220;My name is &#8221; . $name . &#8221; and my age is &#8221; . $age . &#8220;, if I had lived twice as long I&#8217;d be &#8221; . $twice;</p>
<p>One of the methods of combining variables into another is the usage of the period character <strong>(.)</strong> There are other and better ways to do it, but that may be to complicated for a beginner for now.<br />
Some if you may relate this to javascript where they use + to combine variables.<br />
So this actually feeds a lot of parts of information to the echo function to display it:<br />
&#8220;My name is &#8221; is the first part, the next variable will be printed right after it &#8211; so notice the white space before the last qutoe.<br />
Then the period character is used to append the variable $name to it.<br />
The string &#8221; and my age is &#8221; is appended by . again, notice white spaces before and after the string.<br />
You probably understand the rest of it now.<br />
<strong>Notice</strong> that any white space added that is not contained within quotes will not be displayed either.</p>
<p>If anything is unclear or completely wrong, feel free to drop me a comment!<br />
More learning is coming: <a href="http://www.cfg.no/feed/">Subscribe to my feed</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cfg.no/2009/05/learning-php-part-one/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
