<?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; creation of functions</title>
	<atom:link href="http://php.elegosproject.org/tag/creation-of-functions/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>
	</channel>
</rss>
