<?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>Tech Mug &#187; PHP</title>
	<atom:link href="http://www.techmug.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techmug.com</link>
	<description></description>
	<lastBuildDate>Tue, 31 Jan 2012 12:15:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Facebook Reveal Tab Tutorial</title>
		<link>http://www.techmug.com/facebook-reveal-tab-tutorial/</link>
		<comments>http://www.techmug.com/facebook-reveal-tab-tutorial/#comments</comments>
		<pubDate>Fri, 03 Jun 2011 14:21:15 +0000</pubDate>
		<dc:creator>Fraz Ahmed</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Social Media]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[Facebook Reveal Tab]]></category>
		<category><![CDATA[Graph API]]></category>
		<category><![CDATA[Reveal]]></category>
		<category><![CDATA[Tab]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.techmug.com/?p=430</guid>
		<description><![CDATA[Since Facebook has deprecated XFBML, there are several features that are not working with the old FBML code. There is one very common feature/technique used among Fan Pages that is Reveal Tab. In this technique you show different screen to Non-Fans and different screen to Fans. In the FBML days you could have done with [...]]]></description>
			<content:encoded><![CDATA[<p>Since Facebook has deprecated XFBML, there are several features that are not working with the old FBML code. There is one very common feature/technique used among Fan Pages that is Reveal Tab. In this technique you show different screen to Non-Fans and different screen to Fans. In the FBML days you could have done with few lines of FBML code, but now that is not the case.<br />
<span id="more-430"></span><br />
<img title="More..." src="http://www.techmug.com/wp-includes/js/tinymce/plugins/wordpress/img/trans.gif" alt="" />In this video tutorial I&#8217;ll teach you to develop a complete<em> Reveal Fan page Tab</em>. You will learn:</p>
<ul>
<li>How to setup Facebook Application</li>
<li>How to call Facebook PHP SDK in your pages</li>
<li>How to get Facebook Signed Request</li>
<li>How to utilize Signed Request data</li>
<li>How to deal with unnecessary scroll bar</li>
</ul>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "2820701010";
google_ad_width = 336;
google_ad_height = 280;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h2 style="text-align: left;">Source Code &amp; Demo</h2>
<p style="text-align: center;"><a href="/demo/FB-Reveal-Tab/fb_reveal.zip" target="_blank"><img class="alignnone size-full wp-image-337" title="Download Source Code" src="http://www.techmug.com/wp-content/uploads/2010/10/btn-source-code.jpg" alt="Download Source Code" width="140" height="90" /></a> <a href="http://www.facebook.com/techmug?sk=app_148958801842277" target="_blank"><img class="alignnone size-full wp-image-336" title="See Demo" src="http://www.techmug.com/wp-content/uploads/2010/10/btn-see-demo.jpg" alt="See Demo" width="140" height="90" /></a></p>
<h2>Videos</h2>
<p style="text-align: left;"><iframe class="iframe-class" width="625" height="381" src="http://www.screenr.com/embed/nATs" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" allowtransparency="true"></iframe></p>
<p style="text-align: left;"><iframe class="iframe-class" width="625" height="381" src="http://www.screenr.com/embed/DATs" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" allowtransparency="true"></iframe></p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "4831550702";
google_ad_width = 300;
google_ad_height = 250;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<p style="text-align: left;"><iframe class="iframe-class" width="625" height="381" src="http://www.screenr.com/embed/LATs" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" allowtransparency="true"></iframe></p>
<p style="text-align: left;"><iframe class="iframe-class" width="625" height="381" src="http://www.screenr.com/embed/3ATs" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" allowtransparency="true"></iframe></p>
<h2 style="text-align: left;">Last Words</h2>
<p style="text-align: left;">Facebook API changes very rapidly. If you want a tutorial on some specific issue that is happening due to change in API please let me know.</p>
<p style="text-align: left;">Your questions and comments are welcome. If you like this article, please share it on your favorite website. It may help someone somewhere!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techmug.com/facebook-reveal-tab-tutorial/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Drive PHP with break and continue</title>
		<link>http://www.techmug.com/drive-php-with-break-and-continue/</link>
		<comments>http://www.techmug.com/drive-php-with-break-and-continue/#comments</comments>
		<pubDate>Sat, 16 Oct 2010 10:25:01 +0000</pubDate>
		<dc:creator>Fraz Ahmed</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[break]]></category>
		<category><![CDATA[continue]]></category>
		<category><![CDATA[Drive PHP with code]]></category>
		<category><![CDATA[php break code]]></category>
		<category><![CDATA[php continue cod]]></category>
		<category><![CDATA[php for beginners]]></category>

		<guid isPermaLink="false">http://www.techmug.com/?p=316</guid>
		<description><![CDATA[Break and continue are two very common jobs in almost every programming language. Both play their role somewhere in loops. Loop may be for, foreach, while, do...while or switch. In this article, I&#8217;ll be using for each loop to demonstrate the concept of break and continue. They work in same way in almost every programming [...]]]></description>
			<content:encoded><![CDATA[<p><code><strong>Break</strong></code><strong> and <code>continue</code> </strong>are two very common jobs in almost every programming language. Both play their role somewhere in loops. Loop may be <code>for</code>, <code>foreach</code>, <code>while</code>, <code>do...while</code> or <code>switch</code>.</p>
<p>In this article, I&#8217;ll be using <code>for each</code> loop to demonstrate the concept of <code>break</code> and <code>continue</code>. They work in same way in almost every programming language. <code>break</code> will be your friend to save your loop from more iteration and <code>continue</code> will be really helpful to boost your logic.</p>
<p><span id="more-316"></span><br />
<script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "2820701010";
google_ad_width = 336;
google_ad_height = 280;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h2>Breaking out of a loop</h2>
<p>Sometimes in our loop there is no further need to continue the loop, when a certain condition is met. For example, you might want to iterate through an array to select a particular value. And when you have taken away the desired value, you do not need to continue the loop. There <code>break</code> comes to play the job.</p>
<p>Let&#8217;s begin the learning with code example. First we create an array to be used in our example.</p>
<h3>Code</h3>
<pre class="brush: php; title: ; notranslate">
$vitamins = array(
 'Vitamin A' =&gt; 'Milk, Eggs',
 'Vitamin C' =&gt; 'Tomatoes, Spinach',
 'Vitamin D' =&gt; 'Sunlight',
 'Vitamin E' =&gt; 'Vegetable Oils'
);
</pre>
<p>Now we want to check the sources of <strong>Vitamin C</strong>. Once we get the sources of <strong>Vitamin C</strong>, it is useless to continue the loop and we will be interested in stopping the process. We will accomplish this by using <code>break</code> and an <code>if statement</code> in our code.</p>
<h3>Code</h3>
<pre class="brush: php; title: ; notranslate">
foreach ($vitamins as $key=&gt;$value){

 $sources = $value;

 if($key == 'Vitamin C'){
   break;
 }
}

//Print: Vitamin C sources: Tomatoes, Spinach
echo 'Vitamin C sources: ' . $sources;
</pre>
<h3>Explanation</h3>
<p>Above code iterates through all the values in our <code>$vitamins</code> array and put the <code>$value</code> of current index into <code>$sources</code>. It also checks, if current index is Vitamin C then <code>break</code> the loop. Hence above code does not iterate to Vitamin D and Vitamin E indexes.</p>
<p>This is really helpful when you have an array with hundreds of values in it and you want to perform some logic on it and get a value out of it. More on <code>break</code> can be found in PHP official manual <a href="http://php.net/manual/en/control-structures.break.php" target="_blank">here</a>.</p>
<h2>Directing loop traffic with <code>continue</code></h2>
<p>There might be time when you want to skip the execution of nested statements for a particular condition. In such case <code>continue</code> will be used. For example, now we want all the sources from our <code>$vitamin</code> array except Vitamin C sources. We will again use an <code>if statement</code> for this purpose.</p>
<h3>Code</h3>
<pre class="brush: php; title: ; notranslate">
$sources = '';
foreach ($vitamins as $key=&gt;$value) {

 if($key == 'Vitamin C'){
 continue;
 }

 $sources .= ', ' . $value;
}

//Print: All Sources except vitamin C: , Milk, Eggs, Sunlight, Vegetable Oils
echo 'All Sources except vitamin C: ' . $sources;
</pre>
<h3>Explanation</h3>
<p>In the above code inside <code>foreach</code> statement, first we check if current index is Vitamin C or not. If it is Vitamin C, we use <code>continue</code>. This <code>continue</code> stops the current counter of loop and move to the next counter. It really helps to write code logic in more efficient and effective way.</p>
<p>For more information about <code>continue</code> please read the official document <a href="http://php.net/manual/en/control-structures.continue.php" target="_blank">here</a>.</p>
<h2>Final Words</h2>
<p>If used wisely both<strong> <code>break</code> and <code>continue php code</code></strong> can bring the real power in your code. There is more to do with both of them, we may also use a number after <code>break</code> or <code>continue</code>. To get the fullest of both I recommend you to take a look at official documentation.</p>
<p>Your comments are always welcome. If you like this article, please share it on your favorite website. It may help someone somewhere!</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "4831550702";
google_ad_width = 300;
google_ad_height = 250;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.techmug.com/drive-php-with-break-and-continue/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>PHP Basics for Beginners</title>
		<link>http://www.techmug.com/php-basics-for-beginners/</link>
		<comments>http://www.techmug.com/php-basics-for-beginners/#comments</comments>
		<pubDate>Sun, 03 Oct 2010 11:44:38 +0000</pubDate>
		<dc:creator>Fraz Ahmed</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[double quote]]></category>
		<category><![CDATA[PHP Basics for Beginners]]></category>
		<category><![CDATA[php beginner developers]]></category>
		<category><![CDATA[php for beginners]]></category>

		<guid isPermaLink="false">http://www.techmug.com/?p=299</guid>
		<description><![CDATA[This article aims to serve for beginner PHP developers but those who are comfortable with PHP will also find this article useful. Basically, I&#8217;ll tell you the difference between some of PHP commonly used functions/features. By the end of this article you will have a clear difference among: include VS require include_once VS include require_once [...]]]></description>
			<content:encoded><![CDATA[<p>This article aims to serve for <strong>beginner PHP developers</strong> but those who are comfortable with PHP will also find this article useful. Basically, I&#8217;ll tell you the difference between some of PHP commonly used functions/features. By the end of this article you will have a clear difference among:</p>
<ul>
<li>include VS require</li>
<li>include_once VS include</li>
<li>require_once VS require</li>
<li>echo VS print</li>
<li>Single quote VS Double quote</li>
</ul>
<p>Apparently all functions in each above point work in similar fashion. However, they are slightly different from other. Continue reading to explore the differences.</p>
<p><span id="more-299"></span></p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "2820701010";
google_ad_width = 336;
google_ad_height = 280;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h1>include VS require</h1>
<p>Both are frequently used to include other files into our PHP code. Following are the differences between &#8216;include&#8217; and &#8216;require&#8217;:</p>
<ul>
<li>&#8216;include&#8217; throws a warning, if your specified file is not found. Rest of the code will be rendered.</li>
<li>&#8216;require&#8217; throws a fatal error, if your specified file is not found. Rest of the code will not be rendered.</li>
</ul>
<h3><strong>Testing with Code Example</strong></h3>
<p>I&#8217;m using some code to make it more clear. To check it yourself you may follow the steps with me. Create a new file  &#8216;inc.file.php&#8217;, put below code in it:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
function printMe()
{
 return &quot;I have some code &lt;br&gt;&quot;;
}

echo printMe();
?&gt;
</pre>
<p>Save the file in any directory of server. Now create a new file &#8216;index.php&#8217; and put the below code in it:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
include 'inc.nofile.php'; //File does not exist
include 'inc.file.php'; //This line will be rendered
?&gt;
</pre>
<p>Save the file in same directory. To see how &#8216;include&#8217; works, run the &#8216;index.php&#8217; file from your browser. You will see an error something similar to:<br />
<em><br />
<strong>Warning</strong>: include(inc.nofile.php) [<a href="http://localhost/function.include">function.include</a>]: failed to open stream: No such file or directory in&#8230;</em></p>
<p>But still you can see that the text &#8211; &#8220;<strong>I have some code</strong>&#8221; is printed to the screen.</p>
<p>However, same test for &#8216;require&#8217;, shows that second line will not be rendered. Here is the code for testing &#8216;require&#8217;. Just copy and paste below code to your &#8216;index.php&#8217; file (Remove any existing code)</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
require 'inc.nofile.php'; //File does not exist
include 'inc.file.php'; //This line will not be rendered
?&gt;
</pre>
<p>This time we again get an error, but note that nothing is rendered after an error occurred.</p>
<h3><strong>Conclusion</strong></h3>
<p>Use &#8216;include&#8217; when you want to render the code even if an error occurs. And use &#8216;require&#8217; when you do not want to render the remaining code after an error occurs.</p>
<h1>include_once VS include AND require_once VS require</h1>
<p>&#8216;include_once&#8217; and &#8216;require_once&#8217; are same in nature. Here we will see the difference of &#8216;include_once&#8217; against &#8216;include&#8217;. Following are the differences between &#8216;include&#8217; and &#8216;include_once&#8217;:</p>
<ul>
<li>All includes for same file will be rendered, if you are using &#8216;include&#8217;.</li>
<li>While all includes for same file will be rendered only once, if you are using &#8216;include_once&#8217;. This helps you to prevent an error, if same file included again.</li>
</ul>
<h3><strong><strong>Testing with Code Example</strong></strong></h3>
<p>To start off with experimenting, replace below code in &#8216;index.php&#8217;</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
include 'inc.file.php';
include 'inc.file.php'; //It will generate a fatal error
?&gt;
</pre>
<p>Above code will be ended with following error,</p>
<p><em><strong>Fatal error</strong>: Cannot redeclare printMe() (previously declared</em></p>
<p>Because, we are using same file in our includes. And our file &#8211; inc.file.php &#8211; has a function of printMe(). Since, we cannot re-declare a function in PHP, it throws an error.</p>
<p>But if we test below code by replacing it in our &#8216;index.php&#8217; file, it will not throw any error.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
include_once 'inc.file.php';
include_once 'inc.file.php'; //This will be ignored
?&gt;
</pre>
<p>Since &#8216;inc.file.php&#8217; is included using &#8216;include_once&#8217;, it will ignore the second line because it includes same file only once.</p>
<h3>Conclusion</h3>
<p>Use &#8216;include_once&#8217;, where you are in doubt of including the same file again somewhere in your code.</p>
<h1>print VS echo</h1>
<p>&#8216;print&#8217; and &#8216;echo&#8217; are almost similar. Both are used to write text to screen. But there is a single difference between them:</p>
<ul>
<li>&#8216;print&#8217; does not accept multiple parameters.</li>
<li>While &#8216;echo&#8217; accepts multiple parameters.</li>
</ul>
<p>Above difference may be confusing to many people. I try to clear the confusion by explaining the difference by code.</p>
<h3>Testing with Code Example</h3>
<p>Let&#8217;s begin with &#8216;echo&#8217; example. We pass two parameters to &#8216;echo&#8217;:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
echo (&quot;Tech &quot;), (&quot;Mug&quot;);
?&gt;
</pre>
<p>Above code will be rendered without any error. But if we try same thing with &#8216;print&#8217;, it will be ended with a parse error. Let&#8217;s try it:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
print (&quot;Tech &quot;), (&quot;Mug&quot;); //This will generate an error
?&gt;
</pre>
<p>Above code will give you an error something similar to,</p>
<p><strong>Parse error</strong>: parse error in&#8230;</p>
<h3>Conclusion</h3>
<p>We may pass multiple parameters to &#8216;echo&#8217; but not to &#8216;print&#8217;.</p>
<h1>Single quote VS Double quote</h1>
<p>This might be very confusing for many developers to choose between single and double quote for writing string. There is a couple of difference between using single and double quote:</p>
<ul>
<li>Any variable or special character like \n \r etc under the single quote will not be rendered. It will be displayed as an string.</li>
<li>Any variable or special character like \n \r etc under the double quote will be rendered properly.</li>
</ul>
<h3>Test with Code Example</h3>
<p>Test the below code in &#8216;index.php&#8217; file and run the file.</p>
<pre class="brush: php; title: ; notranslate">

&lt;?php
$var = 'I am a string!';

echo 'I write - $var'; //It will not render $var variable
echo &quot;I write - $var&quot;; //It will render $var successfully

// Special formatting characters (\n \r etc) also do not
// work with single quote
echo 'My first line \n my new line'; // \n sequence will not be rendered and displayed as a string
echo &quot;My first line \n my new line&quot;; // \n sequence will be rendered
?&gt;
</pre>
<p>Explanation of above code should be understandable by reading the comments, the code has.</p>
<h3>Conclusion</h3>
<p>It does not make any difference to use single or double quote until you want to render a variable and/or any special character in it.</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "4831550702";
google_ad_width = 300;
google_ad_height = 250;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<p>Hopefully, now you have a better understanding of above discussion and it helps for <strong>php beginner developers</strong> . Please feel free to share your thoughts or ask any question. Your comments are always welcome. If you like this article, please share it on your favorite website. It may help someone somewhere!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techmug.com/php-basics-for-beginners/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Facebook Application From Scratch – 2</title>
		<link>http://www.techmug.com/facebook-application-from-scratch-2/</link>
		<comments>http://www.techmug.com/facebook-application-from-scratch-2/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 12:57:24 +0000</pubDate>
		<dc:creator>Fraz Ahmed</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[Authentication]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[fbml]]></category>

		<guid isPermaLink="false">http://www.techmug.com/?p=205</guid>
		<description><![CDATA[This is second part of our Facebook Application Development from Scratch series. You may read part one of the series here. In this part we will learn, how to get authentication for your Facebook Applications. More specifically, we will learn: Creating an instance of a PHP Class Getting session information of Facebook logged-in user Getting [...]]]></description>
			<content:encoded><![CDATA[<p>This is second part of our Facebook Application Development from Scratch series. You may read <a href="/facebook-application-from-scratch-1/">part one of the series here</a>. In this part we will learn, how to get authentication for your Facebook Applications. More specifically, we will learn:</p>
<ul>
<li>Creating an instance of a PHP Class</li>
<li>Getting session information of Facebook logged-in user</li>
<li>Getting User ID of Facebook User</li>
<li>Getting login URL for authentication purpose</li>
<li>Using FBML redirect tag</li>
</ul>
<p><span id="more-205"></span></p>
<p style="text-align: center;"><a href="http://www.techmug.com/demo/FBFromScratchSeries/FB-app-part-2-code.zip"><img class="alignnone size-full wp-image-337" title="Download Source Code" src="http://www.techmug.com/wp-content/uploads/2010/10/btn-source-code.jpg" alt="Download Source Code" width="140" height="90" /></a> <a href="http://apps.facebook.com/shareacake/" target="_blank"><img class="alignnone size-full wp-image-336" title="See Demo" src="http://www.techmug.com/wp-content/uploads/2010/10/btn-see-demo.jpg" alt="See Demo" width="140" height="90" /></a></p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "2820701010";
google_ad_width = 336;
google_ad_height = 280;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h1>What you need?</h1>
<p>In order to develop Facebook Application, we need a client library. Client library is a PHP class which offers us to talk to Facebook APIs in an easy manner. Download Facebook client library from:</p>
<p><a href="http://github.com/facebook/php-sdk/blob/master/src/facebook.php" target="_blank">http://github.com/facebook/php-sdk/blob/master/src/facebook.php</a></p>
<h1>Start the process!</h1>
<p>Create a folder named <strong>&#8216;fb&#8217;</strong> in your application folder and move this <strong>facebook.php</strong> file into the <strong>&#8216;fb&#8217;</strong> folder. Create a file <strong>fb.inc.php</strong> in your <strong>&#8216;fb&#8217;</strong> folder and insert below code in it:</p>
<pre class="brush: php; title: ; notranslate">

&lt;?php

require 'facebook.php';

define('APP_ID','xxxxxxxxxx');
define('SECRET_KEY', 'xxxxxxxxxx');
define('CANVAS_PAGE', 'http://apps.facebook.com/shareacake/');
define('CANVAS_URL', 'http://www.techmug.com/demo/FBFromScratchSeries/');

// Create our Application instance.
$facebook = new Facebook(array(
 'appId'  =&gt; APP_ID,
 'secret' =&gt; SECRET_KEY,
 'cookie' =&gt; true,
));

//Get current user's Facebook session
$session = $facebook-&gt;getSession();

// Session based API call.
if ($session) {
 try {
 $uid = $facebook-&gt;getUser();
 } catch (FacebookApiException $e) {
 error_log($e);
 }
}

// login or logout url will be needed depending on current user state.
if ($uid) {
 //Do nothing
} else {
 $params = array(
 'next'          =&gt;    CANVAS_PAGE,
 'cancel_url'    =&gt;    CANVAS_PAGE,
 'fbconnect'     =&gt;    0,
 'canvas'        =&gt;    1,
 'req_perms'     =&gt;    ''
 );

 $loginUrl = $facebook-&gt;getLoginUrl($params);

 echo '&lt;fb:redirect url=&quot;' . $loginUrl . '&quot;/&gt;';
}

?&gt;
</pre>
<p>Change <strong>Application ID</strong> and <strong>Secret Key</strong> of your own. We got these keys in our first episode of this series. Above code is enough for Facebook Application authentication.</p>
<p>Create a file outside the <strong>&#8216;fb&#8217;</strong> folder and name it <strong>index.php</strong>. Include the above file (fb.inc.php) in this newly created index.php file using below code.</p>
<pre class="brush: php; title: ; notranslate">

&lt;?php
require 'fb/fb.inc.php';
?&gt;
</pre>
<p>Finally upload all the files to your server to test the application (<em>At CANVAS_URL</em>).</p>
<h1>Code Explanation</h1>
<p>In the beginning we include Facebook client library class. After this we define 4 Constants to be used in our application.  Then we create an instance of Facebook Class with <strong>$facebook</strong> object.</p>
<p>After creating an instance, we use our newly created object to get current user&#8217;s session by using <strong>getSession()</strong> method of Facebook Class. If our code finds a session, we try to get current logged-in  user&#8217;s Facebook ID. We use <strong>getUser()</strong> method to acquire currently logged-in user&#8217;s ID.</p>
<p>At the moment we will not be able to get information about user, because he has not authenticated our application yet. In the result <strong>$uid</strong> will not be created.</p>
<p>Now, we move forward to authentication process. We check if we have <strong>$uid</strong> variable available or not. If this variable is not set yet, we try to get the login URL of our application. We start with creating an array named <strong>$params</strong>. keys inside this array are the hub of our authentication process. Below is the explanation of used keys:</p>
<table width="99%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><strong>next</strong></td>
<td>The URL where you want to send user after authentication</td>
</tr>
<tr>
<td><strong>cancel_url</strong></td>
<td>The URL where you want to send user, if he cancels the process</td>
</tr>
<tr>
<td><strong>fbconnect</strong></td>
<td>Are you using Facebook Connect? (1 or 0 for Yes and No)</td>
</tr>
<tr>
<td><strong>canvas</strong></td>
<td>Are you using Facebook canvas? (1 or 0 for Yes and No)</td>
</tr>
<tr>
<td><strong>req_perms</strong></td>
<td>Additional permissions, you want to access on user account</td>
</tr>
</tbody>
</table>
<p>For all available additional or extended permissions, please visit &#8211; <a href="http://developers.facebook.com/docs/authentication/permissions" target="_blank">http://developers.facebook.com/docs/authentication/permissions</a></p>
<p>Now its time to put our <strong>$params</strong> array in to <strong>getLoginUrl()</strong> method. At this point we have the login url, where user can actually authenticate our application. We use redirect tag of FBML(Facebook Markup Language) to send the user to authentication page.</p>
<p>At the authentication page, user has two options &#8211; <strong>Allow</strong> and <strong>Don&#8217;t Allow</strong>. Once user clicks on Allow button, our application will be authenticated by user. That was all about getting authentication for your Facebook Application.</p>
<p>Since, our application has been authenticated, we can use different API methods to get needful data. To read more about Facebook API and available methods, please visit &#8211; <a href="http://developers.facebook.com/docs/api" target="_blank">http://developers.facebook.com/docs/api</a></p>
<p><strong>To see a demo of above code, visit our application at:</strong></p>
<p><a href="http://apps.facebook.com/shareacake/" target="_blank">http://apps.facebook.com/shareacake/</a></p>
<p>Once you authenticate the application, you will see some information about you. That&#8217;s the thing we will learn in next episode&#8230;Cheers!</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "4831550702";
google_ad_width = 300;
google_ad_height = 250;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h1>What&#8217;s next?</h1>
<p>In the next episode, we will get user data using Facebook API. We will also display some cakes to be shared and we will also talk about that what can be achieved by using Facebook API. So stay tune for your Facebook hunger.</p>
<p>Please feel free to ask, your comments are always welcome. If you like this article, please share it on your favorite website. It may help someone somewhere!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techmug.com/facebook-application-from-scratch-2/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>AJAX Currency Converter with Google API</title>
		<link>http://www.techmug.com/ajax-currency-converter-with-google-api/</link>
		<comments>http://www.techmug.com/ajax-currency-converter-with-google-api/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 15:42:03 +0000</pubDate>
		<dc:creator>Fraz Ahmed</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Converter]]></category>
		<category><![CDATA[Currency]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://www.techmug.com/?p=211</guid>
		<description><![CDATA[No doubt, Google is a magical search engine. Google also provides many APIs to be incorporated in your application. Today, we will use Google&#8217;s hidden currency conversion API. Why hidden? Because there is no official documentation for it. We will build a currency converter from scratch, it pulls data from Google and display the results [...]]]></description>
			<content:encoded><![CDATA[<p>No doubt, Google is a magical search engine. Google also provides many APIs to be incorporated in your application. Today, we will use Google&#8217;s hidden currency conversion API. Why hidden? Because there is no official documentation for it.</p>
<p><span id="more-211"></span></p>
<p>We will build a currency converter from scratch, it pulls data from Google and display the results using jQuery AJAX function. For you better understanding, you may visit demo or download source code below:</p>
<p style="text-align: center;"><strong><a href="/demo/currency-converter/techmug-currency-converter.zip"><img class="alignnone size-full wp-image-337" title="Download Source Code" src="http://www.techmug.com/wp-content/uploads/2010/10/btn-source-code.jpg" alt="Download Source Code" width="140" height="90" /></a><a href="/demo/currency-converter/" target="_blank"><img class="alignnone size-full wp-image-336" title="See Demo" src="http://www.techmug.com/wp-content/uploads/2010/10/btn-see-demo.jpg" alt="See Demo" width="140" height="90" /></a></strong></p>
<p>Basically our whole code consist of three components &#8211; HTML for displaying interface, Javascript for AJAX functionality and PHP for fetching and manipulating conversion results from Google.</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "2820701010";
google_ad_width = 336;
google_ad_height = 280;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h1>HTML Structure</h1>
<p>HTML structure for this application is simple enough. There are two drop downs, one input field, one button and a div for displaying results. Drop downs are used for selecting From Currency and To Currency while input field is used for entering the desired amount to be converted in your selected currency. Conversion result displays in results div once you click on convert button. When you click on the button, it fires a Javascript function which is described below.</p>
<h1>Javascript</h1>
<p>Following is the code for our AJAX enabled javascript. Below code is executed upon clicking on convert button.</p>
<pre class="brush: jscript; title: ; notranslate">

$(document).ready(function() {

 $('#convert').click(function(){

 //Get all the values
 var amount = $('#amount').val();
 var from = $('#fromCurrency').val();
 var to = $('#toCurrency').val();

 //Make data string
 var dataString = &quot;amount=&quot; + amount + &quot;&amp;from=&quot; + from + &quot;&amp;to=&quot; + to;

 $.ajax({
 type: &quot;POST&quot;,
 url: &quot;ajax_converter.php&quot;,
 data: dataString,

 success: function(data){
 //Show results div
 $('#results').show();

 //Put received response into result div
 $('#results').html(data);
 }

 });
 });
});
</pre>
<p>Above code is very simple and should be self explanatory, if you are familiar with jQuery AJAX function. For those who does not know, what&#8217;s going on here I tr to explain them.</p>
<p><em>$(&#8216;#convert&#8217;).click(function&#8230;</em> this is the starting point of our code, it means that execute the underneath code of this function when an element with id of  &#8216;convert&#8217; is clicked. In our case this id is used for &#8216;convert&#8217; button.</p>
<p>After that we grab values from our HTML elements by using <em>.val()</em> function of jQuery and assign these values into Javascript variables. These variables are then used in dataString variable.</p>
<p>In our AJAX function we are using POST method and in &#8216;url&#8217;, we provide the url of our PHP file which will provide conversion results back to this AJAX function. We are sending &#8216;dataString&#8217; as data to our PHP script.</p>
<p>After successful iterating to our PHP script, this AJAX function first shows our results div and out the received data into our results div as HTML.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "4831550702";
google_ad_width = 300;
google_ad_height = 250;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h1>PHP Code</h1>
<p>The brain of our application is below PHP code.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//Get Posted data
$amount = $_POST['amount'];
$from = $_POST['from'];
$to = $_POST['to'];

//make string to be put in API
$string = &quot;1&quot;.$from.&quot;=?&quot;.$to;

//Call Google API
$google_url = &quot;http://www.google.com/ig/calculator?hl=en&amp;q=&quot;.$string;

//Get and Store API results into a variable
$result = file_get_contents($google_url);

//Explode result to convert into an array
$result = explode('&quot;', $result);

################################
# Right Hand Side
################################
$converted_amount = explode(' ', $result[3]);
$conversion = $converted_amount[0];
$conversion = preg_replace('/[\x00-\x08\x0B-\x1F]/', '', $conversion);
$conversion = $conversion * $amount;
$conversion = round($conversion, 2);

//Get text for converted currency
$rhs_text = ucwords(str_replace($converted_amount[0],&quot;&quot;,$result[3]));

//Make right hand side string
$rhs = $conversion.$rhs_text;

################################
# Left Hand Side
################################
$google_lhs = explode(' ', $result[1]);
$from_amount = $google_lhs[0];

//Get text for converted from currency
$from_text = ucwords(str_replace($from_amount,&quot;&quot;,$result[1]));

//Make left hand side string
$lhs = $amount.&quot; &quot;.$from_text;

################################
# Make the result
################################

echo $lhs.&quot; = &quot;.$rhs;
?&gt;
</pre>
<p>Above code is well commented to make you understand better. In this code we are using some built-in PHP functions &#8211; <a href="http://php.net/manual/en/function.file-get-contents.php" target="_blank">file_get_contents</a>, <a href="http://php.net/manual/en/function.explode.php" target="_blank">explode</a>, <a href="http://php.net/manual/en/function.round.php" target="_blank">round</a> and <a href="http://php.net/manual/en/function.ucwords.php">ucwords</a>.</p>
<ul>
<li>In start we grab the values posted to this script by our AJAX function. We store these values into variables.</li>
<li>We make a string variable using above explained variables.</li>
<li>We concatenate this string variable to Google Currency Calculator API url and store the url in a new variable named $google_url.</li>
<li>We fetch the data of $google_url to $result variable using PHP&#8217;s <a href="http://php.net/manual/en/function.file-get-contents.php" target="_blank">file_get_contents</a> function.</li>
<li>We receive data from Google something similar to<br />
<em><strong>{lhs: &#8220;1 Euro&#8221;,rhs: &#8220;1.2692 U.S. dollars&#8221;,error: &#8220;&#8221;,icc: true}</strong></em></li>
<li>Using PHP&#8217;s <a href="http://php.net/manual/en/function.explode.php" target="_blank">explode</a> function, we separate <strong>lhs</strong> and <strong>rhs</strong> from the received response.</li>
<li>From <strong>rhs</strong> we get the converted value and multiply it by our dsired amount to get total conversion.</li>
<li>Using PHP&#8217;s <a href="http://php.net/manual/en/function.round.php" target="_blank">round</a> function, converted amount is round off to two digits.</li>
<li>After this we use PHP&#8217;s <a href="http://php.net/manual/en/function.ucwords.php">ucwords</a> function to Capitalize our text&#8217;s first letter.</li>
<li>We almost do the same for lhs.</li>
<li>At last we make results by joining lhs and rhs and echo it to give the results back to our AJAX function.</li>
</ul>
<h1>Last words</h1>
<p>In the source code I have applied some CSS to make the user interface better. So don&#8217;t be puzzled with the code in source file.</p>
<p>Finally, you have a working Live Currency Converter. You may use this script in your website to attract more visitors. Just play around the code and let me know if you can enhance it further. Please feel free to ask, your comments are always welcome.</p>
<p>If you like this article, please share it on your favorite website. It may help someone somewhere!</p>
<div id="_mcePaste" class="mcePaste" style="position: absolute; left: -10000px; top: 570px; width: 1px; height: 1px; overflow: hidden;">&lt;script type=&#8221;text/javascript&#8221;&gt;&lt;!&#8211;<br />
google_ad_client = &#8220;ca-pub-7217075623453662&#8243;;<br />
/* Tm &#8211; 336 x 280 */<br />
google_ad_slot = &#8220;2820701010&#8243;;<br />
google_ad_width = 336;<br />
google_ad_height = 280;<br />
//&#8211;&gt;<br />
&lt;/script&gt;<br />
&lt;script type=&#8221;text/javascript&#8221;<br />
src=&#8221;http://pagead2.googlesyndication.com/pagead/show_ads.js&#8221;&gt;<br />
&lt;/script&gt;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.techmug.com/ajax-currency-converter-with-google-api/feed/</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
		<item>
		<title>Facebook Application From Scratch &#8211; 1</title>
		<link>http://www.techmug.com/facebook-application-from-scratch-1/</link>
		<comments>http://www.techmug.com/facebook-application-from-scratch-1/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 18:43:28 +0000</pubDate>
		<dc:creator>Fraz Ahmed</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Social Media]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[Facebook Application]]></category>
		<category><![CDATA[fbjs]]></category>
		<category><![CDATA[fbml]]></category>
		<category><![CDATA[share]]></category>

		<guid isPermaLink="false">http://www.techmug.com/?p=97</guid>
		<description><![CDATA[Today, we are starting a series of tutorials for Facebook Application development from scratch. In this series of tutorials, we will develop a fully functional Facebook application. The idea behind our application is simple enough, in this application users will be able to share cakes with their friends, cakes could be posted on users&#8217; or [...]]]></description>
			<content:encoded><![CDATA[<p>Today, we are starting a series of <strong>tutorials for Facebook Application</strong> development from scratch. In this series of tutorials, we will develop a fully functional <strong>Facebook application</strong>. The idea behind our application is simple enough, in this application users will be able to share cakes with their friends, cakes could be posted on users&#8217; or users&#8217; friends wall. Users will also be able to invite their friends to this application. After completing this tutorial you will be able to:</p>
<ul>
<li>Setup a Facebook Application</li>
<li>Use Facebook Authentication for aplications</li>
<li>Learn FBJS</li>
<li>Learn Facebook Graph API</li>
<li>Learn how to post data on users wall using Facebook Application</li>
<li>Learn how to post data on users&#8217; friends wall using Facebook Application</li>
<li>Create a custom tab for your application</li>
</ul>
<p style="text-align: center;"><span id="more-97"></span><br />
<script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "2820701010";
google_ad_width = 336;
google_ad_height = 280;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h2>Let&#8217;s kick the app!</h2>
<p>To develop Facebook applications you need to have a developer account on facebook.com. Click <a href="http://www.facebook.com/developers/" target="_blank">here</a> and follow simple steps to create your developer account. Once you have created your developer account, go to:</p>
<p><a href="http://www.facebook.com/developers/" target="_blank">http://www.facebook.com/developers/</a></p>
<p>At this stage you should have something similar to below screen. Click on the &#8220;<strong>Set Up New Application</strong>&#8221; button:</p>
<p><em><strong>Tip:</strong> Click on any of below snapshots to view bigger image.</em></p>
<p><a href="http://www.techmug.com/wp-content/uploads/2010/08/fb-setup-01.jpg"><img class="alignnone size-full wp-image-177" title="Facbook Application Setup" src="http://www.techmug.com/wp-content/uploads/2010/08/fb-setup-01.jpg" alt="Facbook Application Setup" width="618" height="210" /></a></p>
<h2>Create Application</h2>
<p>Give your application a name and choose &#8220;Agree&#8221; for terms and click &#8220;Create Application&#8221;. Congratulations! your new facebook application has been created. Now we will look at some of configurations.</p>
<p><a href="http://www.techmug.com/wp-content/uploads/2010/08/fb-setup-02.jpg"><img class="alignnone size-full wp-image-178" title="Facebook Application Setup" src="http://www.techmug.com/wp-content/uploads/2010/08/fb-setup-02.jpg" alt="Facebook Application Setup" width="625" height="276" /></a></p>
<h2>Basic Configuration</h2>
<p>When you hit the &#8220;Create Application&#8221; button, you will be redirected to a page similar to below screen. Here you can configure your application according to your needs. For the sack of simplicity we will go for basic configuration. Later in this series of tutorials we will come back here to do more stuff.</p>
<p>For now you should see the same application name, you provided in last step  (Share a Cake in our case). Give your application some description. And click on &#8216;<strong>Facebook Integration</strong>&#8216; at the left side of the screen.</p>
<p><a href="http://www.techmug.com/wp-content/uploads/2010/08/fb-setup-031.jpg"><img class="alignnone size-full wp-image-332" title="FB Application Setup" src="http://www.techmug.com/wp-content/uploads/2010/08/fb-setup-031.jpg" alt="FB Application Setup" width="590" height="177" /></a></p>
<h2>Canvas Settings</h2>
<p>The area where people interact with your facebook application usually is called <strong>Canvas</strong>. The URL to access your applcation is called <strong>Canvas Page</strong> and the location where you host your files for facebook application is called <strong>Canvas URL</strong>.</p>
<p><strong>Canvas Page</strong> is unique for every facebook application and subject to the availability. While <strong>Canvas URL</strong> is your server URL. Facebook does not host your files, you will need a server to store your application files.</p>
<p>Enter your values for <strong>Canvas Page</strong> and <strong>Canvas URL</strong>.</p>
<p>In last, choose<strong> FBML</strong> for Canvas Type in Canvas Settings. Difference between IFrame and FBML application is a topic &#8211; we are leaving for some later time in future.</p>
<p>Finally click on <strong>Save Changes</strong> button at bottom.</p>
<p><a href="http://www.techmug.com/wp-content/uploads/2010/08/fb-setup-041.jpg"><img class="alignnone size-full wp-image-333" title="FB Application Setup" src="http://www.techmug.com/wp-content/uploads/2010/08/fb-setup-041.jpg" alt="" width="591" height="167" /></a></p>
<h2>Final Step</h2>
<p>Three elements <strong>Application ID</strong>, <strong>API Key</strong> and <strong>Application Secret</strong> are very important in order to run our application in the Facebook. You should notice hidden values for <strong>API Key</strong> and <strong>Application Secret</strong> in below screen shot. In your case you must be having some alpha numeric characters for them.</p>
<p><a href="http://www.techmug.com/wp-content/uploads/2010/08/fb-setup-05.jpg"><img class="alignnone size-full wp-image-334" title="FB Application Setup" src="http://www.techmug.com/wp-content/uploads/2010/08/fb-setup-05.jpg" alt="FB Application Setup" width="546" height="393" /></a></p>
<p>For now, just make a note for the values of <strong>Application ID</strong>, <strong>API Key</strong> and <strong>Application Secret</strong>. We will use this information later in this series.</p>
<h2>We are done!</h2>
<p>That&#8217;s all for setting up your Facebook Application. In upcoming days we are bringing more delicious CAKES for your Facebook Application development hunger. So stay tune with us.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "4831550702";
google_ad_width = 300;
google_ad_height = 250;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<p>If you like this article, please share it on your favorite website. It may help someone somewhere!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techmug.com/facebook-application-from-scratch-1/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Send Emails / Newsletters using HTML templates and PHP</title>
		<link>http://www.techmug.com/send-emails-newsletters-using-html-templates-and-php/</link>
		<comments>http://www.techmug.com/send-emails-newsletters-using-html-templates-and-php/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 17:44:44 +0000</pubDate>
		<dc:creator>Fraz Ahmed</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Newsletter]]></category>
		<category><![CDATA[phpmailer]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://www.techmug.com/?p=23</guid>
		<description><![CDATA[HTML emails look attractive, provide easy calls of action. And importantly creates your brand image in the eyes of your clients. But it becomes very difficult to manage your HTML emails if you want to change the look and feel of your email design. Or you may be interested in changing the content of your [...]]]></description>
			<content:encoded><![CDATA[<p><strong>HTML emails </strong>look attractive, provide easy calls of action. And importantly creates your brand image in the eyes of your clients. But it becomes very difficult to manage your HTML emails if you want to change the look and feel of your email design. Or you may be interested in changing the content of your HTML email without playing with your code.</p>
<p>In this tutorial we will be<strong> sending HTML emails</strong> using PHP. This tutorial will teach you to keep your HTML presentation separate from your PHP code.</p>
<p><span id="more-23"></span></p>
<p style="text-align: center;"><a href="http://www.techmug.com/demo/html-email/html-email.zip"><img class="alignnone size-full wp-image-337" title="Download Source Code" src="http://www.techmug.com/wp-content/uploads/2010/10/btn-source-code.jpg" alt="Download Source Code" width="140" height="90" /></a></p>
<h2><strong>How it works?</strong></h2>
<p>Basically there are only three steps to send your branded HTML emails.</p>
<ol>
<li>Read HTML template file from your specified location.</li>
<li>Replace variables with your data, if any.</li>
<li>Finally send email.</li>
</ol>
<h2><strong>What we need?</strong></h2>
<p>Before we move to define the above steps, its worth to mention the requirements of this tutorial. The first thing we need to send emails is PHPMailer class. To learn more about PHPMailer class please visit the official website <a title="PHPMailer" href="http://phpmailer.worxware.com/" target="_blank">here</a>. And the last other thing is your HTML template file.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "2820701010";
google_ad_width = 336;
google_ad_height = 280;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h2><strong>Step by step learning</strong></h2>
<p>Here are the steps you need to take in order to develop your own email sending program.</p>
<p><strong>First</strong> of all you need to include the PHPMailer class in your code. To do so you use below code:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
#####################################
# Include PHP Mailer Class
#####################################
require(&quot;class.phpmailer.php&quot;);
?&gt;
</pre>
<p>Before moving further I would like to introduce two PHP functions we will use in our code. First one is for sending emails and second is for reading your <strong>HTML template</strong> file. First function (sendEmail) accepts five parameters and return false if email is not sent and returns true if email is sent. Parameters are self explanatory in below code:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
#####################################
# Function to send email
#####################################
function sendEmail ($fromName, $fromEmail, $toEmail, $subject, $emailBody) {
$mail = new PHPMailer();
$mail-&gt;FromName = $fromName;
$mail-&gt;From = $fromEmail;
$mail-&gt;AddAddress(&quot;$toEmail&quot;);

$mail-&gt;Subject = $subject;
$mail-&gt;Body = $emailBody;
$mail-&gt;isHTML(true);
$mail-&gt;WordWrap = 150;

if(!$mail-&gt;Send()) {
return false;
} else {
return true;
}
}
?&gt;
</pre>
<p>The other function (readTemplateFile) reads your HTML template file and returns all the stuff found in that file. This function accepts only one parameter which is the location of your HTML file. Below is function:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
#####################################
# Function to Read a file
# and store all data into a variable
#####################################
function readTemplateFile($FileName) {
$fp = fopen($FileName,&quot;r&quot;) or exit(&quot;Unable to open File &quot;.$FileName);
$str = &quot;&quot;;
while(!feof($fp)) {
$str .= fread($fp,1024);
}
return $str;
}
?&gt;
</pre>
<p><strong>Finally</strong> you need to call and pass parameters to above function to complete the task of sending HTML email. Yeah! really we almost done it. Ok we are using a template which send username and password of a user. Please see HTML template file in downloaded file. We break our last and final step in three pieces:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//Data to be sent (Ideally fetched from Database)
$NameOfUser = &quot;XYZ&quot;;
$Username = &quot;abcdef&quot;;
$password = &quot;123456&quot;;
$UserEmail = &quot;receiver.email@somedomain.com&quot;;
?&gt;
</pre>
<p>First of all we put data into some variables. These variables will be used later in our code. Ideally this data will be fetched from database in real world.</p>
<p>Now we put our HTML template data into other variable:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//Send email to user containing username and password
//Read Template File
$emailBody = readTemplateFile(&quot;template.html&quot;);
?&gt;
</pre>
<p>So we have all our file data in a variable ($emailBody). And we may change anything in this variable. We will use PHP&#8217;s str_replace function to replace the data:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//Replace all the variables in template file
$emailBody = str_replace(&quot;#name#&quot;,$NameOfUser,$emailBody);
$emailBody = str_replace(&quot;#username#&quot;,$Username,$emailBody);
$emailBody = str_replace(&quot;#password#&quot;,$password,$emailBody);
?&gt;
</pre>
<p>To understand the above code we take first line of code. It simply replace #name# with the value of $NameOfuser variable in $emailBody variable. And now our new replaced content is stored in $emailBody variable, so now we can use this content to be passed in our email sending function below:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//Send email
$emailStatus = sendEmail (&quot;Sender Name&quot;, &quot;some.email@yourdomain.com&quot;, $UserEmail, &quot;Email Subject&quot;, $emailBody);
?&gt;
</pre>
<p>If email is not sent successfully, we will display error message else we will show success message:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//If email function return false
if ($emailStatus != 1) {
echo &quot;An error occured while sending email. Please try again later.&quot;;
} else {
echo &quot;Email with account details were sent successfully.&quot;;
}
?&gt;
</pre>
<p>That&#8217;s all! Now you have fully functional code to send your branded emails. If in future you want to change the design of your email, you just have to replace HTML template file. There is no need of changing your PHP code. Ideally you can use this code to send:</p>
<ul>
<li>Newsletters</li>
<li>Registration Details</li>
<li>Forgot Password email</li>
<li>And for anything you want</li>
</ul>
<p>Besides the benefits of using above code there is one shortcoming too of this code. If your user&#8217;s email client does not support <em>HTML email</em>, he/she will not be able to see your fancy deigned email. But there are ways to prevent this shortcoming.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "4831550702";
google_ad_width = 300;
google_ad_height = 250;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<p>Hope so you enjoyed this tutorial. Please feel free to ask or comment for this tutorial. And Don’t forget to share the post on your favorite website. It may also help someone somewhere <img src='http://www.techmug.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Send your <strong>newsletters using HTML templates and PHP</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.techmug.com/send-emails-newsletters-using-html-templates-and-php/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>

