<?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; function</title>
	<atom:link href="http://php.elegosproject.org/tag/function/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>Functions</title>
		<link>http://php.elegosproject.org/2009/06/10/functions/</link>
		<comments>http://php.elegosproject.org/2009/06/10/functions/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 22:12:03 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[Basic]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[creation of functions]]></category>
		<category><![CDATA[function]]></category>

		<guid isPermaLink="false">http://php.elegosproject.org/?p=29</guid>
		<description><![CDATA[Here is the basic tutorial (I may call it: basic guide) about functions! In this article I&#8217;m going to explain what are the functions and how to create them too!
PHP is based on functions. Without functions you couldn&#8217;t combine and express your variables. So it can be defined as the hinge of PHP.
As you may [...]]]></description>
			<content:encoded><![CDATA[<p>Here is the basic tutorial (I may call it: basic guide) about functions! In this article I&#8217;m going to explain what are the functions and how to create them too!<span id="more-29"></span></p>
<p>PHP is based on functions. Without functions you couldn&#8217;t combine and express your variables. So it can be defined as the hinge of PHP.</p>
<p>As you may correctly think, functions &#8220;do something&#8221;. Starting from the easiest &#8220;print&#8221; to the more complex socket opening or image generation.</p>
<p>First of all: all the functions require arguments, from zero up to a lot of them. Functions are called in this way:<div><div class="wp-synhighlighter-expanded"><a name="#codesyntax1"></a><a style="wp-synhighlighter-title" href="#codesyntax1"  onClick="javascript:wpContainer=this.parentNode.parentNode.getElementsByTagName('div')[1];	if(wpContainer.style.display=='none') {wpContainer.style.display=''; this.parentNode.className='wp-synhighlighter-expanded'} 	else {wpContainer.style.display='none'; this.parentNode.className='wp-synhighlighter-collapsed'}">Code</a></div><div class="wp-synhighlighter-inner"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">function(arg1, arg2, arg3);</div></li></ol></pre></div></div>Just like most of the programming languages (C, Java and so on).</p>
<p>Arguments are variables which can be defined &#8220;on the fly&#8221; or previously set and then put as them. For the first example, I&#8217;m going to print two strings, equals to each other, in these two different ways:</p>
<div><div class="wp-synhighlighter-expanded"><a name="#codesyntax2"></a><a style="wp-synhighlighter-title" href="#codesyntax2"  onClick="javascript:wpContainer=this.parentNode.parentNode.getElementsByTagName('div')[1];	if(wpContainer.style.display=='none') {wpContainer.style.display=''; this.parentNode.className='wp-synhighlighter-expanded'} 	else {wpContainer.style.display='none'; this.parentNode.className='wp-synhighlighter-collapsed'}">Code</a></div><div class="wp-synhighlighter-inner"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$welcome</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><a href="http://www.php.net/print"><span style="color: #990000;">print</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// direct</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><a href="http://www.php.net/print"><span style="color: #990000;">print</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$welcome</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div></li></ol></pre></div></div>
<p>By the way, // means a comment on a single line while /* comment here */ means a comment in multi lines.<br />
In this case the print function requires one argument, which can be an integer, float, array, string&#8230; it doesn&#8217;t matter, PHP is cool <img src='http://php.elegosproject.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  . As you can see the potential of PHP is to get a data, edit it, combine it with other data, recollect other informations and output the final code. In this way you can just imagine the dynamic potentials of PHP versus a static HTML web page.</p>
<p>Some functions require some arguments as the others do, but sometime they may be able to accept extra arguments. They&#8217;re set by default and thus can be omitted in the function call, but can be changed if necessary. An example of these functions is <a title="fsockopen php.net page" href="http://www.php.net/fsockopen" target="_blank">fsockopen</a>, which is able to open a connection to another server (i.e. for pinging a game-server or to establish a plain text / binary transmission between two services&#8230; don&#8217;t bother about it (for now)!):<br />
<div><div class="wp-synhighlighter-expanded"><a name="#codesyntax3"></a><a style="wp-synhighlighter-title" href="#codesyntax3"  onClick="javascript:wpContainer=this.parentNode.parentNode.getElementsByTagName('div')[1];	if(wpContainer.style.display=='none') {wpContainer.style.display=''; this.parentNode.className='wp-synhighlighter-expanded'} 	else {wpContainer.style.display='none'; this.parentNode.className='wp-synhighlighter-collapsed'}">Code</a></div><div class="wp-synhighlighter-inner"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">resource fsockopen ( string $hostname [, int $port= -1 [, int &amp;$errno [, string &amp;$errstr [, float $timeout= ini_get(&quot;default_socket_timeout&quot;) ]]]] )</div></li></ol></pre></div></div><br />
Well, all the arguments in squares [] are optional arguments. For example, I want to ping my web server (port: 80, default value of the argument), then my FTP server (port 21). Here is the code:</p>
<div><div class="wp-synhighlighter-expanded"><a name="#codesyntax4"></a><a style="wp-synhighlighter-title" href="#codesyntax4"  onClick="javascript:wpContainer=this.parentNode.parentNode.getElementsByTagName('div')[1];	if(wpContainer.style.display=='none') {wpContainer.style.display=''; this.parentNode.className='wp-synhighlighter-expanded'} 	else {wpContainer.style.display='none'; this.parentNode.className='wp-synhighlighter-collapsed'}">Code</a></div><div class="wp-synhighlighter-inner"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$web</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fsockopen"><span style="color: #990000;">fsockopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php.elegosproject.org&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$ftp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fsockopen"><span style="color: #990000;">fsockopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php.elegosproject.org&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">21</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div></li></ol></pre></div></div>
<p>If I want to know if something goes wrong, I may specify $errno and $errstr, two variables that are set in case of errors, but this function is very complex, it was just an example on how optional arguments work ^^.</p>
<p>So, functions may edit variables, but most commonly they return <em>something</em>. For example the function rand, saw for the first time in the <a title="ABC of PHP" href="http://php.elegosproject.org/2009/06/09/abc-of-php/" target="_self">ABC of PHP</a> article, returns an integer (which is a number).</p>
<p>Generally in other program languages, like C, when you create a function you have to declare what type of variable will return. In PHP there is no need to do it, as all the variables are treated in the same way!</p>
<p>To create a function you have simply to&#8230; write it! just with this structure:</p>
<div><div class="wp-synhighlighter-expanded"><a name="#codesyntax5"></a><a style="wp-synhighlighter-title" href="#codesyntax5"  onClick="javascript:wpContainer=this.parentNode.parentNode.getElementsByTagName('div')[1];	if(wpContainer.style.display=='none') {wpContainer.style.display=''; this.parentNode.className='wp-synhighlighter-expanded'} 	else {wpContainer.style.display='none'; this.parentNode.className='wp-synhighlighter-collapsed'}">Code</a></div><div class="wp-synhighlighter-inner"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span><span style="color: #000088;">$myArg1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$myArg2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$myArg3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// my operations here</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">return</span> <span style="color: #000088;">$myVariable</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//can also not return at all, in that case omit this line </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div></li></ol></pre></div></div>
<ol>
<li>Functions require arguments, but if your function requires no arguments, simply leave the parenthesis void -&gt; function myFunction()</li>
<li>All the code of the function is closed in the graphs {}: it&#8217;s a separate environment, and thus you won&#8217;t be able to use local variables declared in the main body of your script, but only global ones (like $_SERVER, $_POST, $_GET and so on) and local ones declared in the function (or arguments passed to the function, but pay attention: they are only COPIES of the original variables!).</li>
<li>A function may return something. In this case, &#8220;return $whatToReturn&#8221; will be written, generally at the end of the function: in fact nothing of the function will be processed after the &#8220;return line&#8221;, so pay attention!</li>
</ol>
<p>Here is an example of a very stupid function:</p>
<div><div class="wp-synhighlighter-expanded"><a name="#codesyntax6"></a><a style="wp-synhighlighter-title" href="#codesyntax6"  onClick="javascript:wpContainer=this.parentNode.parentNode.getElementsByTagName('div')[1];	if(wpContainer.style.display=='none') {wpContainer.style.display=''; this.parentNode.className='wp-synhighlighter-expanded'} 	else {wpContainer.style.display='none'; this.parentNode.className='wp-synhighlighter-collapsed'}">Code</a></div><div class="wp-synhighlighter-inner"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">function</span> getNameSurname<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$surname</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sex</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;m&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strtolower"><span style="color: #990000;">strtolower</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sex</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;m&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Mister&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">else</span> <span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Missus&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">return</span> <span style="color: #000088;">$title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$surname</span><span style="color: #339933;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> getNameSurname<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Giacomo&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Furlan&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><a href="http://www.php.net/print"><span style="color: #990000;">print</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will print &quot;Mister Giacomo Furlan&quot;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div></li></ol></pre></div></div>
<p>In this case the function will require $name and $surname. If $sex is not specified, male will be the default one. Strtolower is a function which makes a string all in lower case (easier to handle in these cases where case sensitivity is not required). Then the function concatenates the sex title, the name and the surname. Thus this function will return a string. The script will eventually call this function and echo the result. Easy, wasn&#8217;t it? (don&#8217;t bother about if else cycle, I&#8217;ll speak about it in another tutorial).</p>
]]></content:encoded>
			<wfw:commentRss>http://php.elegosproject.org/2009/06/10/functions/feed/</wfw:commentRss>
		<slash:comments>1</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>
