<?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; integer</title>
	<atom:link href="http://php.elegosproject.org/tag/integer/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>
		<item>
		<title>ABC of PHP</title>
		<link>http://php.elegosproject.org/2009/06/09/abc-of-php/</link>
		<comments>http://php.elegosproject.org/2009/06/09/abc-of-php/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 22:52:29 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[Basic]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[operator]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://php.elegosproject.org/?p=3</guid>
		<description><![CDATA[OK, first of all: sorry for the bad article&#8217;s title, but it&#8217;s concise and perfect for the purpose of this first and simple tutorial. Today we&#8217;re going to discover the simplicity of PHP, the basic structure and functions.
Before starting, here is a simple example:
&#60;html&#62;
	&#60;head&#62;
		&#60;title&#62;My first PHP code!&#60;/title&#62;
	&#60;/head&#62;
	&#60;body&#62;
		Hello! This is a plain HTML text. I can't [...]]]></description>
			<content:encoded><![CDATA[<p>OK, first of all: sorry for the bad article&#8217;s title, but it&#8217;s concise and perfect for the purpose of this first and simple tutorial. Today we&#8217;re going to discover the simplicity of PHP, the basic structure and functions.</p>
<p><span id="more-3"></span>Before starting, here is a simple example:</p>
<pre class="brush: php;">&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;My first PHP code!&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;
		Hello! This is a plain HTML text. I can't modify it without editing the page itself! (even if I like the block notes <img src='http://php.elegosproject.org/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> )&lt;br /&gt;
		&lt;?php
			$message = &quot;Hello! This is a (potential) dynamic string!&quot;;
			$simpleNumber = 7;
			echo $message.&quot;&lt;br /&gt; I really like number &quot;.$simpleNumber.&quot;!&quot;;
		?&gt;
	&lt;/body&gt;
&lt;/html&gt;</pre>
<p>This code will reproduce this output:</p>
<blockquote><p><em>Hello! This is a plain HTML text. I can&#8217;t modify it without editing the page itself! (even if I like the block notes <img src='http://php.elegosproject.org/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> )<br />
Hello! This is a (potential) dynamic string!<br />
I really like number 7!</em></p></blockquote>
<p>Let&#8217;s start explaining!</p>
<p>First of all, PHP means &#8220;<em>Personal Home Page</em>&#8220;. Yes, you read it correctly: it was initially designed to create simple and (specially) dynamic web pages. During its development PHP become a more and more popular web scripting language and today is one of the most used languages for internet applications, including classes support (PHP5), database access (MySQL but not only), file reading/writing and so on. To demonstrate the power of PHP you could even create an IRC bot with it! Because of its simplicity it&#8217;s also easy to understand and to learn.</p>
<p>As you may see in the codebox, PHP code is closed between &#8220;<strong>&lt;?php</strong>&#8221; and &#8220;<strong>?&gt;</strong>&#8220;. A more standard implementation of it would require &#8220;?php&gt;&#8221; as the ending tag, but nowadays all the web servers recognise just &#8220;?&gt;&#8221;. When you call this tag you won&#8217;t be able to use simple HTML code, you&#8217;ll need to &#8220;print&#8221; it if you want to use it in the &#8220;PHP space&#8221;.</p>
<p>Usually PHP is embedded in the HTML code. Please note that you can create an entire site using pure PHP and ignoring the <em>HEAD</em> and <em>BODY</em> HTML tags, but I allways suggest you to use them in order to produce a standard HTML web page: in fact PHP code will run server-side and will produce plain HTML code, because it&#8217;s the only code a browser will ever decode and show you correctly.</p>
<p>Web pages with PHP inside must be named with the extension &#8220;.php&#8221;: in this way the web server will recognise them and elaborate them; otherwise the web server will show the code instead of the desired output. Another requirement is that the web server handles PHP correctly: it&#8217;s usually written in the website of the hoster. A few italian free web hosting services I used (and still use) are <a title="Official Netsons homepage (italian hosting)" href="http://www.netsons.com/" target="_blank">Netsons</a> and <a title="Altervista official homepage (italian hosting)" href="http://it.altervista.org/" target="_blank">Altervista</a>; find your free hosting on <a title="Google search &quot;free web hosting php&quot;" href="http://www.google.it/search?q=free+web+hosting+php&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:it:official&amp;client=firefox-a" target="_blank">google</a>!</p>
<p>All the commands are followed by a comma, this because it enables you to create complex commands in more than one line. Don&#8217;t worry, I too miss them sometimes! The PHP engine will warn you about them and stop executing the script.</p>
<p><strong>Variables</strong> are all the parts in the code starting with <strong>$</strong>. There are several types of variables, from the local ones (just the ones you see in the code) to the global ones (like <em>$_GET</em>, <em>$_POST</em> or <em>$_SERVER</em>) which are defined by default in the PHP environment and you can&#8217;t modify them (but we&#8217;ll see them later in another tutorial). They are automatically recognised by the PHP engine: this means you can write a string (which is a text variable) without declaring it before (remember in C++? string sString = &#8220;blablabla&#8221;), you can save a number, a float (decimal int, i.e. 1.002) and so on <em>&#8220;just writing it&#8221;</em>.</p>
<p>To declare a variable use the &#8220;=&#8221; operator, to compare two of them (or one of them and a constant) you&#8217;ll use &#8220;==&#8221; in most of the cases.</p>
<pre class="brush: php;">&lt;?php
	$integer = 6;
	$string = &quot;bla bla bla&quot;;
	$float = 1.002;
?&gt;</pre>
<p>Let&#8217;s speak a little bit about <strong>strings</strong>: you can concatenate strings with other strings or numbers. The &#8220;magical&#8221; operation is made by the &#8220;dot operator&#8221; (.). In this way you can mix static with dynamic variables in order to create dynamic contents. In PHP you can mix strings with all the other types of variables. Here is an example:</p>
<pre class="brush: php;">&lt;?php
	$string1 = &quot;Hello &quot;;
	$name = $_POST['name'];
	$random_number = rand(0,100);
	echo $string1.$name.&quot;! Here is your number: &quot;.$random_number;
?&gt;</pre>
<p>As you can see here we&#8217;ve concatenated a local string ($string1), a global variable ($name, $_POST is taken from an HTML form (variable &#8216;name&#8217;) with method POST and as action the PHP page) and a number (rand is used to generate randomic numbers, it&#8217;s a function and we&#8217;ll see it in a moment). Of course there are several functions for strings, they&#8217;re the most elaborated variables of all PHP, I&#8217;ll dedicate an entire tutorial on them.</p>
<p><strong>Numbers</strong>: as said before numbers can be integers, floats and so on. Just declare them as usual! (see the variables example code). You can sum, subtract, divide or multiply them with the standard operators: + (sum), &#8211; (subtraction), / (division), * (moltiplication). Nothing less, nothing more ^^</p>
<p><strong>Functions</strong>: one of the most difficult things to <em>&#8220;tame&#8221;</em> in PHP. The most difficult part is indeed to know them, they&#8217;re as easy as breathing (you&#8217;ll say: this man is crazy!). You have just to make some practice!<br />
Functions are piece of code pre-written for you. They may need <em>arguments</em>: they are contained in the parenthesis next to the function name. We have just seen an example of function: rand($int,$int). Rand is used to generate randomic numbers and requires two arguments: the minimal and the maximal number. You can see a list of function on the <a title="php.net, official PHP website" href="http://www.php.net" target="_blank">php.net</a> website. Don&#8217;t worry about this: following my tutorials you won&#8217;t need to explore it (it&#8217;s HUGE!), in the future, if you&#8217;ll have to search for a function, you&#8217;ll know at least how it may be called the function and thus your search will be fast.</p>
<p>A particular function which needs an argument but not in parenthesis is <em>echo</em>: it requires a variable (of all the types), but just writing it after its calling:</p>
<pre class="brush: php;">&lt;?php
	echo &quot;Hello World!&quot;;
?&gt;</pre>
<p>Of course if it disturbs you, you can allways the more-standard function <em>print</em>, which has nothing different then the function above:</p>
<pre class="brush: php;">&lt;?php
	print(&quot;Hello World!&quot;);
?&gt;</pre>
<p>As of my experience, echo is faster to write down and, using a highlighting program like <a title="Smultron official homepage" href="http://tuppis.com/smultron/" target="_blank">Smultron</a>* (for MacOS), <a title="NotePad++ official homepage" href="http://notepad-plus.sourceforge.net/" target="_blank">NotePad++</a>* (for Windows) or GEdit* / Kate* (for Linux) there is no risk to forget quotes opened. Honestly I started writing PHP with <a title="Adobe Dreamweaver's official homepage" href="http://www.adobe.com/products/dreamweaver/" target="_blank">Adobe (Macromedia) Dreamweaver</a>, but I suggest you to try writing directly the source, you&#8217;ll learn 200% faster!</p>
<p>What have you learnt since the beginning?</p>
<ol>
<li>What PHP is and how it&#8217;s called within an HTML web page</li>
<li>How are variables declared</li>
<li>How to print them in the HTML document</li>
<li>How to interact with them</li>
<li>What functions are and how to use them</li>
<li>What programs to use to write in PHP</li>
</ol>
<p>* they are all free and opensource, download them for free!</p>
<p>Now download a PHP editor (or just use the BlockNotes) and start practising!</p>
<p>Giacomo</p>
]]></content:encoded>
			<wfw:commentRss>http://php.elegosproject.org/2009/06/09/abc-of-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
