<?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>PHP Simple Tutorials &#187; array</title>
	<atom:link href="http://php.elegosproject.org/tag/array/feed/" rel="self" type="application/rss+xml" />
	<link>http://php.elegosproject.org</link>
	<description>Free and user-friendly PHP tutorials</description>
	<lastBuildDate>Sun, 19 Jul 2009 13:12:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Strings &amp; variables</title>
		<link>http://php.elegosproject.org/2009/06/13/strings-and-variables/</link>
		<comments>http://php.elegosproject.org/2009/06/13/strings-and-variables/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 08:12:52 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[Basic]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://php.elegosproject.org/?p=43</guid>
		<description><![CDATA[Why is this article called &#8220;Strings &#38; Variables&#8221;? Well, just because we&#8217;re going to speak about variables. The thing is that numbers are not that hard to use, while strings have a variety of functions, not that easy to understand on the first attempts.

Variables are what you&#8217;re going to integrate to your website. Variables are [...]]]></description>
			<content:encoded><![CDATA[<p>Why is this article called &#8220;Strings &amp; Variables&#8221;? Well, just because we&#8217;re going to speak about variables. The thing is that numbers are not that hard to use, while strings have a variety of functions, not that easy to understand on the first attempts.</p>
<p><span id="more-43"></span></p>
<p>Variables are what you&#8217;re going to integrate to your website. Variables are strings, numbers, arrays&#8230; and they can (must, I may say) interact with each other in order to output the desired result.</p>
<p><strong>Let&#8217;s start from numbers</strong>: the most common variable types are <em>integers</em> and <em>floats</em> (decimal numbers). But, as said before, variables aren&#8217;t declared but in their implementation, while in C or other languages you have to specify the type during the declaration (which you can omit it in most of the cases in PHP). Numbers can interact with each others with the normal operators + (plus), &#8211; (minus), * (multiplier), / (divisor), % (rest). The last one will output the <span style="text-decoration: underline;">rest</span> of a division, while the division will return an integer.</p>
<p>Numbers can be mixed: in this case you&#8217;ll create the most precise type, depending on your variables. For example an integer plus a float will return a float. This can be avoided <em>casting</em> the type of the result. No, we&#8217;re not going to make a film, it&#8217;s simply a way to force a type of a variable <img src='http://php.elegosproject.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  . You can cast a type just preceding the operation with the type of the desired variable in rounded parenthesis.</p>
<pre class="brush: php;">&lt;?php
	$anInteger = (integer)4+5.001; // will be 9
	$aFloatNumber = (float)4+5; // will be 9.000
?&gt;</pre>
<p>Easy, wasn&#8217;t it?</p>
<p><strong>Well, now speak a little bit about strings</strong>: they have an entire class of functions. You can know their lenght, you can capitalise them, lowerise them, take a substring, know where is the first occourrance of a substring and so on.<br />
To concatenate two different strings we use the dot operator, not the plus one.</p>
<pre class="brush: php;">&lt;php
	$string1 = &quot;Hello&quot;;
	$string2 = &quot;World&quot;;
	$string3 = $string1.&quot; &quot;.$string2.&quot;!&quot;;
?&gt;</pre>
<p>I don&#8217;t have the time to spend lots and lots of words about the string functions, they&#8217;re <span style="text-decoration: underline;">a lot</span>! You can find the list of functions (with detailed description) <a title="php.net strings page" href="http://www.php.net/strings" target="_blank">here</a>. The most important thing to understand is that a string is an array of characters. What is an array? A series of data, united under the same variable&#8217;s name. So, when for example you want to take the first three characters of a string you have to &#8220;select&#8221; object 0, 1 and 2 (arrays by the way start from 0). Here is an example:</p>
<pre class="brush: php;">&lt;?php
$abc = substr(&quot;abcdefghijklmnopqrstuwxyz&quot;,0,3); // arguments are: original string, starting point (negative values will be considered from the end of the string), optional lenght of the substring
?&gt;</pre>
<p>What about arrays? Well, they&#8217;re a more advanced variable, I&#8217;ll try to explain them with some examples, the best way to do it in my opinion. You have to know that arrays are indexed. This means that you can find a value in an array just calling its index. Index may be automatically generated by PHP, or manually set. To automatically generate the index, you don&#8217;t need to specify the index itself. But here are some examples:</p>
<pre class="brush: php;">&lt;?php
	$array1 = array(1 =&gt; &quot;a&quot;, 2 =&gt; &quot;b&quot;, 3 =&gt; &quot;c&quot;);
	$array2 = array(); // in this case I declare it first because some strict settings of PHP may print a warning in the next function
	if(!in_array(&quot;abc&quot;,$array2)) $array2[] = &quot;abc&quot;; // In this case the index is automatically generated
	$array3 = array(&quot;Riccardo&quot; =&gt; 22, &quot;Diego&quot; =&gt; 46, &quot;Elena&quot; =&gt; 33); // Yes it's not kind to tell the age of a madame, but it's a totally casual ^^

	echo $array1[2]; // b
	echo $array2[0]; // abc
	echo $array3[&quot;Diego&quot;]; // 46
?&gt;</pre>
<p>As you saw from the example, to call the variable at a certain index you use squares. You can add new variables to the existing arrays also &#8220;pushing&#8221; the variable with array_push(), and &#8220;pop&#8221; the array (eliminating the last variable in the array) using&#8230; uhm&#8230; array_pop()? ^^</p>
<p>Now make practice with the functions of strings, numbers and arrays, see you in the next tutorial!</p>
]]></content:encoded>
			<wfw:commentRss>http://php.elegosproject.org/2009/06/13/strings-and-variables/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
