<?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>BoltPress &#187; functions</title>
	<atom:link href="http://blog.novalistic.com/tag/functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.novalistic.com</link>
	<description>Also the official blog.NOVALISTIC</description>
	<lastBuildDate>Mon, 02 Aug 2010 09:45:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Custom PHP functions</title>
		<link>http://blog.novalistic.com/archives/2007/09/custom-php-functions/</link>
		<comments>http://blog.novalistic.com/archives/2007/09/custom-php-functions/#comments</comments>
		<pubDate>Thu, 20 Sep 2007 08:30:49 +0000</pubDate>
		<dc:creator>BoltClock</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Development & Design]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blog.dev.novalistic.com/?p=101</guid>
		<description><![CDATA[As I&#8217;d promised, today I&#8217;ll teach you how to create your own functions to assist you in your programming. It&#8217;s pretty simple, but some people might not be able to grasp the concept so I&#8217;ll try to make things clear for everyone On to the tutorial! Recap In our previous tutorial, we learned that functions [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.novalistic.com/archives/2007/09/php-functions">As I&#8217;d promised</a>, today I&#8217;ll teach you how to create your own functions to assist you in your programming.</p>
<p>It&#8217;s pretty simple, but some people might not be able to grasp the concept so I&#8217;ll try to make things clear for everyone <img src='http://blog.novalistic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  On to the tutorial!</p>
<p><span id="more-101"></span></p>
<h3>Recap</h3>
<p>In our previous tutorial, we learned that functions could have return values or not:</p>
<p><small>Example 1: last lesson</small></p>
<pre class="phi php">
<span style="color: #fff"><span style="color: #138dd7">&lt;?php

</span><span style="color: #952d13">//&nbsp;Yodeling&nbsp;doesn't&nbsp;really&nbsp;bring&nbsp;anything&nbsp;but&nbsp;attention.
</span><span style="color: #138dd7">yodel</span><span style="color: #df0">();

</span><span style="color: #952d13">//&nbsp;Encrypts&nbsp;a&nbsp;string&nbsp;then&nbsp;returns&nbsp;the&nbsp;result,&nbsp;known&nbsp;as&nbsp;a&nbsp;hash.
//&nbsp;This&nbsp;function&nbsp;is&nbsp;built-into&nbsp;PHP&nbsp;and&nbsp;is&nbsp;good&nbsp;for&nbsp;security.
</span><span style="color: #138dd7">$hash&nbsp;</span><span style="color: #df0">=&nbsp;</span><span style="color: #138dd7">sha1</span><span style="color: #df0">(</span><span style="color: #64e6a5">'String'</span><span style="color: #df0">);

</span><span style="color: #138dd7">?&gt;</span></span>
</pre>
<h3>I wanna make a basic one!</h3>
<p>Let&#8217;s start with the famous Hello World function, shall we? This function won&#8217;t have a return value, we&#8217;ll create another function with one in the next page <img src='http://blog.novalistic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Let&#8217;s try to call a function named <code>hello_world()</code> and see what happens&#8230;</p>
<p><small>Example 2: calling a custom function</small></p>
<pre class="phi php">
<span style="color: #fff"><span style="color: #138dd7">&lt;?php

hello_world</span><span style="color: #df0">();

</span><span style="color: #138dd7">?&gt;</span></span>
</pre>
<p>Oops&#8230;</p>
<blockquote>
<p><strong>Fatal error</strong>: Call to undefined function hello_world() in <strong>D:\NOVALISTIC\3.0\example.php</strong> on line <strong>3</strong></p>
</blockquote>
<p>We haven&#8217;t introduced PHP to our <code>hello_world()</code> function. It doesn&#8217;t know what we want this function to do and we have to guide it. Computers and programs are highly renowned for their ability to only shut up and follow exactly what you tell them to do.</p>
<p>To declare a function, you need to first add a keyword <strong>function</strong>. Yeah, it sounds really obvious doesn&#8217;t it (I&#8217;m shouting out to VBScript and Perl, both of which use <strong>sub</strong> instead of function to declare custom functions)? Next, name your function, add a pair of parentheses &#8216;()&#8217; and a pair of curly braces &#8216;{}&#8217;, and we&#8217;ll call the function immediately after defining it:</p>
<p><small>Example 3: structure of a function declaration</small></p>
<pre class="phi php">
<span style="color: #fff"><span style="color: #138dd7">&lt;?php

</span><span style="color: #df0">function&nbsp;</span><span style="color: #138dd7">hello_world</span><span style="color: #df0">()
{
}

</span><span style="color: #952d13">//&nbsp;Call&nbsp;our&nbsp;function
</span><span style="color: #138dd7">hello_world</span><span style="color: #df0">();

</span><span style="color: #138dd7">?&gt;</span></span>
</pre>
<p>You don&#8217;t have to place the opening curly brace on another line. That&#8217;s just my own coding standard (shared by many other developers though) to make code blocks clearer. You could always place your brace on the same line as the first: <code>function hello_world() {</code>.</p>
<p>Calling our function now does nothing. PHP doesn&#8217;t spit any errors. That means our function was defined successfully and PHP had absolutely no problem with the job.</p>
<p>But&#8230; there&#8217;s nothing between the curly braces. Precisely why nothing happens. PHP thinks <code>hello_world()</code> does nothing. Let&#8217;s get it to do something, and I think you know what I want this function to do&#8230;</p>
<p>Add an echo command between the curly braces like so, and test/refresh ur PHP script.</p>
<p><small>Example 4: adding an echo command</small></p>
<pre class="phi php">
<span style="color: #fff"><span style="color: #138dd7">&lt;?php

</span><span style="color: #952d13">//&nbsp;This&nbsp;function&nbsp;just&nbsp;prints&nbsp;'Hello&nbsp;World!'
</span><span style="color: #df0">function&nbsp;</span><span style="color: #138dd7">hello_world</span><span style="color: #df0">()
{
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #64e6a5">'Hello&nbsp;World!'</span><span style="color: #df0">;
&nbsp;&nbsp;&nbsp;&nbsp;
}

</span><span style="color: #952d13">//&nbsp;Call&nbsp;our&nbsp;function
</span><span style="color: #138dd7">hello_world</span><span style="color: #df0">();

</span><span style="color: #138dd7">?&gt;</span></span>
</pre>
<pre>
Hello World!
</pre>
<p>If you got exactly that message &#8216;Hello World!&#8217;, then congratulations you&#8217;ve just made your first function! But functions don&#8217;t have to be limited to echoing simple sentences like that.</p>
<p>Turn over to define another function that <em>returns</em> a value instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.novalistic.com/archives/2007/09/custom-php-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP functions</title>
		<link>http://blog.novalistic.com/archives/2007/09/php-functions/</link>
		<comments>http://blog.novalistic.com/archives/2007/09/php-functions/#comments</comments>
		<pubDate>Sat, 15 Sep 2007 06:34:38 +0000</pubDate>
		<dc:creator>BoltClock</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Development & Design]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blog.dev.novalistic.com/?p=97</guid>
		<description><![CDATA[Today we&#8217;re going to look at what most programmers call functions. A function is what you call in your program to tell it to perform a series of smaller actions, like echo(), substr(), mysql_query(), or your very own functions like calculate_age() or bake_cookies() (which we will learn how to create in the next tutorial). I&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>Today we&#8217;re going to look at what most programmers call <strong>functions</strong>.</p>
<p>A function is what you call in your program to tell it to perform a series of smaller actions, like <code>echo()</code>, <code>substr()</code>, <code>mysql_query()</code>, or your very own functions like <code>calculate_age()</code> or <code>bake_cookies()</code> (which we will learn how to create in the next tutorial).</p>
<p>I&#8217;ll be explaining some of the most basic PHP default functions you&#8217;ll definitely come across in 99.9% (with a 30-day money-back guarantee&#8230; wait I&#8217;m not a hosting service) of all PHP scripts, then guiding you on basic methods of usage of said functions.</p>
<p><span id="more-97"></span></p>
<h3>Name me some basic ones</h3>
<p>You&#8217;ve seen <code>echo()</code>, its brother <code>print()</code> and a couple more. Here are some other functions that you&#8217;ll probably come across often:</p>
<ul>
<li><code><a href="http://php.net/str_replace">str_replace()</a></code> &#8211; searches a big string and replaces a smaller string/pattern inside it with a new one</li>
<li><code><a href="http://php.net/define">define()</a></code> &#8211; defines a constant value; we&#8217;ll learn more some other time</li>
<li><code><a href="http://php.net/date">date()</a></code> &#8211; returns a certain date format, of right now or a given time</li>
<li><code><a href="http://php.net/htmlspecialchars">htmlspecialchars()</a></code> &#8211; converts all &lt;, &gt; and &amp; in a string to their HTML entities, i.e. <strong>&amp;lt;</strong>, <strong>&amp;gt;</strong> and <strong>&amp;amp;</strong>
</li>
<li><code><a href="http://php.net/empty">empty()</a></code> &#8211; checks if a variable is empty
</li>
</ul>
<p>We&#8217;ll learn more about the above functions in later tutorials, but for now here&#8217;s a little, uh, &#8216;primer&#8217;, followed by guides on how to use some other simple functions.</p>
<h3>What&#8217;s this about language constructs?</h3>
<p>You already know <code>echo()</code>. Actually, it&#8217;s not really a function, but a <strong>language construct</strong>, as in a keyword that should be used in a specific way in a PHP statement. Truth is, I can&#8217;t quite explain it, but just understand that <code>echo()</code> isn&#8217;t exactly a function so you can omit those parentheses:</p>
<p><small>Example 1: <code>echo()</code> with and without parentheses</small></p>
<pre class="phi php">
<span style="color: #fff"><span style="color: #138dd7">&lt;?php

$str&nbsp;</span><span style="color: #df0">=&nbsp;</span><span style="color: #64e6a5">'Hello&nbsp;world!'</span><span style="color: #df0">;
</span><span style="color: #952d13">//&nbsp;Both&nbsp;are&nbsp;perfectly&nbsp;fine,&nbsp;but&nbsp;I&nbsp;prefer&nbsp;omitting&nbsp;parentheses.
</span><span style="color: #df0">echo(</span><span style="color: #138dd7">$str</span><span style="color: #df0">);
echo&nbsp;</span><span style="color: #138dd7">$str</span><span style="color: #df0">;

</span><span style="color: #138dd7">?&gt;</span></span>
</pre>
<p>And yes, <code>print()</code> <em>is</em> a language construct too, as well as control structures like <code>if()</code> (you learned about this in the <a href="http://blog.novalistic.com/archives/2007/07/php-if-elseif-else">previous tutorial</a>).</p>
<h3>How do I use functions?</h3>
<p>Functions can be <strong>called</strong> or used in a variety of ways, but I&#8217;ll just explain some common ones.</p>
<p>The first way, is to just call it by itself:</p>
<p><small>Example 2: calling a function on its own</small></p>
<pre class="phi php">
<span style="color: #fff"><span style="color: #138dd7">&lt;?php

my_lovely_function</span><span style="color: #df0">();
exit;

</span><span style="color: #138dd7">?&gt;</span></span>
</pre>
<p>Such functions usually aren&#8217;t intended to have a <strong>return value</strong> so can be called by themselves, like <code>echo()</code>. A return value is a value that a function spits back to the program after doing its job. For example, if you made a function that squares a number, you want the function to return the result after squaring it.</p>
<p><small>Example 3: return value</small></p>
<pre class="phi php">
<span style="color: #fff"><span style="color: #138dd7">&lt;?php

$num&nbsp;</span><span style="color: #df0">=&nbsp;</span><span style="color: #138dd7">6</span><span style="color: #df0">;

</span><span style="color: #952d13">//&nbsp;We&nbsp;throw&nbsp;this&nbsp;number&nbsp;in&nbsp;as&nbsp;what&nbsp;programmers&nbsp;call&nbsp;a&nbsp;parameter&nbsp;or&nbsp;an
//&nbsp;argument&nbsp;(don't&nbsp;worry,&nbsp;functions&nbsp;settle&nbsp;THEIR&nbsp;arguments&nbsp;pretty&nbsp;ethically),
//&nbsp;then&nbsp;our&nbsp;function&nbsp;squares&nbsp;it&nbsp;and&nbsp;returns&nbsp;the&nbsp;result&nbsp;for&nbsp;us&nbsp;to&nbsp;finally&nbsp;echo.
</span><span style="color: #138dd7">$sqr&nbsp;</span><span style="color: #df0">=&nbsp;</span><span style="color: #138dd7">square</span><span style="color: #df0">(</span><span style="color: #138dd7">$num</span><span style="color: #df0">);

echo&nbsp;</span><span style="color: #138dd7">$sqr</span><span style="color: #df0">;

</span><span style="color: #138dd7">?&gt;</span></span>
</pre>
<p>Most functions require parentheses wrapped around its arguments (I prefer that term to parameters, dunno why), separated by commas:</p>
<p><small>Example 4: some arguments</small></p>
<pre class="phi php">
<span style="color: #fff"><span style="color: #138dd7">&lt;?php

</span><span style="color: #952d13">//&nbsp;Too&nbsp;many&nbsp;arguments?&nbsp;Line&nbsp;breaks&nbsp;help&nbsp;to&nbsp;clear&nbsp;things&nbsp;up.
</span><span style="color: #138dd7">do_stuff</span><span style="color: #df0">(</span><span style="color: #138dd7">$alpha</span><span style="color: #df0">,&nbsp;</span><span style="color: #64e6a5">'bravo'</span><span style="color: #df0">,&nbsp;</span><span style="color: #138dd7">$charlie</span><span style="color: #df0">,&nbsp;array(
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #64e6a5">'delta'&nbsp;</span><span style="color: #df0">=&gt;&nbsp;</span><span style="color: #64e6a5">'echo'</span><span style="color: #df0">,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #138dd7">$foxtrot</span><span style="color: #df0">,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #64e6a5">'golf'
</span><span style="color: #df0">),&nbsp;</span><span style="color: #138dd7">$hotel</span><span style="color: #df0">);

</span><span style="color: #138dd7">?&gt;</span></span>
</pre>
<p>Functions with return values are just like the different types of data you handle. So you could stick in a function where a data type would go in something like a formula:</p>
<p><small>Example 5: what return values mean as well</small></p>
<pre class="phi php">
<span style="color: #fff"><span style="color: #138dd7">&lt;?php

</span><span style="color: #952d13">//&nbsp;Instead&nbsp;of&nbsp;this...
</span><span style="color: #138dd7">$title&nbsp;</span><span style="color: #df0">=&nbsp;</span><span style="color: #64e6a5">'I&nbsp;&amp;lt;3&nbsp;PHP'</span><span style="color: #df0">;
</span><span style="color: #138dd7">$title&nbsp;</span><span style="color: #df0">=&nbsp;</span><span style="color: #138dd7">htmlspecialchars</span><span style="color: #df0">(</span><span style="color: #138dd7">$title</span><span style="color: #df0">);
</span><span style="color: #138dd7">$title&nbsp;</span><span style="color: #df0">=&nbsp;</span><span style="color: #64e6a5">"&lt;h2&gt;$title&lt;/h2&gt;"</span><span style="color: #df0">;
echo&nbsp;</span><span style="color: #138dd7">$title</span><span style="color: #df0">;

</span><span style="color: #952d13">//&nbsp;You&nbsp;can&nbsp;always&nbsp;try&nbsp;this!&nbsp;Remember&nbsp;our&nbsp;concatenating&nbsp;operator?
</span><span style="color: #138dd7">$title2&nbsp;</span><span style="color: #df0">=&nbsp;</span><span style="color: #64e6a5">'&lt;h2&gt;'&nbsp;</span><span style="color: #df0">.&nbsp;</span><span style="color: #138dd7">htmlspecialchars</span><span style="color: #df0">(</span><span style="color: #64e6a5">'I&nbsp;&amp;lt;3&nbsp;PHP'</span><span style="color: #df0">)&nbsp;.&nbsp;</span><span style="color: #64e6a5">'&lt;/h2&gt;'</span><span style="color: #df0">;
echo&nbsp;</span><span style="color: #138dd7">$title2</span><span style="color: #df0">;

</span><span style="color: #138dd7">?&gt;</span></span>
</pre>
<p>You can even nest functions, i.e. pass functions as arguments!</p>
<p><small>Example 6: nesting functions</small></p>
<pre class="phi php">
<span style="color: #fff"><span style="color: #138dd7">&lt;?php

</span><span style="color: #952d13">//&nbsp;Long&nbsp;procedure?
</span><span style="color: #138dd7">$ingredients&nbsp;</span><span style="color: #df0">=&nbsp;array(
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #952d13">//&nbsp;I'd&nbsp;list&nbsp;them&nbsp;but&nbsp;it'd&nbsp;make&nbsp;this&nbsp;snippet&nbsp;too&nbsp;long!
</span><span style="color: #df0">);
</span><span style="color: #138dd7">$pasta&nbsp;</span><span style="color: #df0">=&nbsp;</span><span style="color: #138dd7">prepare</span><span style="color: #df0">(</span><span style="color: #64e6a5">'pasta'</span><span style="color: #df0">,&nbsp;</span><span style="color: #138dd7">$ingredients</span><span style="color: #df0">);
</span><span style="color: #138dd7">$g&nbsp;</span><span style="color: #df0">=&nbsp;</span><span style="color: #138dd7">garnish</span><span style="color: #df0">(</span><span style="color: #64e6a5">'spinach'</span><span style="color: #df0">);
</span><span style="color: #138dd7">$dish&nbsp;</span><span style="color: #df0">=&nbsp;</span><span style="color: #138dd7">serve</span><span style="color: #df0">(</span><span style="color: #138dd7">$pasta</span><span style="color: #df0">,&nbsp;</span><span style="color: #64e6a5">'hot'</span><span style="color: #df0">,&nbsp;</span><span style="color: #138dd7">$g</span><span style="color: #df0">);

</span><span style="color: #952d13">//&nbsp;Though&nbsp;the&nbsp;above&nbsp;is&nbsp;a&nbsp;bit&nbsp;more&nbsp;organized,&nbsp;give&nbsp;this&nbsp;a
//&nbsp;whirl&nbsp;if&nbsp;you&nbsp;find&nbsp;such&nbsp;long&nbsp;code&nbsp;a&nbsp;hassle.
</span><span style="color: #138dd7">$dish&nbsp;</span><span style="color: #df0">=&nbsp;</span><span style="color: #138dd7">serve</span><span style="color: #df0">(</span><span style="color: #138dd7">prepare</span><span style="color: #df0">(</span><span style="color: #64e6a5">'pasta'</span><span style="color: #df0">,&nbsp;array(</span><span style="color: #952d13">/*&nbsp;Your&nbsp;ingredients&nbsp;*/</span><span style="color: #df0">)),&nbsp;</span><span style="color: #64e6a5">'hot'</span><span style="color: #df0">,&nbsp;</span><span style="color: #138dd7">garnish</span><span style="color: #df0">(</span><span style="color: #64e6a5">'spinach'</span><span style="color: #df0">));

</span><span style="color: #138dd7">?&gt;</span></span>
</pre>
<p>Well, that&#8217;s it for the basics of functions in PHP. I hope you learned something from this tutorial <img src='http://blog.novalistic.com/wp-includes/images/smilies/icon_confused.gif' alt=':?' class='wp-smiley' /> </p>
<h3>I heard I can make my own functions!</h3>
<p>Like most other programming languages, PHP lets <strong>you</strong> define <strong>your</strong> own functions to fulfill <strong>your</strong> own tasks <strong>your</strong> way!</p>
<p>You will learn how to create functions in my next PHP post. Stick around <img src='http://blog.novalistic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.novalistic.com/archives/2007/09/php-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
