<?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"
	>

<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>/* fueling the web */</description>
	<pubDate>Wed, 02 Jul 2008 14:39:22 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>Linked List implementation in PHP</title>
		<link>http://www.codediesel.com/php/linked-list-in-php/</link>
		<comments>http://www.codediesel.com/php/linked-list-in-php/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 17:24:02 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[algorithms]]></category>

		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=54</guid>
		<description><![CDATA[Data structures are one of the core elements of computer science and they are also fun to program. But being a web developer the opportunity for implementing them in any application is quite rare. But then I thought, What the heck! I can just code for the joy of it, and it will also help [...]]]></description>
			<content:encoded><![CDATA[<p>Data structures are one of the core elements of computer science and they are also fun to program. But being a web developer the opportunity for implementing them in any application is quite rare. But then I thought, What the heck! I can just code for the joy of it, and it will also help me brush up on my DS skills. So here it is, a single linked list implementation in PHP for whoever may care. I will be posting other data structure and algorithm implementations here, so keeping watching.</p>
<p><strong>CODE:</strong></p>

<div class="wp_codebox"><table width="100%" ><tr id="543"><td class="code" id="54code3"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ListNode
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* Data to hold */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #0000ff;">$data</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Link to next node */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #0000ff;">$next</span>;
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Node constructor */</span>
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">data</span> = <span style="color: #0000ff;">$data</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">next</span> = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> readNode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">data</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> LinkList
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* Link to the first node in the list */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$firstNode</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Link to the last node in the list */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$lastNode</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Total nodes in the list */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066;">static</span> <span style="color: #0000ff;">$count</span>;
&nbsp;
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* List constructor */</span>
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span> = <span style="color: #000000; font-weight: bold;">NULL</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">lastNode</span> = <span style="color: #000000; font-weight: bold;">NULL</span>;
        self::<span style="color: #0000ff;">$count</span> = <span style="color: #cc66cc;">0</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isEmpty<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span> == <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insertFirst<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$link</span> = <span style="color: #000000; font-weight: bold;">new</span> ListNode<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$link</span>-&gt;<span style="color: #006600;">next</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span> = &amp;<span style="color: #0000ff;">$link</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">/* If this is the first node inserted in the list
           then also set the lastNode pointer to it.
        */</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">lastNode</span> == <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">lastNode</span> = &amp;<span style="color: #0000ff;">$link</span>;
&nbsp;
        self::<span style="color: #0000ff;">$count</span>++;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insertLast<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span> != <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$link</span> = <span style="color: #000000; font-weight: bold;">new</span> ListNode<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">lastNode</span>-&gt;<span style="color: #006600;">next</span> = <span style="color: #0000ff;">$link</span>;
            <span style="color: #0000ff;">$link</span>-&gt;<span style="color: #006600;">next</span> = <span style="color: #000000; font-weight: bold;">NULL</span>;
            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">lastNode</span> = &amp;<span style="color: #0000ff;">$link</span>;
            self::<span style="color: #0000ff;">$count</span>++;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">else</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">insertFirst</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> deleteFirstNode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$temp</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>-&gt;<span style="color: #006600;">next</span>;
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span> != <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
            self::<span style="color: #0000ff;">$count</span>--;
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$temp</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> deleteLastNode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span> != <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>-&gt;<span style="color: #006600;">next</span> == <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span> = <span style="color: #000000; font-weight: bold;">NULL</span>;
                self::<span style="color: #0000ff;">$count</span>--;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">else</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$previousNode</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>;
                <span style="color: #0000ff;">$currentNode</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>-&gt;<span style="color: #006600;">next</span>;
&nbsp;
                <span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$currentNode</span>-&gt;<span style="color: #006600;">next</span> != <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #0000ff;">$previousNode</span> = <span style="color: #0000ff;">$currentNode</span>;
                    <span style="color: #0000ff;">$currentNode</span> = <span style="color: #0000ff;">$currentNode</span>-&gt;<span style="color: #006600;">next</span>;
                <span style="color: #66cc66;">&#125;</span>
&nbsp;
                <span style="color: #0000ff;">$previousNode</span>-&gt;<span style="color: #006600;">next</span> = <span style="color: #000000; font-weight: bold;">NULL</span>;
                self::<span style="color: #0000ff;">$count</span>--;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> deleteNode<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$current</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>;
        <span style="color: #0000ff;">$previous</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>;
&nbsp;
        <span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">data</span> != <span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">next</span> == <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
            <span style="color: #b1b100;">else</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$previous</span> = <span style="color: #0000ff;">$current</span>;
                <span style="color: #0000ff;">$current</span> = <span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">next</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$current</span> == <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>-&gt;<span style="color: #006600;">next</span>;
        <span style="color: #b1b100;">else</span>
            <span style="color: #0000ff;">$previous</span>-&gt;<span style="color: #006600;">next</span> = <span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">next</span>;
&nbsp;
        self::<span style="color: #0000ff;">$count</span>--;   
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> find<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$current</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>;
        <span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">data</span> != <span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">next</span> == <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
            <span style="color: #b1b100;">else</span>
                <span style="color: #0000ff;">$current</span> = <span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">next</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$current</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> readNode<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$nodePos</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$nodePos</span> &lt;= self::<span style="color: #0000ff;">$count</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$current</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>;
            <span style="color: #0000ff;">$pos</span> = <span style="color: #cc66cc;">1</span>;
            <span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pos</span> != <span style="color: #0000ff;">$nodePos</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">next</span> == <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
                <span style="color: #b1b100;">else</span>
                    <span style="color: #0000ff;">$current</span> = <span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">next</span>;
&nbsp;
                <span style="color: #0000ff;">$pos</span>++;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">data</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">else</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> totalNodes<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> self::<span style="color: #0000ff;">$count</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> readList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$listData</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$current</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>;
&nbsp;
        <span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$current</span> != <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066;">array_push</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$listData</span>, <span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">readNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #0000ff;">$current</span> = <span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">next</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$listData</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> reverseList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span> != <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>-&gt;<span style="color: #006600;">next</span> != <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$current</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span>;
                <span style="color: #0000ff;">$new</span> = <span style="color: #000000; font-weight: bold;">NULL</span>;
&nbsp;
                <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$current</span> != <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #0000ff;">$temp</span> = <span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">next</span>;
                    <span style="color: #0000ff;">$current</span>-&gt;<span style="color: #006600;">next</span> = <span style="color: #0000ff;">$new</span>;
                    <span style="color: #0000ff;">$new</span> = <span style="color: #0000ff;">$current</span>;
                    <span style="color: #0000ff;">$current</span> = <span style="color: #0000ff;">$temp</span>;
                <span style="color: #66cc66;">&#125;</span>
                <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">firstNode</span> = <span style="color: #0000ff;">$new</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>PHPUnit test :</strong></p>

<div class="wp_codebox"><table width="100%" ><tr id="544"><td class="code" id="54code4"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'linklist.class.php'</span>;
<span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'PHPUnit/Framework.php'</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> LinkListTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testLinkList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #0000ff;">$totalNodes</span> = <span style="color: #cc66cc;">100</span>;
&nbsp;
        <span style="color: #0000ff;">$theList</span> = <span style="color: #000000; font-weight: bold;">new</span> LinkList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span>=<span style="color: #cc66cc;">1</span>; <span style="color: #0000ff;">$i</span> &lt;= <span style="color: #0000ff;">$totalNodes</span>; <span style="color: #0000ff;">$i</span>++<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">insertLast</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$totalNodes</span>, <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">totalNodes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span>=<span style="color: #cc66cc;">1</span>; <span style="color: #0000ff;">$i</span> &lt;= <span style="color: #0000ff;">$totalNodes</span>; <span style="color: #0000ff;">$i</span>++<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">insertFirst</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0000ff;">$totalNodes</span> = <span style="color: #0000ff;">$totalNodes</span> * <span style="color: #cc66cc;">2</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$totalNodes</span>, <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">totalNodes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">reverseList</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$totalNodes</span>, <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">totalNodes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">deleteFirstNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$totalNodes</span> - <span style="color: #cc66cc;">1</span>, <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">totalNodes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">deleteLastNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$totalNodes</span> - <span style="color: #cc66cc;">2</span>, <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">totalNodes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">/* Delete node which has a value of '5' */</span>
        <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">deleteNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$totalNodes</span> - <span style="color: #cc66cc;">3</span>, <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">totalNodes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">/* Insert a node at the end of the list with a value of '22' */</span>
        <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">insertLast</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">22</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$totalNodes</span> - <span style="color: #cc66cc;">2</span>, <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">totalNodes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">/* Find a node with a value of '25' (is in the list) */</span>
        <span style="color: #0000ff;">$found</span> = <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>, <span style="color: #0000ff;">$found</span>-&gt;<span style="color: #006600;">data</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">/* Find a node with a value of '125' (is not in the list) */</span>
        <span style="color: #0000ff;">$found</span> = <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">125</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertNull</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$found</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">/* Return the data stored in the node at position '50'
           which is in the list */</span>
        <span style="color: #0000ff;">$data</span> = <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">readNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span>, <span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">/* Return the data stored in the node at position '450'
           which is not in the list */</span>
        <span style="color: #0000ff;">$data</span> = <span style="color: #0000ff;">$theList</span>-&gt;<span style="color: #006600;">readNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">450</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertNull</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><a class="download" href="http://www.codediesel.com/data/code/link_list_class.zip">Download code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/linked-list-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Benchmarking PHP code</title>
		<link>http://www.codediesel.com/php/benchmarking-php-code/</link>
		<comments>http://www.codediesel.com/php/benchmarking-php-code/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 10:36:28 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[php]]></category>

		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=50</guid>
		<description><![CDATA[Code tuning is an essential part of programming. But most programmers would rather try to complete the project in hand then spend time optimizing a piece of code. A well tuned and optimized code block can speedup your application by an order of magnitude or even more.
Many people confuse profiling with benchmarking. A profiler is [...]]]></description>
			<content:encoded><![CDATA[<p>Code tuning is an essential part of programming. But most programmers would rather try to complete the project in hand then spend time optimizing a piece of code. A well tuned and optimized code block can speedup your application by an order of magnitude or even more.</p>
<p>Many people confuse profiling with benchmarking. A profiler is a analysis tool that lets you measure the performance of code at runtime. Profiling an application lets you find bottlenecks in your application code. You can find which part of your code is taking most of the time, you can than optimize that part of code. For example if there are 4 functions in  your application, profiling may report the following distribution:</p>
<p>function a: 68%<br />
function b: 12%<br />
function c: 1.3%<br />
function d: 18.7%</p>
<p>i.e function &#8216;a&#8217; accounts for about 70% of the execution time in your application; if you can optimize that function it will boost your application performance considerably. If you had executed the above code on a laptop, and than executed the same on a fast server the code may run in half the time, but the distribution will remain the same. The distribution is what is important in profiling.</p>
<p>Benchmarking is different. It lets you find exactly how long it takes to execute a piece of code. For example I have developed two versions of a sorting algorithm for a application : Sort1(), and Sort2(). If I&#8217;ve to find which is faster than I can use a benchmarking software for the same.</p>
<p>To take a contrived example, I recently implemented a Linked List class in PHP (brushing up on my data structure skills), with all the usual functions a linked-list requires - &#8216;<em>deleteNode</em>&#8216;, &#8216;<em>insertFirstNode</em>&#8216;, &#8216;<em>insertLastNode</em>&#8216;, &#8216;<em>reverseList</em>&#8216; etc. I was quite happy with the outcome untill I ran a benchmark between two functions - &#8216;<em>insertFirstNode</em>&#8216; and &#8216;<em>insertLastNode</em>&#8216;. Here is a benchmark report for the two functions for various amount of nodes, in table and graph format. The last column shows by how much the &#8216;<em>insertLastNode</em>&#8216; is slower than the &#8216;<em>insertFirstNode</em>&#8216; function.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/06/benchmark.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/06/benchmark.gif" alt="" title="benchmark" width="393" height="553" class="aligncenter size-full wp-image-51" /></a><br />
<a href='http://www.codediesel.com/wp-content/uploads/2008/06/benchmark2.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/06/benchmark2.gif" alt="benchmark2" title="benchmark2" width="460" height="348" class="aligncenter size-full wp-image-52" /></a></p>
<p>As you can see from the report &#8216;<em>insertLastNode</em>&#8216; takes exponential time to insert nodes. For example, when inserting 3000 nodes, &#8216;<em>insertLastNode</em>&#8216; is 30 times slower then &#8216;<em>insertFirstNode</em>&#8216;. The reason for this is that &#8216;<em>insertLastNode</em>&#8216; has to traverse the entire list to find the last node before adding a new node, while &#8216;<em>insertFirstNode</em>&#8216; always adds a new node at the beginning of the list. The simple solution as all you &#8216;data structure fans&#8217; know is to add a pointer that points to the last node of the list; so every time you have to add a node to the end of the list you use the end node pointer. After making the necessary changes I ran the benchmark and here are the results.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/06/benchmark3.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/06/benchmark3.gif" alt="benchmark3" title="benchmark3" width="457" height="347" class="aligncenter size-full wp-image-53" /></a></p>
<p>As you can see now both the functions take an equal amount of time to insert a node. Before the code tuning &#8216;insertLastNode&#8217; took more than 6 seconds to insert 3000 nodes, while after tuning less than quarter of a second.</p>
<p>The whole point of this exercise is to tell you that code tuning can make a tremendous difference to how your application performs. </p>
<p><strong>Installation</strong><br />
You can download the Benchmark pacakage from <a href="http://pear.php.net/package/Benchmark">here</a> or use the pear installer like below:</p>

<div class="wp_codebox"><table width="100%" ><tr id="508"><td class="code" id="50code8"><pre class="php">c:\pear install Benchmark</pre></td></tr></table></div>

<p><strong>Using the package:</strong><br />
Using the package for benchmarking is simple enough. A implementation is shown below.</p>

<div class="wp_codebox"><table width="100%" ><tr id="509"><td class="code" id="50code9"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* The following sample function is what we
       want to test */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> someFunction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span>=<span style="color: #cc66cc;">1</span>; <span style="color: #0000ff;">$i</span> &lt; <span style="color: #cc66cc;">1000</span>; <span style="color: #0000ff;">$i</span>++<span style="color: #66cc66;">&#41;</span>
            <span style="color: #0000ff;">$d</span> = <span style="color: #000066;">sqrt</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* include the PEAR::Benchmark package */</span>
    <span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'Benchmark/Iterate.php'</span>;
&nbsp;
    <span style="color: #0000ff;">$bench</span> = <span style="color: #000000; font-weight: bold;">new</span> Benchmark_Iterate;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Run the above function 100 times.
       If you want to pass parameters to the function,
       you can add it after the function name like this:
       $bench-&gt;run(100, 'someFunction', para1, para2, para n);
    */</span>
    <span style="color: #0000ff;">$bench</span>-&gt;<span style="color: #006600;">run</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>, <span style="color: #ff0000;">'someFunction'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Get the results */</span>
    <span style="color: #0000ff;">$result</span> = <span style="color: #0000ff;">$bench</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Display the mean time the function 'someFunction'
       takes to execute */</span>
    <span style="color: #000066;">print</span> <span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'mean'</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Output:</strong></p>

<div class="wp_codebox"><table width="100%" ><tr id="5010"><td class="code" id="50code10"><pre class="php"><span style="color: #cc66cc;">0.022045</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/benchmarking-php-code/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Adding HeatMaps to your website</title>
		<link>http://www.codediesel.com/php/heatmaps-for-your-website/</link>
		<comments>http://www.codediesel.com/php/heatmaps-for-your-website/#comments</comments>
		<pubDate>Tue, 13 May 2008 15:57:39 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[php]]></category>

		<category><![CDATA[visualization]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=46</guid>
		<description><![CDATA[ClickHeat is a wonderful PHP software that lets you add Heatmaps for your webpages.  Heatmaps are visual representation of clicks on a HTML page, showing hot and cold click zones. It can be useful to see at a glance which webpage areas are getting the most clicks as this areas get redder in color [...]]]></description>
			<content:encoded><![CDATA[<p><a title="clickheat" href="http://www.labsmedia.com/clickheat/index.html" target="_blank">ClickHeat </a>is a wonderful PHP software that lets you add Heatmaps for your webpages.  Heatmaps are visual representation of clicks on a HTML page, showing hot and cold click zones. It can be useful to see at a glance which webpage areas are getting the most clicks as this areas get redder in color while the areas receiving less clicks are white. The software has many options to tweak, and the heatmaps can be added on a per page basis.</p>
<p>A sample heatmap image is shown below. You can also use the clickheat class in your own PHP applications. The software doesn&#8217;t require any database, only GD graphics library must be enabled on the server, which in most cases already is.</p>
<p>You can view a heatmap for the index page for this site <a title="heatmap" href="http://www.codediesel.com/clickheat/index.php" target="_self">here</a>. I&#8217;ve presently only added click log for the index page, so try clicking on the index page links to update the heatmap.</p>
<p>username : <strong>visitor</strong><br />
password: <strong>visitor<br />
</strong></p>
<p>If you don&#8217;t see any changes in the heatmap, click on the &#8220;log my clicks&#8221; link in the right corner or try logging out and then logging in again.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2008/05/heatmap.gif"><img class="alignnone size-full wp-image-47" title="heatmap" src="http://www.codediesel.com/wp-content/uploads/2008/05/heatmap.gif" alt="heatmap" width="465" height="269" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/heatmaps-for-your-website/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Which is the most popular version of PHP today</title>
		<link>http://www.codediesel.com/php/which-is-the-most-popular-version-of-php-today/</link>
		<comments>http://www.codediesel.com/php/which-is-the-most-popular-version-of-php-today/#comments</comments>
		<pubDate>Fri, 09 May 2008 09:20:58 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=37</guid>
		<description><![CDATA[The graphic below displays the market share of each PHP version as on march 2008. The blue graph represents the sum of all preceding versions.
PHP 5.2.5 now is the most popular version of PHP followed closely by PHP 4.4.8 and 4.4.7. This information can be helpful if you are planning on creating an Open source [...]]]></description>
			<content:encoded><![CDATA[<p>The graphic below displays the market share of each PHP version as on march 2008. The blue graph represents the sum of all preceding versions.</p>
<p>PHP 5.2.5 now is the most popular version of PHP followed closely by PHP 4.4.8 and 4.4.7. This information can be helpful if you are planning on creating an Open source project or are thinking of moving past the version 4 mark in your professional career but are hesitant regarding the version 5 support on servers.<span id="more-37"></span></p>
<p><a title="graph" href="http://www.nexen.net/images/stories/phpversion/200804/versions.en.png" target="_blank"><img class="aligncenter size-full wp-image-39" title="popular php versions" src="http://www.codediesel.com/wp-content/uploads/2008/05/versionsenmini1.png" alt="popular php versions" width="430" height="322" /></a></p>
<p>The below two graphs show the market share of various PHP versions for April 2007 and April 2008.</p>
<p><a href="http://www.nexen.net/images/stories/phpversion/200704/majeure.en.png" target="_blank"><img class="center size-medium wp-image-44" title="php versions 2007" src="http://www.codediesel.com/wp-content/uploads/2008/05/majeureenmini1-300x224.png" alt="php versions" width="300" height="224" /></a></p>
<p><a href="http://www.nexen.net/images/stories/phpversion/200804/majeure.en.png" target="_blank"><img class="center size-medium wp-image-45" title="php versions 2008" src="http://www.codediesel.com/wp-content/uploads/2008/05/majeureenmini2-300x224.png" alt="php versions 2008" width="300" height="224" /></a></p>
<p>As you can see PHP version 5 has reached a market share of 36% from last years 17%, more then double in one year. As PHP version 6.0.0 is nearing the release date, you can expect PHP 5 to hopefully cross the 50% market share by this year end.</p>
<p>I&#8217;am very keen on knowing if any of you are planning on migrating to version 6 after release or have decided to wait a year or year-an-half prior to adoption. Most of the people I know, still stuck with version 4.x.x, have decided to move to PHP 6 directly rather then go through the 5.x.x route.</p>
<p>The data for this analysis was collected by <a title="nexen.net" href="http://www.nexen.net/" target="_blank">Nexen </a>from 28 million servers and you can find a more detailed analysis on their site.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/which-is-the-most-popular-version-of-php-today/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Tip : PHP Mail()</title>
		<link>http://www.codediesel.com/php/tip-1-php-mail/</link>
		<comments>http://www.codediesel.com/php/tip-1-php-mail/#comments</comments>
		<pubDate>Wed, 07 May 2008 13:19:07 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[php]]></category>

		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=36</guid>
		<description><![CDATA[The PHP mail() function on success returns a 1 or a 0 . This is not the same thing as the email successfully being sent or received. You cannot easily test for either using PHP.
]]></description>
			<content:encoded><![CDATA[<blockquote><p>The PHP mail() function on success returns a 1 or a 0 . This is not the same thing as the email successfully being sent or received. You cannot easily test for either using PHP.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/tip-1-php-mail/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Microsoft Live Mesh. The exciting world ahead!</title>
		<link>http://www.codediesel.com/livemesh/microsoft-live-mesh/</link>
		<comments>http://www.codediesel.com/livemesh/microsoft-live-mesh/#comments</comments>
		<pubDate>Mon, 05 May 2008 10:55:55 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[livemesh]]></category>

		<category><![CDATA[microsoft]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=31</guid>
		<description><![CDATA[The basic principle: To make this multi-device world easier for users and developers.
Microsoft Live Mesh is a “software-plus-services” platform that enables PCs, mobiles and other devices to work with each other through the Internet, enabling individuals and organizations to manage, access, share and sync their files, data  and applications seamlessly on the Web and [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><span style="color: #993300;"><strong>The basic principle</strong></span>: To make this multi-device world easier for users and developers.</p>
<p class="MsoNormal">Microsoft <a title="microsoft live mesh" href="http://www.mesh.com" target="_blank">Live Mesh</a> is a “software-plus-services” platform that enables PCs, mobiles<span> </span>and other devices to work with each other through the Internet, enabling individuals and organizations to manage, access, share and sync their files, data <span> </span>and applications seamlessly on the Web and across their world of devices. Live Mesh is a Software + Services platform rather then an application.<span id="more-31"></span></p>
<p class="MsoNormal"><a href="http://www.codediesel.com/wp-content/uploads/2008/05/livemesh.gif"><img class="aligncenter size-full wp-image-34" title="microsoft live mesh" src="http://www.codediesel.com/wp-content/uploads/2008/05/livemesh.gif" alt="microsoft live mesh" width="388" height="387" /></a></p>
<p class="MsoNormal">
<p class="MsoNormal">
<p class="MsoNormal"><span style="color: #993300;"><strong>Main design goals</strong></span>:<br />
1. To make various devices work together.<br />
2. Your data and applications available from anywhere<br />
3. Make sharing and collaborating with people easier.<br />
4. The information you need to stay up-to-date is always available and in sync with other devices.</p>
<p class="MsoNormal">
<p class="MsoNormal">While many existing web solutions may appear similar in that they utilize hosted services for storage, sharing files, or peer-to-peer connections between PCs, most of those products fulfill only one specific need. What makes Live Mesh different is that it brings all this disparate technologies together seamlessly.</p>
<p class="MsoNormal">
<p class="MsoNormal"><span style="color: #993300;"><strong>So what are the benefits for the end user?</strong></span></p>
<p><strong>Devices Working Together: </strong><span>You can share and synchronize your applications and documents across the web and devices.</span> <strong></strong></p>
<p><strong>Anywhere Access: </strong>The information you put in your mesh is available from any of your other devices, including your own personal Live Desktop that resides on the Web. The Live Desktop comes with a 5GB space when you signup. The Mesh also provides you with a remote desktop functionality in case you need to take full control of a PC remotely.</p>
<p><strong>Simple to Share: </strong>With Live Mesh, you can simply invite a friend or colleague to share a folder, giving them access to view or edit only the files you put in the folder. Live Mesh keeps track of and synchronizes the changes across your mesh.</p>
<p><strong>Stay Informed: </strong>Live Mesh has a feed system that allows you to keep track of all the activities you care about, including the online status of your friends or colleagues, comments they make, changes to files and folders in your mesh, and the status of your devices.</p>
<p class="MsoNormal"><span style="color: #993300;"><strong>Where do developers fit in?</strong></span></p>
<p class="MsoNormal">As said above, Live Mesh is a platform. So developers will be able to build applications using the Live Mesh SDK.  For example you could create a Facebook application that syncs your photos with the mesh. The application thus build will be available on the users mesh, so the user could view it on their PCs, mobile devices, web or any future device that Microsoft adds support for. You can learn more about the Live Mesh architecture <a title="live mesh architecture" href="http://channel9.msdn.com/ShowPost.aspx?PostID=399577" target="_blank">here</a> and mesh application demo <a title="mesh application demo" href="http://channel9.msdn.com/Showpost.aspx?postid=399964" target="_blank">here</a>.<a title="programming the mesh" href="http://channel9.msdn.com/Showpost.aspx?postid=399964" target="_blank"><br />
</a></p>
<p class="MsoNormal"><span style="color: #993300;"><strong>Will it work?</strong></span><br />
In my humble opinion Microsoft is on the right path in making “<a title="cloud computing" href="http://en.wikipedia.org/wiki/Cloud_computing" target="_blank">cloud computing</a>” a reality. Mesh is at present only in a preview mode, so expect more changes in the future as Microsoft gets user feedbacks. As a developer I&#8217;am waiting to get my hands on the SDK.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/livemesh/microsoft-live-mesh/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Item based collaborative filtering in PHP</title>
		<link>http://www.codediesel.com/php/item-based-collaborative-filtering-php/</link>
		<comments>http://www.codediesel.com/php/item-based-collaborative-filtering-php/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 04:05:19 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[data]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[collaborative filtering]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=27</guid>
		<description><![CDATA[Most people are familiar with recommendation systems on websites, wherein after you select an item you are presented with a list of similar items other people purchased . Amazon being the popular one and also one of the first to use it. Below is shown a snapshot from Amazon.
Collaborative filtering algorithms work by searching a [...]]]></description>
			<content:encoded><![CDATA[<p>Most people are familiar with recommendation systems on websites, wherein after you select an item you are presented with a list of similar items other people purchased . Amazon being the popular one and also one of the first to use it. Below is shown a snapshot from Amazon.</p>
<p>Collaborative filtering algorithms work by searching a large group of users or items and finding a smaller llist from it with tastes similar to yours.<span id="more-27"></span></p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2008/04/recommend_amazon.gif"><img class="alignnone size-full wp-image-28" title="recommend_amazon" src="http://www.codediesel.com/wp-content/uploads/2008/04/recommend_amazon.gif" alt="" width="500" height="143" /></a></p>
<p>In this post I&#8217;ll show you how to integrate a simple recommendation system. You can download the class file <a href="http://www.codediesel.com/data/code/sample_recommend.zip" target="_blank">here</a>.</p>
<p>We will work with the following book data as an example.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2008/04/recommend1.gif"><img class="aligncenter size-full wp-image-29" title="recommend1" src="http://www.codediesel.com/wp-content/uploads/2008/04/recommend1.gif" alt="sample data" width="297" height="349" /></a></p>
<p>It shows a list of books purchased by various people and rated on a scale of 5 by them. The list can be any thing - books, jewelry, movies, blog posts; any items where ratings are possible. You have to create this array at runtime form the database.</p>
<p>Our job here will be to use this list to recommend books. The above list is converted into an array here.</p>

<div class="wp_codebox"><table width="100%" ><tr id="2723"><td class="code" id="27code23"><pre class="php"><span style="color: #0000ff;">$books</span> =  <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span>
&nbsp;
    <span style="color: #ff0000;">&quot;phil&quot;</span> =&gt; <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;my girl&quot;</span> =&gt; <span style="color: #cc66cc;">2.5</span>, <span style="color: #ff0000;">&quot;the god delusion&quot;</span> =&gt; <span style="color: #cc66cc;">3.5</span>,
                    <span style="color: #ff0000;">&quot;tweak&quot;</span> =&gt; <span style="color: #cc66cc;">3</span>, <span style="color: #ff0000;">&quot;the shack&quot;</span> =&gt;; <span style="color: #cc66cc;">4</span>,
                    <span style="color: #ff0000;">&quot;the birds in my life&quot;</span> =&gt; <span style="color: #cc66cc;">2.5</span>,
                    <span style="color: #ff0000;">&quot;new moon&quot;</span> =&gt; <span style="color: #cc66cc;">3.5</span><span style="color: #66cc66;">&#41;</span>,
&nbsp;
    <span style="color: #ff0000;">&quot;sameer&quot;</span> =&amp;gt; <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;the last lecture&quot;</span> =&gt; <span style="color: #cc66cc;">2.5</span>,
                      <span style="color: #ff0000;">&quot;the god delusion&quot;</span> =&gt; <span style="color: #cc66cc;">3.5</span>,
                      <span style="color: #ff0000;">&quot;the noble wilds&quot;</span> =&gt; <span style="color: #cc66cc;">3</span>, <span style="color: #ff0000;">&quot;the shack&quot;</span> =&gt; <span style="color: #cc66cc;">3.5</span>,
                      <span style="color: #ff0000;">&quot;the birds in my life&quot;</span> =&gt; <span style="color: #cc66cc;">2.5</span>, <span style="color: #ff0000;">&quot;new moon&quot;</span> =&gt; <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>,
&nbsp;
    <span style="color: #ff0000;">&quot;john&quot;</span> =&gt; <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;a thousand splendid suns&quot;</span> =&gt; <span style="color: #cc66cc;">5</span>, <span style="color: #ff0000;">&quot;the secret&quot;</span> =&gt; <span style="color: #cc66cc;">3.5</span>,
                    <span style="color: #ff0000;">&quot;tweak&quot;</span> =&gt; <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>,
&nbsp;
    <span style="color: #ff0000;">&quot;peter&quot;</span> =&gt; <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;chaos&quot;</span> =&gt; <span style="color: #cc66cc;">5</span>, <span style="color: #ff0000;">&quot;php in action&quot;</span> =&gt; <span style="color: #cc66cc;">3.5</span><span style="color: #66cc66;">&#41;</span>,
&nbsp;
    <span style="color: #ff0000;">&quot;jill&quot;</span> =&gt; <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;the last lecture&quot;</span> =&gt; <span style="color: #cc66cc;">1.5</span>, <span style="color: #ff0000;">&quot;the secret&quot;</span> =&gt; <span style="color: #cc66cc;">2.5</span>,
                    <span style="color: #ff0000;">&quot;the noble wilds&quot;</span> =&gt; <span style="color: #cc66cc;">4</span>, <span style="color: #ff0000;">&quot;the host: a novel&quot;</span> =&gt; <span style="color: #cc66cc;">3.5</span>,
                    <span style="color: #ff0000;">&quot;the world without end&quot;</span> =&gt; <span style="color: #cc66cc;">2.5</span>, <span style="color: #ff0000;">&quot;new moon&quot;</span> =&gt; <span style="color: #cc66cc;">3.5</span><span style="color: #66cc66;">&#41;</span>,
&nbsp;
    <span style="color: #ff0000;">&quot;bruce&quot;</span> =&gt; <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;the last lecture&quot;</span> =&gt; <span style="color: #cc66cc;">3</span>, <span style="color: #ff0000;">&quot;the hollow&quot;</span> =&gt; <span style="color: #cc66cc;">1.5</span>,
                     <span style="color: #ff0000;">&quot;the noble wilds&quot;</span> =&gt; <span style="color: #cc66cc;">3</span>, <span style="color: #ff0000;">&quot;the shack&quot;</span> =&gt; <span style="color: #cc66cc;">3.5</span>,
                     <span style="color: #ff0000;">&quot;the appeal&quot;</span> =&gt; <span style="color: #cc66cc;">2</span>, <span style="color: #ff0000;">&quot;new moon&quot;</span> =&gt; <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>,
&nbsp;
    <span style="color: #ff0000;">&quot;tom&quot;</span> =&gt; <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;chaos&quot;</span> =&gt; <span style="color: #cc66cc;">2.5</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Lets start by including the class file first.</p>

<div class="wp_codebox"><table width="100%" ><tr id="2724"><td class="code" id="27code24"><pre class="php"><span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;recommend.php&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Lets say you are &#8216;John&#8217; and the books you have purchased till now are &#8216;a thousand splendid suns&#8217;, &#8216;the secret&#8217; and &#8216;tweak&#8217; with appropriate rating given. If you come to the website again what books would the site recommend. Lets try!</p>

<div class="wp_codebox"><table width="100%" ><tr id="2725"><td class="code" id="27code25"><pre class="php"><span style="color: #0000ff;">$re</span> = <span style="color: #000000; font-weight: bold;">new</span> Recommend<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">print_r</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$re</span>-&gt;<span style="color: #006600;">getRecommendations</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$books</span>, <span style="color: #ff0000;">&quot;john&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>It will output the following with appropriate ratings and sorted in descending order.</p>

<div class="wp_codebox"><table width="100%" ><tr id="2726"><td class="code" id="27code26"><pre class="php"><span style="color: #000066;">Array</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #66cc66;">&#91;</span>the noble wilds<span style="color: #66cc66;">&#93;</span> =&gt;; <span style="color: #cc66cc;">4</span>
    <span style="color: #66cc66;">&#91;</span>the shack<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">4</span>
    <span style="color: #66cc66;">&#91;</span>the host: a novel<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3.5</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> moon<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3.5</span>
    <span style="color: #66cc66;">&#91;</span>the god delusion<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3.5</span>
    <span style="color: #66cc66;">&#91;</span>the world without <span style="color: #000066;">end</span><span style="color: #66cc66;">&#93;</span> =&gt;; <span style="color: #cc66cc;">2.5</span>
    <span style="color: #66cc66;">&#91;</span>the birds in my life<span style="color: #66cc66;">&#93;</span> =&gt;; <span style="color: #cc66cc;">2.5</span>
    <span style="color: #66cc66;">&#91;</span>my girl<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">2.5</span>
    <span style="color: #66cc66;">&#91;</span>the last lecture<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">1.5</span>
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Now lets try with &#8216;tom&#8217;.</p>

<div class="wp_codebox"><table width="100%" ><tr id="2727"><td class="code" id="27code27"><pre class="php"><span style="color: #0000ff;">$re</span> = <span style="color: #000000; font-weight: bold;">new</span> Recommend<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">print_r</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$re</span>-&gt;<span style="color: #006600;">getRecommendations</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$books</span>, <span style="color: #ff0000;">&quot;tom&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>The recommended books will be&#8230;</p>

<div class="wp_codebox"><table width="100%" ><tr id="2728"><td class="code" id="27code28"><pre class="php"><span style="color: #000066;">Array</span>
<span style="color: #66cc66;">&#40;</span>
<span style="color: #66cc66;">&#91;</span>php in action<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3.5</span>
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Now how about recommending items based on book name rather then on the person. For that what we have to do is invert the array using the &#8216;transformPreferences&#8217; function and then use the &#8216;matchItems&#8217; function.</p>
<p>Inverting the array like this</p>

<div class="wp_codebox"><table width="100%" ><tr id="2729"><td class="code" id="27code29"><pre class="php"><span style="color: #0000ff;">$result</span> = <span style="color: #0000ff;">$re</span>-&gt;<span style="color: #006600;">transformPreferences</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$books</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>will return an array as show below.</p>

<div class="wp_codebox"><table width="100%" ><tr id="2730"><td class="code" id="27code30"><pre class="php">rray
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #66cc66;">&#91;</span>my girl<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #000066;">Array</span>
        <span style="color: #66cc66;">&#40;</span>
            <span style="color: #66cc66;">&#91;</span>phil<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">2.5</span>
        <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">&#91;</span>the god delusion<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #000066;">Array</span>
        <span style="color: #66cc66;">&#40;</span>
            <span style="color: #66cc66;">&#91;</span>phil<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3.5</span>
            <span style="color: #66cc66;">&#91;</span>sameer<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3.5</span>
        <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">&#91;</span>tweak<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #000066;">Array</span>
        <span style="color: #66cc66;">&#40;</span>
            <span style="color: #66cc66;">&#91;</span>phil<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3</span>
            <span style="color: #66cc66;">&#91;</span>john<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">1</span>
        <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">&#91;</span>the shack<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #000066;">Array</span>
        <span style="color: #66cc66;">&#40;</span>
            <span style="color: #66cc66;">&#91;</span>phil<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">4</span>
            <span style="color: #66cc66;">&#91;</span>sameer<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3.5</span>
            <span style="color: #66cc66;">&#91;</span>bruce<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3.5</span>
        <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">&#91;</span>the birds in my life<span style="color: #66cc66;">&#93;</span> =&amp;gt; <span style="color: #000066;">Array</span>
        <span style="color: #66cc66;">&#40;</span>
            <span style="color: #66cc66;">&#91;</span>phil<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">2.5</span>
            <span style="color: #66cc66;">&#91;</span>sameer<span style="color: #66cc66;">&#93;</span> =&gt;; <span style="color: #cc66cc;">2.5</span>
        <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> moon<span style="color: #66cc66;">&#93;</span> =&amp;gt; <span style="color: #000066;">Array</span>
        <span style="color: #66cc66;">&#40;</span>
            <span style="color: #66cc66;">&#91;</span>phil<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3.5</span>
            <span style="color: #66cc66;">&#91;</span>sameer<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">1</span>
            <span style="color: #66cc66;">&#91;</span>jill<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3.5</span>
            <span style="color: #66cc66;">&#91;</span>bruce<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3</span>
        <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">&#91;</span>the last lecture<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #000066;">Array</span>
        <span style="color: #66cc66;">&#40;</span>
            <span style="color: #66cc66;">&#91;</span>sameer<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">2.5</span>
            <span style="color: #66cc66;">&#91;</span>jill<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">1.5</span>
            <span style="color: #66cc66;">&#91;</span>bruce<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">3</span>
        <span style="color: #66cc66;">&#41;</span>
&nbsp;
.
.
.
.</pre></td></tr></table></div>

<p>Lets try with the book &#8216;chaos&#8217; and see what the system will recommend us.</p>

<div class="wp_codebox"><table width="100%" ><tr id="2731"><td class="code" id="27code31"><pre class="php"><span style="color: #0000ff;">$re</span> = <span style="color: #000000; font-weight: bold;">new</span> Recommend<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$result</span> = <span style="color: #0000ff;">$re</span>-&gt;<span style="color: #006600;">transformPreferences</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$books</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">print_r</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$re</span>-&gt;<span style="color: #006600;">matchItems</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span>, <span style="color: #ff0000;">&quot;chaos&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>The out will be.</p>

<div class="wp_codebox"><table width="100%" ><tr id="2732"><td class="code" id="27code32"><pre class="php"><span style="color: #000066;">Array</span>
<span style="color: #66cc66;">&#40;</span>
<span style="color: #66cc66;">&#91;</span>php in action<span style="color: #66cc66;">&#93;</span> =&gt; <span style="color: #cc66cc;">0.4</span>
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>But now the value on the right is not a rating but a match probability from a scale of 0 - 1, 1 being a perfect match. If you change the rating of the &#8216;php in action book&#8217; in the above array to 5 and then run the above code again you will get the match probability as &#8216;1&#8242;.</p>
<p>You can also find how similar the choice of two persons is on a scale of 0-1.</p>

<div class="wp_codebox"><table width="100%" ><tr id="2733"><td class="code" id="27code33"><pre class="php"><span style="color: #0000ff;">$similarity</span> =  <span style="color: #0000ff;">$re</span>-&gt;<span style="color: #006600;">similarityDistance</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$books</span>, <span style="color: #ff0000;">&quot;tom&quot;</span>, <span style="color: #ff0000;">&quot;peter&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//Converted to percent.</span>
<span style="color: #000066;">echo</span> <span style="color: #000066;">sprintf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%.2f&quot;</span>, <span style="color: #0000ff;">$similarity</span> * <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">&quot;%&quot;</span>;</pre></td></tr></table></div>

<p>Will return</p>

<div class="wp_codebox"><table width="100%" ><tr id="2734"><td class="code" id="27code34"><pre class="php"><span style="color: #cc66cc;">28.57</span>%</pre></td></tr></table></div>

<p>This simplified filtering system can be used on a site with a few thousand items and members. But as the number of items grows it will be time consuming to calculate the recommendations every time someone purchases or browses an item. In that case it will be helpful to precalculate the array and store it in a database maybe once a day. But that is a different story altogether. For more information on the algorithms and variations you can refer to the wonderful <a title="Programming collective intelligence" href="http://www.amazon.com/Programming-Collective-Intelligence-Building-Applications/dp/0596529325/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1209100464&amp;sr=1-1" target="_blank">Programming Collective Intelligence: Building Smart Web 2.0 Applications</a>. The code examples in the book are in Python, though.</p>
<p>The above code may not work in each and every instance but you can tweak the code according to your requirements.</p>
<p><a class="download" href="http://www.codediesel.com/data/code/sample_recommend.zip">Download code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/item-based-collaborative-filtering-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>6 books to master PHP</title>
		<link>http://www.codediesel.com/php/6-books-to-master-php/</link>
		<comments>http://www.codediesel.com/php/6-books-to-master-php/#comments</comments>
		<pubDate>Mon, 21 Apr 2008 09:44:11 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[mysql]]></category>

		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=18</guid>
		<description><![CDATA[PHP is a wonderful dynamic language, and the addition of Unicode support, namespaces in the upcoming version 6 will make it even better.
If you are new to PHP or are thinking of moving to PHP form other language then the following list of books will provide you with the required knowledge to become a seasoned [...]]]></description>
			<content:encoded><![CDATA[<p>PHP is a wonderful dynamic language, and the addition of Unicode support, namespaces in the upcoming version 6 will make it even better.<br />
If you are new to PHP or are thinking of moving to PHP form other language then the following list of books will provide you with the required knowledge to become a seasoned PHP programmer. Of course, the list is subjective and you may have a different list of books in mind.<span id="more-18"></span></p>
<hr size="1" /><a href="http://www.codediesel.com/wp-content/uploads/2008/04/2.gif"><img title="Zend PHP 5 Certification" src="http://www.codediesel.com/wp-content/uploads/2008/04/2.gif" alt="Zend PHP 5 Certification" width="77" height="94" /></a></p>
<p><strong>1.<a href="http://www.amazon.com/architects-Zend-Certification-Study-Guide/dp/0973862149/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1208775291&amp;sr=8-1" target="_blank"> Zend PHP 5 Certification</a></strong> : If you are new to PHP, then you cannot get wrong with this book. This book will get you up and running in no time.; right from the basics to OOP, security, XML processing and more. A warning - the book doesn&#8217;t go into much detail, so you will have to refer to other books given below for a deeper understanding of the concepts.</p>
<hr size="1" /><a href="http://www.codediesel.com/wp-content/uploads/2008/04/1.gif"><img title="phpinaction" src="http://www.codediesel.com/wp-content/uploads/2008/04/1.gif" alt="php in action" width="75" height="94" /></a></p>
<p><strong>2. <a href="http://www.amazon.com/PHP-Action-Objects-Design-Agility/dp/1932394753/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1208775343&amp;sr=1-1" target="_blank">PHP in Action</a></strong> : If you a beginner or intermediate PHP programmer, then this book will take you to the next level. Design Patterns, unit testing, advanced OOP.</p>
<hr size="1" /><a href="http://www.codediesel.com/wp-content/uploads/2008/04/51.gif"><img class="alignnone size-thumbnail wp-image-22" title="PHP and MySQL Web Development" src="http://www.codediesel.com/wp-content/uploads/2008/04/51.gif" alt="PHP and MySQL Web Development" width="77" height="99" /></a></p>
<p><strong>3. <a href="http://www.amazon.com/PHP-MySQL-Development-Developers-Library/dp/0672326728/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1208775375&amp;sr=1-1" target="_blank">PHP and MySQL Web Development</a></strong> : More pragmatic then the other books posted here. A little old compared to others. No information about MVC or design patterns, but this book you show you how to build real world applications like shopping carts, forums, mailing list managers, content management systems etc. It also has an excellent coverage of MySQL.</p>
<hr size="1" /><a href="http://www.codediesel.com/wp-content/uploads/2008/04/3.gif"><img class="alignnone size-thumbnail wp-image-23" title="PEAR" src="http://www.codediesel.com/wp-content/uploads/2008/04/3.gif" alt="PEAR" width="74" height="94" /></a></p>
<p><strong>4. <a href="http://www.amazon.com/PHP-Programming-PEAR-Schmidt-Stephan/dp/1904811795/ref=pd_bbs_2?ie=UTF8&amp;s=books&amp;qid=1208775417&amp;sr=1-2" target="_blank">PEAR</a></strong><a href="http://www.codediesel.com/wp-admin/PHP Programming with PEAR" target="_blank"> </a>: PEAR is the PHP Extension and Application Repository, and is a framework and distribution system for reusable, high-quality PHP components. If you have been programming PHP for a while then you will surely have come across PEAR. PEAR provides classes for most of your daily programming needs. Whether you want to work with XML, create Excel documents, encryption, text processing; PEAR provides a class for the purpose and for many more then you can shake a stick at.</p>
<hr size="1" /><a href="http://www.codediesel.com/wp-content/uploads/2008/04/6.gif"><img class="alignnone size-thumbnail wp-image-24" title="PHP Cookbook" src="http://www.codediesel.com/wp-content/uploads/2008/04/6.gif" alt="PHP Cookbook" width="78" height="102" /></a></p>
<p><strong>5. <a href="http://www.amazon.com/Cookbook-Cookbooks-OReilly-Adam-Trachtenberg/dp/0596101015/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1208775472&amp;sr=1-1" target="_blank">PHP Cookbook</a> </strong>: The book to get you unstuck. Mostly for intermediate programmers; just the book to refer when you want a quick solution to a particular coding problem.</p>
<hr size="1" /><a href="http://www.codediesel.com/wp-content/uploads/2008/04/7.gif"><img class="alignnone size-thumbnail wp-image-25" title="SQL for MySQL Developers" src="http://www.codediesel.com/wp-content/uploads/2008/04/7.gif" alt="SQL for MySQL Developers" width="70" height="95" /></a></p>
<p><strong>6. <a href="http://www.amazon.com/SQL-MySQL-Developers-Comprehensive-Reference/dp/0131497359/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1208775500&amp;sr=1-1" target="_blank">SQL for MySQL Developers</a></strong> : Most PHP programmers I have encountered have a very limited understanding of SQL. How many intermediate programmers are aware of the &#8216;HAVING&#8217; clause in the SELECT statement or have used sub queries more then 2 level deep. The book provides excellent coverage of SQL as implemented in MySQL. The SELECT statement itself is given 9 chapters of coverage.<br />
The book covers everything from MySQL installation, configuration, security to Stored Procedures in a friendly manner with many exercises at each chapter end.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/6-books-to-master-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Google Pagerank in PHP</title>
		<link>http://www.codediesel.com/php/google-pagerank-in-php/</link>
		<comments>http://www.codediesel.com/php/google-pagerank-in-php/#comments</comments>
		<pubDate>Sat, 19 Apr 2008 08:59:08 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=16</guid>
		<description><![CDATA[Mark Woodman has created a nifty class which lets you query Google pagerank info in PHP 5 and above. The complete classes are available here.
The minimum classes required are &#8216;cacher.class.php&#8216; and &#8216;google_pagerank.class.php&#8216;.
Sample code is shown below.

require_once&#40;&#34;popstats/google_pagerank.class.php&#34;&#41;;
&#160;
$rankObject = new GooglePageRank&#40;&#34;http://www.someDomain.com&#34;&#41;;
&#160;
$pageRank = $rankObject-&#62;pagerank;
&#160;
echo $pageRank;

The class also supports result caching on the local machine, so if a second [...]]]></description>
			<content:encoded><![CDATA[<p>Mark Woodman has created a nifty class which lets you query Google pagerank info in PHP 5 and above. The complete classes are available <a title="popStats" href="http://code.google.com/p/popstats/" target="_blank">here</a>.</p>
<p>The minimum classes required are &#8216;<a rel="nofollow" href="http://popstats.googlecode.com/svn/trunk/cacher.class.php">cacher.class.php</a>&#8216; and &#8216;<a rel="nofollow" href="http://popstats.googlecode.com/svn/trunk/google_pagerank.class.php">google_pagerank.class.php</a>&#8216;.</p>
<p>Sample code is shown below.<span id="more-16"></span></p>

<div class="wp_codebox"><table width="100%" ><tr id="1637"><td class="code" id="16code37"><pre class="php"><span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;popstats/google_pagerank.class.php&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$rankObject</span> = <span style="color: #000000; font-weight: bold;">new</span> GooglePageRank<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://www.someDomain.com&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$pageRank</span> = <span style="color: #0000ff;">$rankObject</span>-&gt;<span style="color: #006600;">pagerank</span>;
&nbsp;
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$pageRank</span>;</pre></td></tr></table></div>

<p>The class also supports result caching on the local machine, so if a second query arrives within the cache time limit the result saved in the local file is returned rather then going out and querying Google.</p>
<p>The &#8216;GooglePageRank&#8217; class accepts cache time as a second parameter. So if you want to cache the result for 6 hours you set the second parameter for 21600 seconds (6 * 60 * 60):</p>

<div class="wp_codebox"><table width="100%" ><tr id="1638"><td class="code" id="16code38"><pre class="php"><span style="color: #0000ff;">$rankObject</span> = <span style="color: #000000; font-weight: bold;">new</span> GooglePageRank<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://www.someDomain.com&quot;</span>, <span style="color: #cc66cc;">21600</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>The default is set to 24 hours, which is more then adequate for most purposes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/google-pagerank-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Time series visualization with Timeplot</title>
		<link>http://www.codediesel.com/data/timeplot/</link>
		<comments>http://www.codediesel.com/data/timeplot/#comments</comments>
		<pubDate>Fri, 18 Apr 2008 08:31:52 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[data]]></category>

		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=15</guid>
		<description><![CDATA[Using lines, points and other geometrical objects to visualize data has always fascinated me; specially time series. Time series charts let you visualize data points separated by time intervals.
The following image shows an example of a time series data representing the salary of software engineers for a particular duration.

Timeplot is a javascript widget which lets [...]]]></description>
			<content:encoded><![CDATA[<p>Using lines, points and other geometrical objects to visualize data has always fascinated me; specially time series. Time series charts let you visualize data points separated by time intervals.</p>
<p>The following image shows an example of a time series data representing the salary of software engineers for a particular duration.<span id="more-15"></span><br />
<img src="http://www.codediesel.com/data/images/timeplot.gif" alt="TimePlot" /></p>
<p><a title="Timeplot" href="http://simile.mit.edu/timeplot/examples/immigration/index.html" target="_blank">Timeplot</a> is a javascript widget which lets you visualize time series data in a colorful manner with many options to tweak the final output. In this post we will build a time series plot for visualizing yearly US immigrant population. You can download the raw data <a title="population data" href="http://www.codediesel.com/data/pages/timeplot/data.txt" target="_blank">here</a>.<br />
Lets start with creating a time series plot.</p>
<p>Shown below is a sample excerpt from the data file.<br />
<img src="http://www.codediesel.com/data/images/timeplot0.gif" alt="timeplot data" /><br />
<strong>STEP 1. Creating the basic container.</strong><br />
The basic setup is simple. A javascript library and a container to hold the plot as shown below.</p>
<p><img src="http://www.codediesel.com/data/images/timeplot1.gif" alt="timeplot1" /></p>
<p><strong>STEP 2. The main display block.</strong><br />
The main display code is executed in the onLoad event. There are many options to tweak the display but the one shown below will get you started. You can save the below code in a separate file or embeded in a script tag.<br />
<img src="http://www.codediesel.com/data/images/timeplot2.gif" alt="timeplot2" /></p>
<p>The final output is show below.<br />
&lt;br /&gt; Your browser does not support inline frames or is currently configured not to display inline frames.&lt;br /&gt;</p>
<p><a class="download" href="http://www.codediesel.com/data/code/timeplot.zip">Download code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/data/timeplot/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
