<?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>code-diesel</title>
	<atom:link href="http://www.codediesel.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Sun, 29 Aug 2010 15:42:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>HTML 5: Learning the new standard</title>
		<link>http://www.codediesel.com/html5/html-5-learning-the-new-standard/</link>
		<comments>http://www.codediesel.com/html5/html-5-learning-the-new-standard/#comments</comments>
		<pubDate>Sun, 29 Aug 2010 15:29:28 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[html5]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2689</guid>
		<description><![CDATA[learning html 5 with html5rocks.com]]></description>
			<content:encoded><![CDATA[<p>With HTML5 quickly making inroads in the web world, information relating to the workings of the same are on the increase. One resource I find quite useful and thorough is the one by Google &#8211; <a target="_blank"  href="http://www.html5rocks.com/">html5rocks</a>. Filled with various tutorials and a playground to work with HTML5 code, the site makes a quite useful destination for those learning the new HTML 5 standard.</p>
<p><a target="_blank" href="http://www.html5rocks.com/"><img src="http://www.codediesel.com/wp-content/uploads/2010/08/html5rocks_google.jpg" alt="" title="html5rocks" width="408" height="169" class="aligncenter size-full wp-image-2690" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/html5/html-5-learning-the-new-standard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multi column sequences in MySQL</title>
		<link>http://www.codediesel.com/mysql/multi-column-sequences-in-mysql/</link>
		<comments>http://www.codediesel.com/mysql/multi-column-sequences-in-mysql/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 09:28:25 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[sequences]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2686</guid>
		<description><![CDATA[multi-column auto-increment sequences in mysql]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2010/08/sequence.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2010/08/sequence.jpg" alt="" title="sequence" width="150" height="150" class="alignleft size-full wp-image-2688" /></a>One of the most common used attributes in MySQL is definitely AUTO_INCREMENT. This is quite helpful when one needs to generate unique identities for the table rows. By default when a AUTO_INCREMENT column is the only column in a index, whether PRIMARY KEY or UNIQUE, it generates a single monotonic sequence of numbers : 1,2,3,4,&#8230; etc. But for <a target="_blank" href="http://dev.mysql.com/doc/refman/5.0/en/myisam-storage-engine.html">MyISAM</a> storage engine it is possible to create complex sequences in a table containing an AUTO_INCREMENT column.<br />
<span id="more-2686"></span><br />
Take the simple table definition below:</p>

<div class="wp_codebox"><table width="100%" ><tr id="26861"><td class="code" id="2686code1"><pre class="sql"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> directors
<span style="color: #66cc66;">&#40;</span>
    id INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>,
    director VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,
    film VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#41;</span> ENGINE = MyISAM;</pre></td></tr></table></div>

<p>Add a few rows to the table and you can see the &#8216;id&#8217; column sequence marching steadily in a uniform fashion.</p>

<div class="wp_codebox"><table width="100%" ><tr id="26862"><td class="code" id="2686code2"><pre class="sql">+<span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span>
| id | director   | film                  |
+<span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span>
|  <span style="color: #cc66cc;">1</span> | spielberg  | The Sugarland Express |
|  <span style="color: #cc66cc;">2</span> | spielberg  | Minority Report       |
|  <span style="color: #cc66cc;">3</span> | spielberg  | Empire of the Sun     |
|  <span style="color: #cc66cc;">4</span> | soderbergh | Traffic               |
|  <span style="color: #cc66cc;">5</span> | soderbergh | Erin Brockovich       |
|  <span style="color: #cc66cc;">6</span> | spielberg  | The Goonies           |
+<span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span></pre></td></tr></table></div>

<h4>Complex sequences</h4>
<p>Linear sequences are fine, but sometimes we need to generate some complex sequence in our AUTO_INCREMENT column. A complex sequence is something that does not increase monotonically, but according to some pattern. In our example we want a separate linear sequence in the &#8216;id&#8217; column for each director. To create a such a sequence we change the table definition a little by combining two columns for the PRIMARY KEY; the &#8216;id&#8217; column (which is a AUTO_INCREMENT type) and the &#8216;director&#8217; column.</p>

<div class="wp_codebox"><table width="100%" ><tr id="26863"><td class="code" id="2686code3"><pre class="sql"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> directors
<span style="color: #66cc66;">&#40;</span>
    id INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span>,
    director VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,
    film VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>,
    <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>director, id<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#41;</span> ENGINE = MyISAM;</pre></td></tr></table></div>

<p>Now add a few rows and the below table is what you will get.</p>

<div class="wp_codebox"><table width="100%" ><tr id="26864"><td class="code" id="2686code4"><pre class="sql">+<span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span>
| id | director   | film                  |
+<span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span>
|  <span style="color: #cc66cc;">1</span> | spielberg  | The Sugarland Express |
|  <span style="color: #cc66cc;">2</span> | spielberg  | Minority Report       |
|  <span style="color: #cc66cc;">3</span> | spielberg  | Empire of the Sun     |
|  <span style="color: #cc66cc;">1</span> | soderbergh | Traffic               |
|  <span style="color: #cc66cc;">2</span> | soderbergh | Erin Brockovich       |
|  <span style="color: #cc66cc;">4</span> | spielberg  | The Goonies           |
+<span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span></pre></td></tr></table></div>

<p>Although the &#8216;id&#8217; column may now look random, sorting it by the &#8216;director&#8217; field will make it clear. The &#8216;id&#8217; column sequence is now linked up with the &#8216;director&#8217; field.</p>

<div class="wp_codebox"><table width="100%" ><tr id="26865"><td class="code" id="2686code5"><pre class="sql">mysql&gt; <span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> directors <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> director;
&nbsp;
+<span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span>
| id | director   | film                  |
+<span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span>
|  <span style="color: #cc66cc;">1</span> | soderbergh | Traffic               |
|  <span style="color: #cc66cc;">2</span> | soderbergh | Erin Brockovich       |
|  <span style="color: #cc66cc;">1</span> | spielberg  | The Sugarland Express |
|  <span style="color: #cc66cc;">2</span> | spielberg  | Minority Report       |
|  <span style="color: #cc66cc;">3</span> | spielberg  | Empire of the Sun     |
|  <span style="color: #cc66cc;">4</span> | spielberg  | The Goonies           |
+<span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span></pre></td></tr></table></div>

<p>In the above example we have a two column index, but the concept can be applied to a n-column index, where the last column is the AUTO_INCREMENT column. MySQL than generates a independent sequence for each unique non-AUTO_INCREMENT column combination.</p>
<h4>Sequence calculation</h4>
<p>Incidentally, this is how MySQL calculates the next sequence number for a multi column index.</p>

<div class="wp_codebox"><table width="100%" ><tr id="26866"><td class="code" id="2686code6"><pre class="php"><span style="color: #000000;">MAX</span><span style="color: #66cc66;">&#40;</span>auto_increment_column<span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">1</span> WHERE prefix=given-prefix</pre></td></tr></table></div>

<p>So to get the next sequence number for &#8217;soderbergh&#8217;, MySQL runs something like the following query.</p>

<div class="wp_codebox"><table width="100%" ><tr id="26867"><td class="code" id="2686code7"><pre class="php">SELECT <span style="color: #000000;">MAX</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">1</span> FROM directors WHERE director = <span style="color: #DD1144;">'soderbergh'</span></pre></td></tr></table></div>

<h4>Some notes</h4>
<p><strong>Note</strong>:<br />
The &#8216;id&#8217; (AUTO_INCREMENT) field comes last in the key definition. Not placing the AUTO_INCREMENT field last will give you a normal AUTO_INCREMENT sequence.</p>
<p>If any of the non AUTO_INCREMENT columns to be indexed will contain a NULL value, create a UNIQUE index instead of a PRIMARY KEY.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/multi-column-sequences-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Displaying a list of file stats</title>
		<link>http://www.codediesel.com/php/getting-a-quick-list-of-file-stats/</link>
		<comments>http://www.codediesel.com/php/getting-a-quick-list-of-file-stats/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 11:51:32 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[stats]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2683</guid>
		<description><![CDATA[display file type stats using php]]></description>
			<content:encoded><![CDATA[<p>Usually after completing a long project I find that I&#8217;ve created various extraneous files in the project directory; like zip files or maybe a few big graphic files or some huge MySQL dumps. If the directory sizes are small I can manually delete those unwanted files. But if the directory sizes are big or if they are nested deeply, than it can be quite time consuming. Maybe you left a couple of huge MySQL dumps somewhere and forgot to delete them, thus increasing the project file size. And if you are trying to do the cleanup on a online server then it can be even more painful. </p>
<p>Or maybe you are just curious to find how various types of files are taking up your directory space.</p>
<p>Whatever the reason, below is a small php script that displays the distribution of files in a particular directory and its sub-directories by its type. This can be handy if you would like to see which files are taking up space in your project. Although you can easily do such kind of things with a variety of desktop tools, the following code can easily be used online, or integrated into your existing php application.<br />
<span id="more-2683"></span></p>
<h4>Downloading the class</h4>
<p>Before proceeding download the class from below.</p>
<div  class="download2">
<a href="http://www.codediesel.com/downloads/filestats">Download Source</a><br />
<span>Downloads : 55  / File size : 3 kB</span>
</div>
<h4>Displaying file stats</h4>
<p>The class allows you to display the stats in a text format on the command-line or in a HTML format, which you can use online.</p>
<p>To display a text based stats you can use the class as given below and run it from the command line.</p>

<div class="wp_codebox"><table width="100%" ><tr id="26838"><td class="code" id="2683code8"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000;">include</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'FileTypeStats.php'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$stats</span> = <span style="color: #000000; font-weight: bold;">new</span> File_Type_Stats;
<span style="color: #0000ff;">$stats</span>-&gt;<span style="color: #990000;">getText</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;/localhost/home/project&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Below is the output for the above. Note that files that do not have an extension are not displayed.</p>

<div class="wp_codebox"><table width="100%" ><tr id="26839"><td class="code" id="2683code9"><pre>D:\localhost\test\scan_dir&gt;php textStat.php
&nbsp;
=====================================================
File Type      Total Files      Total Size     %
=====================================================
 php                214           2149 kb    (53.72 %)
 js                  55           1183 kb    (29.57 %)
 db                   6            264 kb    (6.60 %)
 css                 20            144 kb    (3.62 %)
 gif                 90            124 kb    (3.11 %)
 pdf                  2             55 kb    (1.40 %)
 jpg                 48             45 kb    (1.14 %)
 png                 41             24 kb    (0.62 %)
 log                  3              5 kb    (0.13 %)
 sql                  1              3 kb    (0.09 %)
 prefs                1              0 kb    (0.00 %)
 txt                  2              0 kb    (0.00 %)</pre></td></tr></table></div>

<p>Setting a path in the function can be a bit limiting, so you can take the path argument from the command line instead.</p>

<div class="wp_codebox"><table width="100%" ><tr id="268310"><td class="code" id="2683code10"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000;">include</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'FileTypeStats.php'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$stats</span> = <span style="color: #000000; font-weight: bold;">new</span> File_Type_Stats;
<span style="color: #0000ff;">$stat_data</span> = <span style="color: #0000ff;">$stats</span>-&gt;<span style="color: #990000;">getText</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$argv</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now you can get the path from the command line.</p>

<div class="wp_codebox"><table width="100%" ><tr id="268311"><td class="code" id="2683code11"><pre class="text">D:\localhost\test\scan_dir&gt;php textStat.php /localhost/home/project/</pre></td></tr></table></div>

<p>Set the <em>textStat.php</em> in you path variable, and you will have the file stat functionality available anywhere.</p>
<p>Someone could have written this class as a shell script easily, but the advantage of doing it in PHP is that we can also have a HTML version of the above stat display, as shown below.</p>
<p>To display HTML table based stats you can use the class with the <em>getHtml()</em> method and run it from the browser.</p>

<div class="wp_codebox"><table width="100%" ><tr id="268312"><td class="code" id="2683code12"><pre class="php">&lt;html&gt;
&lt;head&gt;
&lt;link rel=<span style="color: #DD1144;">&quot;stylesheet&quot;</span> type=<span style="color: #DD1144;">&quot;text/css&quot;</span> href=<span style="color: #DD1144;">&quot;style.css&quot;</span> /&gt;
&lt;/head&gt;
&lt;body&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000;">include</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'FileTypeStats.php'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$stats</span> = <span style="color: #000000; font-weight: bold;">new</span> File_Type_Stats;
<span style="color: #0000ff;">$stat_data</span> = <span style="color: #0000ff;">$stats</span>-&gt;<span style="color: #990000;">getHtml</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;/localhost/home/project&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">echo</span> <span style="color: #0000ff;">$stat_data</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Below is the HTML output.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2010/08/file_stat.png"><img src="http://www.codediesel.com/wp-content/uploads/2010/08/file_stat.png" alt="" title="file_stat" width="400" height="414" class="center size-full wp-image-2685" /></a></p>
<p>The style is defined in a CSS, so you can customize it to your liking.</p>
<p>A word of caution. For hugely nested directory running into thousands of files the script can timeout, so make sure that you increase the php <em>max_execution_time</em> in your script to an appropriate number.</p>

<div class="wp_codebox"><table width="100%" ><tr id="268313"><td class="code" id="2683code13"><pre class="php"><span style="color: #000000;">set_time_limit</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">600</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/getting-a-quick-list-of-file-stats/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Parallel cURL execution in PHP</title>
		<link>http://www.codediesel.com/php/parallel-curl-execution/</link>
		<comments>http://www.codediesel.com/php/parallel-curl-execution/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 07:15:28 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[parallel]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2678</guid>
		<description><![CDATA[executing curl requests in parallel in php]]></description>
			<content:encoded><![CDATA[<p>cURL is a Swiss army knife of web content processing. Programmers use it for a number of things  everyday. One interesting feature the cURL library offers that many programmers are unaware of is the parallel execution of requests.<br />
curl has two major modes of request execution: &#8216;easy&#8217; mode and the &#8216;multi&#8217; mode. Most people use the &#8216;easy&#8217; mode &#8211; in this mode when we issue multiple requests, the second request will not start until the first one is complete. This is known as synchronous execution, and this is the one we normally use. This means that if we have 100 requests to process, each will be processed in a linear manner, which could take a lot of time. This is where &#8216;multi&#8217; mode comes to the rescue. In this mode all requests can be handled in parallel or asynchronously. And it can be quite handy and time saving on many occasions. The &#8216;multi&#8217; or parallel mode is handled by the following curl functions:</p>

<div class="wp_codebox"><table width="100%" ><tr id="267814"><td class="code" id="2678code14"><pre class="php">curl_multi_init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
curl_multi_add_handle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
curl_multi_select<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
curl_multi_exec<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
curl_multi_getcontent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
curl_multi_info_read<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
curl_multi_remove_handle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p><span id="more-2678"></span></p>
<h4>Using parallel curl libraries</h4>
<p>But there is one caveat when using the multi mode; it can be a little confusing. And as always, working with asynchronous code is not always easy, so I prefer to use classes developed by other people. Between the various libraries I&#8217;ve found on the net, <a target="_blank" href="http://github.com/petewarden/ParallelCurl">pete wardens</a> is the one I like. Below is a rough outline of how it works. You first initialize the class and specify the number of requests it should handle in parallel (10 here) along with some curl options.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267815"><td class="code" id="2678code15"><pre class="php"><span style="color: #0000ff;">$parallel_curl</span> = <span style="color: #000000; font-weight: bold;">new</span> ParallelCurl<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>, <span style="color: #0000ff;">$curl_options</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Next we call the <em>startRequest</em>method, specifying the url to request and a callback function, which will be run when the request is processed. We can also pass a optional third parameter which will be handed over to the callback function. If there are any post fields, you can pass them as the fourth parameter in the <em>startRequest</em>method.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267816"><td class="code" id="2678code16"><pre class="php"><span style="color: #0000ff;">$parallel_curl</span>-&gt;<span style="color: #990000;">startRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$search_url</span>, <span style="color: #DD1144;">'on_request_done'</span>, <span style="color: #0000ff;">$search</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Thats it, this is all that is required to execute parallel curl requests. Below is a broad overview of how to use the class.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267817"><td class="code" id="2678code17"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'parallelcurl.php'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$url1</span> = <span style="color: #DD1144;">'http://www.example.com/'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$url2</span> = <span style="color: #DD1144;">'http://www.example.com/'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$url3</span> = <span style="color: #DD1144;">'http://www.example.com/'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// This function gets called back for each request that completes</span>
<span style="color: #000000; font-weight: bold;">function</span> on_request_done<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$content</span>, <span style="color: #0000ff;">$url</span>, <span style="color: #0000ff;">$ch</span>, <span style="color: #0000ff;">$search</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
...
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$max_requests</span> = <span style="color: #cc66cc;">10</span>;
&nbsp;
<span style="color: #0000ff;">$curl_options</span> = <span style="color: #000000;">array</span><span style="color: #66cc66;">&#40;</span>
    CURLOPT_SSL_VERIFYPEER =&gt; <span style="color: #000000; font-weight: bold;">FALSE</span>,
    CURLOPT_SSL_VERIFYHOST =&gt; <span style="color: #000000; font-weight: bold;">FALSE</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$parallel_curl</span> = <span style="color: #000000; font-weight: bold;">new</span> ParallelCurl<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$max_requests</span>, <span style="color: #0000ff;">$curl_options</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Start 3 parallel requests. All three will be started simultaneously.</span>
<span style="color: #0000ff;">$parallel_curl</span>-&gt;<span style="color: #990000;">startRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$url1</span>, <span style="color: #DD1144;">'on_request_done'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$parallel_curl</span>-&gt;<span style="color: #990000;">startRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$url2</span>, <span style="color: #DD1144;">'on_request_done'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$parallel_curl</span>-&gt;<span style="color: #990000;">startRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$url3</span>, <span style="color: #DD1144;">'on_request_done'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$parallel_curl</span>-&gt;<span style="color: #990000;">finishAllRequests</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The complete test script using the class to run Google search queries in parallel is shown below.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267818"><td class="code" id="2678code18"><pre class="php">&nbsp;
<span style="color: #000000;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'parallelcurl.php'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000;">define</span> <span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'SEARCH_URL_PREFIX'</span>, <span style="color: #DD1144;">'http://ajax.googleapis.com/ajax/services/<span style="color: #000099; font-weight: bold;">\</span>
                            search/web?v=1.0&amp;rsz=large&amp;filter=0'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// This function gets called back for each request that completes</span>
<span style="color: #000000; font-weight: bold;">function</span> on_request_done<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$content</span>, <span style="color: #0000ff;">$url</span>, <span style="color: #0000ff;">$ch</span>, <span style="color: #0000ff;">$search</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$httpcode</span> = curl_getinfo<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLINFO_HTTP_CODE<span style="color: #66cc66;">&#41;</span>;    
    <span style="color: #000000;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$httpcode</span> !== <span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000;">print</span> <span style="color: #DD1144;">&quot;Fetch error $httpcode for '$url'<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
        <span style="color: #000000;">return</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$responseobject</span> = json_decode<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$content</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000;">empty</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$responseobject</span><span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">'responseData'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">'results'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000;">print</span> <span style="color: #DD1144;">&quot;No results found for '$search'<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
        <span style="color: #000000;">return</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000;">print</span> <span style="color: #DD1144;">&quot;********<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #000000;">print</span> <span style="color: #DD1144;">&quot;$search:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #000000;">print</span> <span style="color: #DD1144;">&quot;********<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
    <span style="color: #0000ff;">$allresponseresults</span> = <span style="color: #0000ff;">$responseobject</span><span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">'responseData'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">'results'</span><span style="color: #66cc66;">&#93;</span>;
    <span style="color: #000000;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$allresponseresults</span> <span style="color: #000000;">as</span> <span style="color: #0000ff;">$responseresult</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$title</span> = <span style="color: #0000ff;">$responseresult</span><span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">'title'</span><span style="color: #66cc66;">&#93;</span>;
        <span style="color: #000000;">print</span> <span style="color: #DD1144;">&quot;$title<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// The terms to search for on Google</span>
<span style="color: #0000ff;">$terms_list</span> = <span style="color: #000000;">array</span><span style="color: #66cc66;">&#40;</span>
    <span style="color: #DD1144;">&quot;Paul&quot;</span>, <span style="color: #DD1144;">&quot;Lena&quot;</span>,
    <span style="color: #DD1144;">&quot;Lee&quot;</span>, <span style="color: #DD1144;">&quot;Marie&quot;</span>,
    <span style="color: #DD1144;">&quot;Tom&quot;</span>, <span style="color: #DD1144;">&quot;Ada&quot;</span>,
    <span style="color: #DD1144;">&quot;Herman&quot;</span>, <span style="color: #DD1144;">&quot;Josephine&quot;</span>,
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$argv</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$max_requests</span> = <span style="color: #0000ff;">$argv</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #000000;">else</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$max_requests</span> = <span style="color: #cc66cc;">10</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$curl_options</span> = <span style="color: #000000;">array</span><span style="color: #66cc66;">&#40;</span>
    CURLOPT_SSL_VERIFYPEER =&gt; <span style="color: #000000; font-weight: bold;">FALSE</span>,
    CURLOPT_SSL_VERIFYHOST =&gt; <span style="color: #000000; font-weight: bold;">FALSE</span>,
    CURLOPT_USERAGENT, <span style="color: #DD1144;">'Parallel Curl test script'</span>,
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$parallel_curl</span> = <span style="color: #000000; font-weight: bold;">new</span> ParallelCurl<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$max_requests</span>, <span style="color: #0000ff;">$curl_options</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$terms_list</span> <span style="color: #000000;">as</span> <span style="color: #0000ff;">$terms</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$search</span> = <span style="color: #DD1144;">'&quot;'</span>.<span style="color: #0000ff;">$terms</span>.<span style="color: #DD1144;">' is a&quot;'</span>;
    <span style="color: #0000ff;">$search_url</span> = SEARCH_URL_PREFIX.<span style="color: #DD1144;">'&amp;q='</span>.<span style="color: #000000;">urlencode</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$terms</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$parallel_curl</span>-&gt;<span style="color: #990000;">startRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$search_url</span>,<span style="color: #DD1144;">'on_request_done'</span>,<span style="color: #0000ff;">$search</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$parallel_curl</span>-&gt;<span style="color: #990000;">finishAllRequests</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<blockquote><p>
Note: I have found the php curl multi mode execution to be inconsistent on Windows. But it works great on Linux (I&#8217;m using Ubuntu).
</p></blockquote>
<p>You can download the parallelCurl class from <a target="_blank" href="http://github.com/petewarden/ParallelCurl">gitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/parallel-curl-execution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Installing node.js on ubuntu 10.04</title>
		<link>http://www.codediesel.com/linux/installing-node-js-on-ubuntu-10-04/</link>
		<comments>http://www.codediesel.com/linux/installing-node-js-on-ubuntu-10-04/#comments</comments>
		<pubDate>Sat, 10 Jul 2010 06:12:53 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[ubunut]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2675</guid>
		<description><![CDATA[With all the euphoria about node.js since the last few months, I finally decided to give it a try. As it is not available for Windows, I decided to install it on Linux instead of going for Windows/Cygwin. node.js is a implementation of CommonJS, a JavaScript ecosystem in development to be used for developing application [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2010/07/nodejs.jpeg"><img src="http://www.codediesel.com/wp-content/uploads/2010/07/nodejs.jpeg" alt="" title="nodejs" width="104" height="104" class="alignleft size-full wp-image-2677" /></a>With all the euphoria about <a target="_blank" href="http://nodejs.org/">node.js</a> since the last few months, I finally decided to give it a try. As it is not available for Windows, I decided to install it on Linux instead of going for Windows/Cygwin. node.js is a implementation of <a target="_blank" href="http://www.commonjs.org/">CommonJS</a>, a JavaScript ecosystem in development to be used for developing application outside the browser, like:</p>
<p>- Server-side JavaScript applications<br />
- Command line tools<br />
- Desktop GUI-based applications<br />
<span id="more-2675"></span></p>
<h4>Installing node.js</h4>
<p>As mine was a clean Ubuntu installation, I needed to install certain libraries first.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267525"><td class="code" id="2675code25"><pre class="text">sudo apt-get install g++ curl libssl-dev apache2-utils</pre></td></tr></table></div>

<p>The easiest way is to download node.js is to get a git clone. For that I needed to install the git package.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267526"><td class="code" id="2675code26"><pre class="text">sudo apt-get install git-core</pre></td></tr></table></div>

<p>Now download node.js with git.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267527"><td class="code" id="2675code27"><pre class="text">git clone git://github.com/ry/node.git</pre></td></tr></table></div>

<p>If you do not want to use git you could get the tar package instead.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267528"><td class="code" id="2675code28"><pre class="text">wget http://nodejs.org/dist/node-v0.1.96.tar.gz
gunzip node-v0.1.96.tar.gz
tar -xf node-v0.1.96.tar</pre></td></tr></table></div>

<p>Now you are ready to install node.js.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267529"><td class="code" id="2675code29"><pre class="text">cd node
./configure
make
sudo make install</pre></td></tr></table></div>

<h4>Example node.js code</h4>
<p>Below is a simple program using node.js for translating text using google API.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267530"><td class="code" id="2675code30"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> http = require<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> url = <span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'ajax.googleapis.com'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #003366; font-weight: bold;">var</span> google = http.<span style="color: #006600;">createClient</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">80</span>, url<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> text = <span style="color: #3366CC;">&quot;Hello World from node!&quot;</span>;
<span style="color: #003366; font-weight: bold;">var</span> requestUrl = <span style="color: #3366CC;">'/ajax/services/language/translate?v=1.0&amp;q='</span> + 
                 escape<span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span> + <span style="color: #3366CC;">'&amp;langpair=en%7Cfr'</span>
<span style="color: #003366; font-weight: bold;">var</span> request = google.<span style="color: #006600;">request</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'GET'</span>, requestUrl, 
              <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;host&quot;</span>: <span style="color: #3366CC;">&quot;ajax.googleapis.com&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
request.<span style="color: #006600;">end</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
request.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'response'</span>, <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>response<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  	<span style="color: #003366; font-weight: bold;">var</span> body = <span style="color: #3366CC;">''</span>;
&nbsp;
	response.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'data'</span>, <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>chunk<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		body += chunk;
    <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    response.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;end&quot;</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> jsonData = JSON.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>body<span style="color: #66cc66;">&#41;</span>;
	    console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>jsonData.<span style="color: #006600;">responseData</span>.<span style="color: #006600;">translatedText</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>This simple example does not to justice to the true power of node.js. I&#8217;ll be posting useful examples in the near future. Keep watching.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/linux/installing-node-js-on-ubuntu-10-04/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Disabling the silence @-operator in PHP</title>
		<link>http://www.codediesel.com/php/disabling-the-silence-operator-in-php/</link>
		<comments>http://www.codediesel.com/php/disabling-the-silence-operator-in-php/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 08:47:34 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[peck]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2673</guid>
		<description><![CDATA[How to disable the silence error operator in php]]></description>
			<content:encoded><![CDATA[<p>PHP supports one error control operator: the at sign (@). When prepended  to an expression any error generated by that expression will be ignored. It can also be useful for hiding errors generated by various functions.Take the following simple example:</p>

<div class="wp_codebox"><table width="100%" ><tr id="267331"><td class="code" id="2673code31"><pre class="php"><span style="color: #0000ff;">$var</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">'data'</span><span style="color: #66cc66;">&#93;</span>;</pre></td></tr></table></div>

<p>If the &#8216;data&#8217; parameter is not defined the expression will generate an error.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267332"><td class="code" id="2673code32"><pre class="php">Notice: Undefined index: data in /<span style="color: #000000; font-weight: bold;">var</span>/www/test.php on line <span style="color: #cc66cc;">9</span></pre></td></tr></table></div>

<p>You can hide the error using the silence @-operator.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267333"><td class="code" id="2673code33"><pre class="php"><span style="color: #0000ff;">$var</span> = @  <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">'data'</span><span style="color: #66cc66;">&#93;</span>;</pre></td></tr></table></div>

<p>Although quite useful at some times, using the @-operator can have some annoying side effects. Say you are using some external libraries in your application which uses the @-operator. If everything works fine than good. But if the library is generating some errors than it becomes difficult to point the exact location where the error occurs, as the @-operator hides it. If the external library is large, it becomes a headache to remove all the @ from the code. One nice option I found is the <a target="_blank" href="http://pecl.php.net/package/scream">Scream</a> Pecl extension. The extension allows you to easily disable the @-operator in your code without making any actual changes to the code.<br />
<span id="more-2673"></span></p>
<h4>Installing the Scream extension</h4>
<p>As a pre-complied binary is not available, you need to make it yourself. The following shows commands to compile the extension on Ubuntu.</p>
<p>First you need to install the Pear distribution environment.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267334"><td class="code" id="2673code34"><pre>sudo apt-get install php-pear</pre></td></tr></table></div>

<p>Next you will need to install the php5-dev package to get the required PHP5 source files to compile additional modules.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267335"><td class="code" id="2673code35"><pre>sudo apt-get install php5-dev</pre></td></tr></table></div>

<p>Finally we are ready to actually create and install the extension.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267336"><td class="code" id="2673code36"><pre>sudo pecl install scream-0.1.0</pre></td></tr></table></div>

<p>Once the extension is created and installed, we need to add one to the php.ini file.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267337"><td class="code" id="2673code37"><pre>sudo gedit /etc/php5/apache2/php.ini</pre></td></tr></table></div>

<p>In the &#8216;extensions&#8217; section add the following line:<br />
extension=scream.so;</p>
<p>After the php.ini has been updated, you need to restart Apache, so that the new extension is loaded.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267338"><td class="code" id="2673code38"><pre>sudo /etc/init.d/apache2 restart</pre></td></tr></table></div>

<p>If hopefully all went well, the Scream extension should now be loaded, which you can confirm using phpinfo().</p>
<h4>Breaking the Silence operator</h4>
<p>Now you can disable the @-operator in your code using the following:</p>

<div class="wp_codebox"><table width="100%" ><tr id="267339"><td class="code" id="2673code39"><pre class="php"><span style="color: #000000;">ini_set</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'display_errors'</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">error_reporting</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">E_ALL</span> | E_STRICT<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Disable the @-operator</span>
<span style="color: #000000;">ini_set</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'scream.enabled'</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$var</span> = @ <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">'data'</span><span style="color: #66cc66;">&#93;</span>;</pre></td></tr></table></div>

<p>Or you can directly enable the extension in your php.ini.</p>

<div class="wp_codebox"><table width="100%" ><tr id="267340"><td class="code" id="2673code40"><pre class="php">scream.enabled=<span style="color: #cc66cc;">1</span></pre></td></tr></table></div>

<p>Now even though the silence operator is present the above code generates an error if the &#8216;data&#8217; parameter is not set. Atlast no need to hunt down for @&#8217;s while debugging.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/disabling-the-silence-operator-in-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Printing relative dates in php</title>
		<link>http://www.codediesel.com/php/printing-relative-dates-in-php/</link>
		<comments>http://www.codediesel.com/php/printing-relative-dates-in-php/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 13:11:24 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[date]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2666</guid>
		<description><![CDATA[printing relative dates in php]]></description>
			<content:encoded><![CDATA[<p>A couple of days back while writing some date code for a messaging service, I required to print the date of the messages in a relative format &#8211; &#8216;today, &#8216;yesterday&#8217;, 3 weeks ago&#8217; etc. I wrote a small function for the same. A sample run of the function is shown below.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266641"><td class="code" id="2666code41"><pre class="php">&nbsp;
<span style="color: #000000;">echo</span> DateToWords<span style="color: #66cc66;">&#40;</span><span style="color: #000000;">time</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #DD1144;">&quot;&lt;br&gt;&quot;</span>;
<span style="color: #000000;">echo</span> DateToWords<span style="color: #66cc66;">&#40;</span><span style="color: #000000;">time</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3600</span> * <span style="color: #cc66cc;">24</span> * <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #DD1144;">&quot;&lt;br&gt;&quot;</span>;
<span style="color: #000000;">echo</span> DateToWords<span style="color: #66cc66;">&#40;</span><span style="color: #000000;">time</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3600</span> * <span style="color: #cc66cc;">24</span> * <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #DD1144;">&quot;&lt;br&gt;&quot;</span>;
<span style="color: #000000;">echo</span> DateToWords<span style="color: #66cc66;">&#40;</span><span style="color: #000000;">time</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3600</span> * <span style="color: #cc66cc;">24</span> * <span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #DD1144;">&quot;&lt;br&gt;&quot;</span>;
<span style="color: #000000;">echo</span> DateToWords<span style="color: #66cc66;">&#40;</span><span style="color: #000000;">time</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3600</span> * <span style="color: #cc66cc;">24</span> * <span style="color: #cc66cc;">14</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #DD1144;">&quot;&lt;br&gt;&quot;</span>;
<span style="color: #000000;">echo</span> DateToWords<span style="color: #66cc66;">&#40;</span><span style="color: #000000;">time</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3600</span> * <span style="color: #cc66cc;">24</span> * <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #DD1144;">&quot;&lt;br&gt;&quot;</span>;
<span style="color: #000000;">echo</span> DateToWords<span style="color: #66cc66;">&#40;</span><span style="color: #000000;">time</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3600</span> * <span style="color: #cc66cc;">24</span> * <span style="color: #cc66cc;">366</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>And the output for the above. For dates above 1 year it returns the actual date.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266642"><td class="code" id="2666code42"><pre class="text">&nbsp;
today
yesterday
4 days ago
1 week ago
2 weeks ago
14 weeks ago
06-17-2009</pre></td></tr></table></div>

<p><span id="more-2666"></span><br />
The function code is shown below.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266643"><td class="code" id="2666code43"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Change the following constants to suit your language */</span>
&nbsp;
<span style="color: #000000;">define</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'STRING_TODAY'</span>, <span style="color: #DD1144;">&quot;today&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">define</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'STRING_YESTERDAY'</span>, <span style="color: #DD1144;">&quot;yesterday&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">define</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'STRING_DAYS'</span>, <span style="color: #DD1144;">&quot;%d days ago&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">define</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'STRING_WEEK'</span>, <span style="color: #DD1144;">&quot;1 week ago&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">define</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'STRING_WEEKS'</span>, <span style="color: #DD1144;">&quot;%d weeks ago&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* Change the following date format to your taste */</span>
<span style="color: #000000;">define</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'DATE_FORMAT'</span>, <span style="color: #DD1144;">&quot;m-d-Y&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* The functions takes the date as a timestamp */</span>        
<span style="color: #000000; font-weight: bold;">function</span> DateToWords<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$time</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0000ff;">$_word</span> = <span style="color: #DD1144;">&quot;&quot;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Get the difference between the current time 
       and the time given in days */</span>
    <span style="color: #0000ff;">$days</span> = <span style="color: #000000;">intval</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000;">time</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #0000ff;">$time</span><span style="color: #66cc66;">&#41;</span> / <span style="color: #cc66cc;">86400</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* If some forward time is given return error */</span>
    <span style="color: #000000;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$days</span> &lt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000;">return</span> <span style="color: #cc66cc;">-1</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$days</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000;">case</span> <span style="color: #cc66cc;">0</span>: <span style="color: #0000ff;">$_word</span> = STRING_TODAY;
                <span style="color: #000000;">break</span>;
        <span style="color: #000000;">case</span> <span style="color: #cc66cc;">1</span>: <span style="color: #0000ff;">$_word</span> = STRING_YESTERDAY;
                <span style="color: #000000;">break</span>;
        <span style="color: #000000;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$days</span> &gt;= <span style="color: #cc66cc;">2</span> &amp;&amp; <span style="color: #0000ff;">$days</span> &lt;= <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>: 
              <span style="color: #0000ff;">$_word</span> =  <span style="color: #000000;">sprintf</span><span style="color: #66cc66;">&#40;</span>STRING_DAYS, <span style="color: #0000ff;">$days</span><span style="color: #66cc66;">&#41;</span>;
              <span style="color: #000000;">break</span>;
        <span style="color: #000000;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$days</span> &gt;= <span style="color: #cc66cc;">7</span> &amp;&amp; <span style="color: #0000ff;">$days</span> &lt; <span style="color: #cc66cc;">14</span><span style="color: #66cc66;">&#41;</span>: 
              <span style="color: #0000ff;">$_word</span>= STRING_WEEK;
              <span style="color: #000000;">break</span>;
        <span style="color: #000000;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$days</span> &gt;= <span style="color: #cc66cc;">14</span> &amp;&amp; <span style="color: #0000ff;">$days</span> &lt;= <span style="color: #cc66cc;">365</span><span style="color: #66cc66;">&#41;</span>: 
              <span style="color: #0000ff;">$_word</span> =  <span style="color: #000000;">sprintf</span><span style="color: #66cc66;">&#40;</span>STRING_WEEKS, <span style="color: #000000;">intval</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$days</span> / <span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
              <span style="color: #000000;">break</span>;
        <span style="color: #000000; font-weight: bold;">default</span> : <span style="color: #000000;">return</span> <span style="color: #000000;">date</span><span style="color: #66cc66;">&#40;</span>DATE_FORMAT, <span style="color: #0000ff;">$time</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000;">return</span> <span style="color: #0000ff;">$_word</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p> Hope someone finds this useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/printing-relative-dates-in-php/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Anonymous functions in PHP</title>
		<link>http://www.codediesel.com/php/anonymous-functions-in-php/</link>
		<comments>http://www.codediesel.com/php/anonymous-functions-in-php/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 11:09:50 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[lambda]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2663</guid>
		<description><![CDATA[tutorial on Anonymous or lambda functions and closures in php]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2010/06/lambda.png"><img src="http://www.codediesel.com/wp-content/uploads/2010/06/lambda.png" alt="" title="lambda" width="100" height="100" class="alignleft size-full wp-image-2665" /></a>Anonymous functions are common in various modern languages, Ruby and Javascript being the popular one. But until version 5.3 PHP lacked true anonymous functions. Although newbie programmers are hard-pressed to find a suitable application for anonymous functions, they are indispensable if you do a lot of OOP, and can provide some elegant solutions to some particular problems.<br />
<span id="more-2663"></span></p>
<h4>PHP variable functions</h4>
<p>Before we learn something about anonymous functions we will take a quick look into a PHP concept known as a variable function. It means that if we append parenthesis to a variable, then php will look for a function with the same name as to whatever the variable evaluates to and tries to execute it. Say we have the following simple function:</p>

<div class="wp_codebox"><table width="100%" ><tr id="266344"><td class="code" id="2663code44"><pre class="php"><span style="color: #000000; font-weight: bold;">function</span> Hello<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000;">echo</span> <span style="color: #DD1144;">&quot;Hello $name&quot;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>We could then call the function indirectly by using a variable whose value evaluates to the function name. This can be quite useful when the name of the function that you want to execute cannot be determined till run-time.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266345"><td class="code" id="2663code45"><pre class="php"><span style="color: #0000ff;">$func</span> = <span style="color: #DD1144;">&quot;Hello&quot;</span>;
<span style="color: #0000ff;">$func</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;World!&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//Output-</span>
<span style="color: #808080; font-style: italic;">//Hello World!</span></pre></td></tr></table></div>

<p>Another example using a class and a static method:</p>

<div class="wp_codebox"><table width="100%" ><tr id="266346"><td class="code" id="2663code46"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> CHello
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> hello<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000;">echo</span> <span style="color: #DD1144;">&quot;Hello $name&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0000ff;">$func</span> = <span style="color: #DD1144;">&quot;Hello&quot;</span>;
CHello::<span style="color: #0000ff;">$func</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;World!&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Output-</span>
<span style="color: #808080; font-style: italic;">// Hello World!</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Anonymous or lambda functions</h4>
<p>There are times when you need to create a small localized throw-away function consisting of a few lines for a specific purpose, such as a callback. It is unwise to pollute the global namespace with these kind of single use functions. For such an event you can create anonymous or lambda functions using <a target="_blank" href="http://php.net/manual/en/function.create-function.php">create_function</a>. Anonymous functions allow the creation of functions which have no specified name. An example is shown below:</p>

<div class="wp_codebox"><table width="100%" ><tr id="266347"><td class="code" id="2663code47"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #0000ff;">$str</span> = <span style="color: #DD1144;">&quot;hello world!&quot;</span>;
<span style="color: #0000ff;">$lambda</span> = <span style="color: #000000;">create_function</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'$match'</span>, <span style="color: #DD1144;">'return &quot;friend!&quot;;'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$str</span> = <span style="color: #000000;">preg_replace_callback</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'/world/'</span>, <span style="color: #0000ff;">$lambda</span>, <span style="color: #0000ff;">$str</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">echo</span> <span style="color: #0000ff;">$str</span> ;
&nbsp;
<span style="color: #808080; font-style: italic;">// Output</span>
<span style="color: #808080; font-style: italic;">// hello friend!</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Here we have created a small nameless (Anonymous) function which is used as a callback in the <em>preg_replace_callback</em> function. Although <em>create_function</em> lets you create anonymous functions, it is not truly a part of the language itself but a hack. PHP 5.3 introduced true Anonymous functions in the base language itself. We create a unnamed function and assign it to a variable, including whatever parameters the functions accepts and then simply use the variable like an actual function. A example is given below:</p>

<div class="wp_codebox"><table width="100%" ><tr id="266348"><td class="code" id="2663code48"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #0000ff;">$func</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000;">echo</span> <span style="color: #DD1144;">&quot;Hello $name<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #0000ff;">$func</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;world!&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$func</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;Sameer!&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Output-</span>
<span style="color: #808080; font-style: italic;">//Hello world!</span>
<span style="color: #808080; font-style: italic;">//Hello Sameer!</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Note the ending semicolon at the end of the defined function. This is because the function definition is actually a statement, and statements always ends with a semicolon. Another example is shown below.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266349"><td class="code" id="2663code49"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #0000ff;">$str</span> = <span style="color: #DD1144;">&quot;Hello World!&quot;</span>;
<span style="color: #0000ff;">$func</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$match</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000;">return</span> <span style="color: #DD1144;">&quot;friend!&quot;</span>;
<span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #0000ff;">$str</span> = <span style="color: #000000;">preg_replace_callback</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'/World/'</span>, <span style="color: #0000ff;">$func</span>, <span style="color: #0000ff;">$str</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">echo</span> <span style="color: #0000ff;">$str</span> ;
&nbsp;
<span style="color: #808080; font-style: italic;">// Output</span>
<span style="color: #808080; font-style: italic;">// Hello friend!</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Anonymous and Nested functions</h4>
<p>PHP allows functions to be nested inside one another. Although it seems like a side-effect of the parser rather then a design decision, it can be quite helpful in some situations. Take the example below. The function <em>censorString</em> takes a string as a parameter and replaces any censored word given with a string of &#8216;*&#8217;. The <em>censorString</em> functions defines a nested function <em>replace</em> that is used as a callback function by <em>preg_replace_callback</em>. Assuming that the &#8216;replace&#8217; function is only used by the <em>censorString</em> function in our program it is better to define it within <em>censorString</em> itself and avoid polluting the global namespace with small single use functions</p>

<div class="wp_codebox"><table width="100%" ><tr id="266350"><td class="code" id="2663code50"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> censorString<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$censor</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> replace<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$match</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000;">return</span> <span style="color: #000000;">str_repeat</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;*&quot;</span>, <span style="color: #000000;">strlen</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$match</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000;">return</span> <span style="color: #000000;">preg_replace_callback</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'/'</span>.<span style="color: #0000ff;">$censor</span>.<span style="color: #DD1144;">'/'</span>, <span style="color: #DD1144;">'replace'</span>, <span style="color: #0000ff;">$string</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000;">echo</span> censorString<span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;hello world!&quot;</span>, <span style="color: #DD1144;">&quot;world&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">echo</span> censorString<span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;hello world!&quot;</span>, <span style="color: #DD1144;">&quot;hello&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Output-</span>
<span style="color: #808080; font-style: italic;">// hello *****!</span>
<span style="color: #808080; font-style: italic;">// Fatal error: Cannot redeclare replace() </span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>When you define a nested function as above, the inner function does not come into existence until the parent function is executed. Once the parent function (censorString) is executed the inner function (replace) goes into global scope. Now you can access the inner function from anywhere in your current document. One problem though is that calling the parent function again in the current document will cause a redeclaration of the inner function, which will generate an error, as the inner function is already declared. A solution is to use a anonymous function as shown below. (Note again the semicolon at the end of the inner function.)</p>

<div class="wp_codebox"><table width="100%" ><tr id="266351"><td class="code" id="2663code51"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> censorString<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$censor</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$func</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$match</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000;">return</span> <span style="color: #000000;">str_repeat</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;*&quot;</span>, <span style="color: #000000;">strlen</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$match</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>;
&nbsp;
    <span style="color: #000000;">return</span> <span style="color: #000000;">preg_replace_callback</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'/'</span>.<span style="color: #0000ff;">$censor</span>.<span style="color: #DD1144;">'/'</span>, <span style="color: #0000ff;">$func</span>, <span style="color: #0000ff;">$string</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000;">echo</span> censorString<span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;hello world!&quot;</span>, <span style="color: #DD1144;">&quot;world&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">echo</span> censorString<span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;hello world!&quot;</span>, <span style="color: #DD1144;">&quot;hello&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Output-</span>
<span style="color: #808080; font-style: italic;">// hello *****!</span>
<span style="color: #808080; font-style: italic;">// ***** world!</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now whenever the <em>censorString</em> function is executed the inner anonymous function comes into existence. But unlike a normal nested function it goes out of scope once the parent function ends. So we can repeatedly call the <em>censorString</em> function without throwing a redeclaration error.</p>
<p>Another way is to define the function in the callback itself.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266352"><td class="code" id="2663code52"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> censorString<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$censor</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000;">return</span> <span style="color: #000000;">preg_replace_callback</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">'/'</span>.<span style="color: #0000ff;">$censor</span>.<span style="color: #DD1144;">'/'</span>, 
                                <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$match</span><span style="color: #66cc66;">&#41;</span> 
                                <span style="color: #66cc66;">&#123;</span>
                                    <span style="color: #000000;">return</span> <span style="color: #000000;">str_repeat</span><span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;*&quot;</span>, 
                                           <span style="color: #000000;">strlen</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$match</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                                <span style="color: #66cc66;">&#125;</span>,
                                <span style="color: #0000ff;">$string</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000;">echo</span> censorString<span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;hello world!&quot;</span>, <span style="color: #DD1144;">&quot;world&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">echo</span> censorString<span style="color: #66cc66;">&#40;</span><span style="color: #DD1144;">&quot;hello world!&quot;</span>, <span style="color: #DD1144;">&quot;hello&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Output-</span>
<span style="color: #808080; font-style: italic;">// hello *****!</span>
<span style="color: #808080; font-style: italic;">// ***** world!</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Closures</h4>
<p>Closures are anonymous functions that are aware of their surrounding context. In short these are anonymous functions which have knowledge about variables not defined within themselves. A simple example will make it clear. Say we want to create a anonymous function that returns a given number multiplied by 5.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266353"><td class="code" id="2663code53"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #0000ff;">$mult</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000;">return</span> <span style="color: #0000ff;">$x</span> * <span style="color: #cc66cc;">5</span>;
<span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #000000;">echo</span> <span style="color: #0000ff;">$mult</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Output-</span>
<span style="color: #808080; font-style: italic;">// 10</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>If we want to return a number multiplied by 7 rather then 5 ,we have to create another function and so on for other numbers. Instead of creating a series of different functions we can create a closure using the &#8216;use&#8217; construct, which allows variables outside the anonymous function to be accessible or &#8216;closed&#8217; within the current function.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266354"><td class="code" id="2663code54"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #0000ff;">$multiply</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$multiplier</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #66cc66;">&#41;</span> use <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$multiplier</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000;">return</span> <span style="color: #0000ff;">$x</span> * <span style="color: #0000ff;">$multiplier</span>;
    <span style="color: #66cc66;">&#125;</span>;
<span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// $mul5 now contains a function that returns a number multiplied by 5</span>
<span style="color: #0000ff;">$mult5</span> = <span style="color: #0000ff;">$multiply</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// $mul7 contains a function that returns a number multiplied by 7</span>
<span style="color: #0000ff;">$mult7</span> = <span style="color: #0000ff;">$multiply</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000;">echo</span> <span style="color: #0000ff;">$mult5</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">echo</span> <span style="color: #0000ff;">$mult7</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Output-</span>
<span style="color: #808080; font-style: italic;">// 25</span>
<span style="color: #808080; font-style: italic;">// 35</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Take another example along the above lines. Lets say we want to filter an array of number according to a certain criteria; say all the numbers above 100. The code for the same is given below. Note the use of a anonymous function.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266355"><td class="code" id="2663code55"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> filter<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$condition</span>, <span style="color: #0000ff;">$numbers</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$len</span> = <span style="color: #000000;">count</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numbers</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$filtered</span> = <span style="color: #000000;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Iterate through all the array elements */</span>
    <span style="color: #000000;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span> = <span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$i</span> &lt; <span style="color: #0000ff;">$len</span>; <span style="color: #0000ff;">$i</span>++<span style="color: #66cc66;">&#41;</span> 
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$num</span> = <span style="color: #0000ff;">$numbers</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">/* If the number satisfies the $condition, store
           it in the $filtered array
        */</span>
        <span style="color: #000000;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$condition</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$num</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$filtered</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$num</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #000000;">return</span> <span style="color: #0000ff;">$filtered</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* An array of random numbers */</span>
<span style="color: #0000ff;">$randomNumbers</span> = <span style="color: #000000;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">34</span>, <span style="color: #cc66cc;">56</span>, <span style="color: #cc66cc;">22</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">67</span>, <span style="color: #cc66cc;">897</span>, <span style="color: #cc66cc;">123</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">55</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$condition</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #66cc66;">&#123;</span> 
    <span style="color: #000000;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span> &gt; <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #000000; font-weight: bold;">true</span> : <span style="color: #000000; font-weight: bold;">false</span>; 
<span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #0000ff;">$greaterThan100</span> = filter<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$condition</span>, <span style="color: #0000ff;">$randomNumbers</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #000000;">print_r</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$greaterThan100</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Output</span>
<span style="color: #808080; font-style: italic;">// Array ( [0] =&gt; 897 [1] =&gt; 123 ) </span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now what if we want to allow all numbers above 400, then we have to change the anonymous function to the following.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266356"><td class="code" id="2663code56"><pre class="php">&nbsp;
<span style="color: #0000ff;">$condition</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #66cc66;">&#123;</span> 
    <span style="color: #000000;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span> &gt; <span style="color: #cc66cc;">400</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #000000; font-weight: bold;">true</span> : <span style="color: #000000; font-weight: bold;">false</span>; 
<span style="color: #66cc66;">&#125;</span>;</pre></td></tr></table></div>

<p>Rather then creating different functions for various criteria, we can create a closure.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266357"><td class="code" id="2663code57"><pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> filter<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$condition</span>, <span style="color: #0000ff;">$numbers</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$len</span> = <span style="color: #000000;">count</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numbers</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$filtered</span> = <span style="color: #000000;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Iterate through all the array elements */</span>
    <span style="color: #000000;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span> = <span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$i</span> &lt; <span style="color: #0000ff;">$len</span>; <span style="color: #0000ff;">$i</span>++<span style="color: #66cc66;">&#41;</span> 
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$num</span> = <span style="color: #0000ff;">$numbers</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">/* If the number satisfies the $condition, store
           it in the $filtered array
        */</span>
        <span style="color: #000000;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$condition</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$num</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$filtered</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$num</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #000000;">return</span> <span style="color: #0000ff;">$filtered</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* createFilter now returns a anonymous function */</span>
<span style="color: #000000; font-weight: bold;">function</span> createFilter<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$lowerBound</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #66cc66;">&#41;</span> use <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$lowerBound</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span> &gt; <span style="color: #0000ff;">$lowerBound</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #000000; font-weight: bold;">true</span> : <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #66cc66;">&#125;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* An array of random numbers */</span>
<span style="color: #0000ff;">$randomNumbers</span> = <span style="color: #000000;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">34</span>, <span style="color: #cc66cc;">56</span>, <span style="color: #cc66cc;">22</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">67</span>, <span style="color: #cc66cc;">897</span>, <span style="color: #cc66cc;">123</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">55</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* Create a new function and store it in $greaterThan400 */</span>
<span style="color: #0000ff;">$greaterThan400</span> = createFilter<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">400</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$greaterThan100</span> = createFilter<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000;">print_r</span><span style="color: #66cc66;">&#40;</span>filter<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$greaterThan400</span>, <span style="color: #0000ff;">$randomNumbers</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">print_r</span><span style="color: #66cc66;">&#40;</span>filter<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$greaterThan100</span>, <span style="color: #0000ff;">$randomNumbers</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Output</span>
<span style="color: #808080; font-style: italic;">// Array ( [0] =&gt; 897 ) </span>
<span style="color: #808080; font-style: italic;">// Array ( [0] =&gt; 897 [1] =&gt; 123 )</span></pre></td></tr></table></div>

<p>Note that in the above example when <em>createFilter</em> exists, normally the $lowerBound variable goes out of scope, but because we have used closure here using the &#8216;use&#8217; keyword, the inner anonymous function binds the $lowerBound variable with itself even after the <em>createFilter</em> function exists. This is what we call closure. The inner function &#8216;closes&#8217; over the variables of the outer function in which it is defined.</p>
<p>We can do a var_dump on the $greaterThan400 and $greaterThan100 objects to see if the inner function really carries the $lowerBound variable with itself.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266358"><td class="code" id="2663code58"><pre class="php"><span style="color: #000000;">var_dump</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$greaterThan400</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000;">var_dump</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$greaterThan100</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Which returns the following:</p>

<div class="wp_codebox"><table width="100%" ><tr id="266359"><td class="code" id="2663code59"><pre class="php">&nbsp;
object<span style="color: #66cc66;">&#40;</span>Closure<span style="color: #66cc66;">&#41;</span><span style="color: #808080; font-style: italic;">#1 (2) {</span>
  <span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">&quot;static&quot;</span><span style="color: #66cc66;">&#93;</span>=&gt;
  <span style="color: #000000;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">&quot;lowerBound&quot;</span><span style="color: #66cc66;">&#93;</span>=&gt;
    int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">400</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">&quot;parameter&quot;</span><span style="color: #66cc66;">&#93;</span>=&gt;
  <span style="color: #000000;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">&quot;$x&quot;</span><span style="color: #66cc66;">&#93;</span>=&gt;
    string<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #DD1144;">&quot;&lt;required&gt;&quot;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
object<span style="color: #66cc66;">&#40;</span>Closure<span style="color: #66cc66;">&#41;</span><span style="color: #808080; font-style: italic;">#2 (2) {</span>
  <span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">&quot;static&quot;</span><span style="color: #66cc66;">&#93;</span>=&gt;
  <span style="color: #000000;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">&quot;lowerBound&quot;</span><span style="color: #66cc66;">&#93;</span>=&gt;
    int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">&quot;parameter&quot;</span><span style="color: #66cc66;">&#93;</span>=&gt;
  <span style="color: #000000;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #DD1144;">&quot;$x&quot;</span><span style="color: #66cc66;">&#93;</span>=&gt;
    string<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #DD1144;">&quot;&lt;required&gt;&quot;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Or better yet we can use the Reflection API.</p>

<div class="wp_codebox"><table width="100%" ><tr id="266360"><td class="code" id="2663code60"><pre class="php"><span style="color: #000000;">echo</span> ReflectionFunction::<span style="color: #990000;">export</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$greaterThan400</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Which gives the following:</p>

<div class="wp_codebox"><table width="100%" ><tr id="266361"><td class="code" id="2663code61"><pre class="php">Closure <span style="color: #66cc66;">&#91;</span> &lt;user&gt; <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #66cc66;">&#123;</span>closure<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
  @@ D:\localhost\test\\index.php <span style="color: #cc66cc;">27</span> - <span style="color: #cc66cc;">30</span>
&nbsp;
  - Bound Variables <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
      Variable <span style="color: #808080; font-style: italic;">#0 [ $lowerBound ]</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  - Parameters <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
    Parameter <span style="color: #808080; font-style: italic;">#0 [ &lt;required&gt; $x ]</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h4>In closing</h4>
<p>Lambda functions and closures have taken PHP a notch closer towards other modern languages. In practice how much people really use lambdas and closures in their daily work remains to be seen. I&#8217;m still getting a hang of it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/anonymous-functions-in-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to Fix PHP Vulnerabilities (So Your Site Won&#8217;t Get Hacked)</title>
		<link>http://www.codediesel.com/php/how-to-fix-php-vulnerabilities/</link>
		<comments>http://www.codediesel.com/php/how-to-fix-php-vulnerabilities/#comments</comments>
		<pubDate>Mon, 24 May 2010 05:54:28 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2660</guid>
		<description><![CDATA[How to Fix PHP Vulnerabilities]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2010/05/the_Crooked_Man.png"><img src="http://www.codediesel.com/wp-content/uploads/2010/05/the_Crooked_Man.png" alt="" title="the_Crooked_Man" width="222" height="250" class="alignleft size-full wp-image-2662" style="border: none;" /></a></p>
<p>As a programming language, PHP has many advantages but security has always been a major issue. Partially these security problems are inherent to the language itself because PHP was meant to be an easy and powerful programming language, while security came second. However, when you add bad coding and non-adherence to even the basic security rules, the situation gets out of control. </p>
<p>Fortunately, it is possible to fix PHP vulnerabilities and make PHP applications more secure. Some of the defenses are common for all programming languages, while others are found only in PHP. Here are some of the best defenses you have when you want to fix PHP vulnerabilities and make your site more secure.<br />
<span id="more-2660"></span></p>
<h4>Defend Your Code Against SQL Injections</h4>
<p>SQL Injection is one of the most typical PHP vulnerabilities and many hackers take advantage of it. In order to prevent SQL injections, you need to always check input data and to escape characters (such as single quotes (&#8216;) or double quotes (&#8220;)). If you do it, it won&#8217;t be possible to execute malicious SQL queries, which take control over your database or compromise the security of your site in other ways. </p>
<p>The two most common defenses against SQL injections are the use of the  <a target="_blank" href="http://php.net/manual/en/function.mysql-real-escape-string.php">mysql_real_escape_string</a> and <a target="_blank" href="http://php.net/manual/en/book.pdo.php">PDO</a>. The mysql_real_escape_string prepends special characters and as a result these special characters are not sent directly to the MySQL database. It is recommendable to use <em>mysql_real_escape_string</em> on all input variables, which are sent to the MySQL database.<br />
PDO adds an abstraction layer to your code, thus making it more secure. PDO prepares a statement for execution and returns a statement object. </p>
<h4>Don&#8217;t Leave Room for Cross-Site Scripting Vulnerabilities</h4>
<p>Cross-Site Scripting (XSS) is also common in PHP. Again, the defense against XSS isn&#8217;t rocket science. If the users input HTML data is escaped properly, your code won&#8217;t be vulnerable against XSS. If you don&#8217;t do it, then a hacker can insert any HTML code (or even Javascript) he or she likes and modify your page, so that he or she can steal data from users. </p>
<p>The best defense against XSS is to use the htmlspecialchars() function. This function identifies any output you wouldn&#8217;t like to be considered as HTML output. While you can never be certain that XSS is impossible, the htmlspecialchars() function will make it harder for a hacker to succeed. </p>
<h4>Watch out for File Inclusion Vulnerabilities</h4>
<p>Of all PHP vulnerabilities, file inclusion attacks are the most severe. A file inclusion attack gives the hacker the opportunity to include a random file and to deploy it on your server. File inclusion attacks are possible when the register_globals directive is on, which means that unchecked input variables are allowed. The best defense against file inclusion vulnerabilities is to mind how you use PHP include() functions. If you are not sure you can use these functions properly, you&#8217;d better avoid any include statements – just use switch statements with hard coded strings and this will help to avoid file inclusion vulnerabilities. </p>
<h4>Don&#8217;t Forget to Initialize Variables</h4>
<p>If you program in many other languages, then you maybe don&#8217;t need to be told explicitly to initialize variables because you already have the habit of doing it. However, if you are mainly a PHP programmer, maybe you don&#8217;t always initialize variables because in PHP, unlike in many other programming languages, a variable can be used without being initialized first. From a security point of view, uninitialized variables are a huge risk and this is why you should never use them. </p>
<h4>Don&#8217;t Leave the <em>register_globals</em> Directive ON</h4>
<p>File inclusion attacks aren&#8217;t the only evil the <em>register_globals</em> directive brings to your code. The register_globals directive is very powerful but unfortunately its power is easily abused. In recent versions of PHP the directive register_globals is OFF by default and in PHP 6 it is altogether removed but if you are using earlier versions of PHP, take the time and check if it isn&#8217;t ON by accident. </p>
<h4>Encryption Always Helps</h4>
<p>No matter which programming language you use, encryption always helps. It doesn&#8217;t matter how secure your PHP code is when you send sensitive data unencrypted and anybody can read it. The safest form of encryption is end-to-end encryption but it takes a lot of resources and it might be hard to implement. This is why it is acceptable if you encrypt at least passwords, credit card numbers, and other similar data. Don&#8217;t leave sensitive data unencrypted because this is what hackers want most. </p>
<h4>Test Your PHP Code with Tools</h4>
<p>There are many PHP tools to test the security of your code with. Sure, you should do your best to write secure code, adhere to security practices, and carefully review your code for errors but an automated tool to check your code with is always useful. Some of the best tools to test PHP vulnerabilities with are <a target="_blank" href="http://phpsec.org/projects/phpsecinfo/">PhpSecInfo</a>, <a target="_blank" href="http://sourceforge.net/projects/securityscanner/">PHP Security Scanner</a>, and <a target="_blank" href="http://developer.spikesource.com/projects/phpsecaudit">Spike PHP Security Audit Tool</a>. Run them on your code and see what they will find. </p>
<h4>Further Reading</h4>
<p>These steps are just the beginning to make your PHP code secure. You must always take at least these steps because if you don&#8217;t you leave the door wide open to hackers. On the other hand, even if you do everything we described here, you can never be sure that no vulnerabilities exist. There is much more to PHP security and if you want to expand your knowledge, read the <a target="_blank" href="http://php.net/manual/en/security.php">PHP Security manual</a> and this paper. Both of them will tell you more about how to fix PHP vulnerabilities and make your site secure. </p>
<blockquote><p>
This guest article was written by Christopher Shepard of <a target="_Blank"  href="http://www.webhostgear.com/">Webhost Gear</a>, a website that provides information about hosting and reviews of the most popular web hosting services, as well as technical and website maintenance tutorials.
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/how-to-fix-php-vulnerabilities/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Generating random data on the client side</title>
		<link>http://www.codediesel.com/data/generating-random-data-on-the-client-side/</link>
		<comments>http://www.codediesel.com/data/generating-random-data-on-the-client-side/#comments</comments>
		<pubDate>Fri, 21 May 2010 10:07:31 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[libraries]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2658</guid>
		<description><![CDATA[Automatically creating fake or sample data is a frequent requirement for front-end web developers. Although usually not tedious, there are times when you need to quickly and automatically generate structured data for your html forms or CMS systems for testing purposes.
Faker.js is a JavaScript implementation inspired by Benjamin Curtis&#8217;s Ruby Gem Faker and Perl&#8217;s Data::Faker [...]]]></description>
			<content:encoded><![CDATA[<p>Automatically creating fake or sample data is a frequent requirement for front-end web developers. Although usually not tedious, there are times when you need to quickly and automatically generate structured data for your html forms or CMS systems for testing purposes.</p>
<p><a target="_blank" href="http://github.com/Marak/Faker.js">Faker.js</a> is a JavaScript implementation inspired by Benjamin Curtis&#8217;s Ruby Gem <a href="http://faker.rubyforge.org/">Faker</a> and Perl&#8217;s <a href="http://search.cpan.org/~jasonk/Data-Faker-0.07/lib/Data/Faker.pm">Data::Faker</a> that lets you generate commonly required data quickly. You can check the <a href="http://maraksquires.com/Faker.js/">demo page</a> to get an idea.<br />
<span id="more-2658"></span></p>
<h4>Getting Fake.js</h4>
<p>You can download the library form <a href="http://github.com/Marak/Faker.js">github</a> and include in your pages where you need to generate the data.</p>

<div class="wp_codebox"><table width="100%" ><tr id="265870"><td class="code" id="2658code70"><pre class="javascript">..
&lt;script src = <span style="color: #3366CC;">&quot;Faker/Faker.js&quot;</span> type = <span style="color: #3366CC;">&quot;text/javascript&quot;</span>&gt;&lt;/script&gt;
..</pre></td></tr></table></div>

<h4>Generating fake data</h4>
<p>The API is divided into various sections as show below:</p>

<div class="wp_codebox"><table width="100%" ><tr id="265871"><td class="code" id="2658code71"><pre class="text"># Name
    * findName
# Address
    * zipCode
    * city
    * streetName
    * streetAddress
    * secondaryAddress
    * ukCounty
    * ukCountry
# PhoneNumber
    * phoneNumber
# Internet
    * email
    * userName
    * domainName
    * domainWord
...</pre></td></tr></table></div>

<p>So if you need to generate a random email address or a zip code you could do it like this:</p>

<div class="wp_codebox"><table width="100%" ><tr id="265872"><td class="code" id="2658code72"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> randomEmail = Faker.<span style="color: #006600;">Internet</span>.<span style="color: #006600;">email</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> randomZip = Faker.<span style="color: #006600;">Address</span>.<span style="color: #006600;">zipCode</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Some sample API methods are shown below, but there are more of them which you can find <a target="_blank" href="http://github.com/Marak/Faker.js">here</a> in the API section.</p>

<div class="wp_codebox"><table width="100%" ><tr id="265873"><td class="code" id="2658code73"><pre class="javascript">&lt;script&gt;
<span style="color: #003366; font-weight: bold;">var</span> longParagraph = Faker.<span style="color: #006600;">Lorem</span>.<span style="color: #006600;">paragraphs</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> singleSentence = Faker.<span style="color: #006600;">Lorem</span>.<span style="color: #006600;">sentence</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> companyName= Faker.<span style="color: #006600;">Company</span>.<span style="color: #006600;">companyName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> catchPhrase = Faker.<span style="color: #006600;">Company</span>.<span style="color: #006600;">catchPhrase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&lt;/script&gt;</pre></td></tr></table></div>

<p>One of the interesting methods is &#8216; Faker.Company.catchPhrase()&#8217;, which generates catchy technological phrases. A kind of a tag line generator for your new multi-million dollar company <img src='http://www.codediesel.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> . A sample output is shown below.</p>

<div class="wp_codebox"><table width="100%" ><tr id="265874"><td class="code" id="2658code74"><pre class="text">Stand-alone 5th generation utilisation
Ergonomic explicit focus group
Horizontal human-resource solution
Automated contextually-based knowledge base
Distributed multi-state encoding
Extended zero administration interface
Proactive coherent productivity
Streamlined national approach
Total transitional algorithm
Stand-alone fault-tolerant moderator</pre></td></tr></table></div>

<p>The API also includes several helper methods that lets you create bulk fake data using the single API methods. For example you can generate complete user information templates cards by using a single helper method <span class="method">&#8216;Faker.Helpers.userCard()&#8217;</span>. A sample run is shown below:</p>

<div class="wp_codebox"><table width="100%" ><tr id="265875"><td class="code" id="2658code75"><pre class="javascript"><span style="color: #000066;">name</span>: Blair Nikolaus
username: Fernando_Olson
email: Jacklyn_Brekke@aurore.<span style="color: #000066;">name</span>
address:street: Kozey Meadow
suite: Apt. <span style="color: #CC0000;">664</span>
city: Felipastad
zipcode: <span style="color: #CC0000;">88071</span>
&nbsp;
phone: <span style="color: #CC0000;">571.540</span><span style="color: #CC0000;">.8605</span> x136
website: reba.<span style="color: #006600;">co</span>.<span style="color: #006600;">uk</span>
company:<span style="color: #000066;">name</span>: Kuhic and Sons
catchPhrase: Integrated solution-oriented Graphical User Interface
bs: seize scalable web services</pre></td></tr></table></div>

<p>The &#8216;Faker.Helpers.userCard()&#8217; method returns a nested object, so you will need to recursively traverse the object to extract the data. A simple function to do the same is given below:</p>

<div class="wp_codebox"><table width="100%" ><tr id="265876"><td class="code" id="2658code76"><pre class="javascript">&lt;script&gt;
<span style="color: #009900; font-style: italic;">// Recursively traverse a nested Javascript object and return it.</span>
<span style="color: #003366; font-weight: bold;">function</span> printObj<span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> prop, res = <span style="color: #3366CC;">''</span>;
&nbsp;
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #66cc66;">&#40;</span>prop <span style="color: #000066; font-weight: bold;">in</span> obj<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> obj<span style="color: #66cc66;">&#91;</span>prop<span style="color: #66cc66;">&#93;</span> == <span style="color: #3366CC;">'object'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        res += prop + <span style="color: #3366CC;">':'</span> + printObj<span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#91;</span>prop<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #3366CC;">'&lt;br&gt;'</span>;
    <span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #66cc66;">&#123;</span>
        res += prop + <span style="color: #3366CC;">': '</span> + obj<span style="color: #66cc66;">&#91;</span>prop<span style="color: #66cc66;">&#93;</span> + <span style="color: #3366CC;">'&lt;br&gt;'</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
   <span style="color: #000066; font-weight: bold;">return</span> res;
<span style="color: #66cc66;">&#125;</span>
&lt;/script&gt;</pre></td></tr></table></div>

<p>So now you can do something like this:</p>

<div class="wp_codebox"><table width="100%" ><tr id="265877"><td class="code" id="2658code77"><pre class="javascript">...
<span style="color: #003366; font-weight: bold;">var</span> obj = Faker.<span style="color: #006600;">Helpers</span>.<span style="color: #006600;">userCard</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span> printObj<span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
...</pre></td></tr></table></div>

<h4>Another Randomizer library</h4>
<p><a target="_blank" href="http://code.google.com/p/clubajax/source/browse/">Club AJAX Mock Data Randomizer</a> library is another library that lets you generate random data quickly. This library lets you generate random dates, colors, boolean values which is lacking in the Faker library. Check a simple <a href="http://www.codediesel.com/data/scripts/random/index.html">demo</a> using this library.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/data/generating-random-data-on-the-client-side/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
