<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>code-diesel</title>
	
	<link>http://www.codediesel.com</link>
	<description>/* PHP &amp; MySQL Journal */</description>
	<pubDate>Sat, 13 Mar 2010 10:27:09 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Code-diesel" /><feedburner:info uri="code-diesel" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by/2.0/</creativeCommons:license><feedburner:emailServiceId>Code-diesel</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Google Translation PHP wrapper</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/_T6qfGqY8F0/</link>
		<comments>http://www.codediesel.com/php/google-translation-php-wrapper/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 04:27:20 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[google]]></category>

		<category><![CDATA[libraries]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[translate]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/php/google-translation-php-wrapper/</guid>
		<description><![CDATA[Google translation is an interesting service. Not only can you do language translation, you can also detect the language of a particular text. I recently needed to create a Wordpress plugin to translate post titles from one language to another. As the translation API is only available for Java and Javascript, I decided to create [...]]]></description>
			<content:encoded><![CDATA[<p>Google translation is an interesting service. Not only can you do language translation, you can also detect the language of a particular text. I recently needed to create a Wordpress plugin to translate post titles from one language to another. As the translation API is only available for Java and Javascript, I decided to create a quick one for PHP. In this post we will see how to translate text from one language to another in a simple and quick way using the created class.<br />
<span id="more-2497"></span></p>
<h4>Downloading the class</h4>
<p>Before we begin you need to download the PHP class from below the link below or from the <a href="http://code.google.com/p/google-translate-php-wrapper/">Google code</a> site.</p>
<div  class="download2">
<a href="http://www.codediesel.com/downloads/GoogleTranslate">Download GoogleTranslateWrapper</a><br />
<span>Downloads : 271  / File size : 72.2 kB</span>
</div>
<h4>Testing the class</h4>
<p>The Translation class used cURL to access the Google translation service, so you need to have that installed. A quick way to check if the class is working correctly is to use the &#8217;selfTest&#8217; method of the class.</p>

<div class="wp_codebox"><table width="100%" ><tr id="24976"><td class="code" id="2497code6"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'googleTranslate.class.php'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$gt</span> = <span style="color: #000000; font-weight: bold;">new</span> GoogleTranslateWrapper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$gt</span>-&gt;<span style="color: #006600;">selfTest</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>Which will print &#8216;Test Ok&#8217; if everything is working fine.</p>
<h4>Translating text</h4>
<p>Once you are sure that the class is working properly, you can use the following simple example to translate a piece of text from one language to another.</p>

<div class="wp_codebox"><table width="100%" ><tr id="24977"><td class="code" id="2497code7"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'googleTranslate.class.php'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* Initialize the class translate class */</span>
<span style="color: #0000ff;">$gt</span> = <span style="color: #000000; font-weight: bold;">new</span> GoogleTranslateWrapper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$sampleText</span> = <span style="color: #ff0000;">&quot;Bonjour de cette partie du monde&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* translate(string, to_language, from_language) */</span>
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$gt</span>-&gt;<span style="color: #006600;">translate</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sampleText</span> , <span style="color: #ff0000;">&quot;en&quot;</span>, <span style="color: #ff0000;">&quot;fr&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* This should print 'Hello from this part of the world' */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The first parameter of the &#8216;translate()&#8217; method takes the text to translate, the second and third parameters take the target language to translate to and the language to translate from, respectively. You can eliminate the third parameter, the language name to translate from, and Google will automatically detect the source language and translate the text. This can be useful if you are not sure of the source language but want to translate and also detect the same. A short example is given below. The following will convert the sample text and also detect the source language (French here).</p>

<div class="wp_codebox"><table width="100%" ><tr id="24978"><td class="code" id="2497code8"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'googleTranslate.class.php'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$gt</span> = <span style="color: #000000; font-weight: bold;">new</span> GoogleTranslateWrapper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$test</span> = <span style="color: #ff0000;">&quot;Bonjour de cette partie du monde&quot;</span>;
<span style="color: #808080; font-style: italic;">/* Convert from English */</span>
<span style="color: #0000ff;">$gt</span>-&gt;<span style="color: #006600;">translate</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$test</span>, <span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$gt</span>-&gt;<span style="color: #006600;">getDetectedSource</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>

<h4>Errors during translation</h4>
<p>If the class fails to translate the text for some reason, you can read the error message using the following:</p>

<div class="wp_codebox"><table width="100%" ><tr id="24979"><td class="code" id="2497code9"><pre class="php">.
.
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$gt</span>-&gt;<span style="color: #006600;">getLastError</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
.</pre></td></tr></table></div>

<h4>Passing valid credentials</h4>
<p>Although the class works without you providing a apiKey - <strong>using a key in your application/site is completely optional</strong>, Google recommends adding your API key and a IP address during the translation request. This helps Google detect valid requests coming from your site and helps it in contacting you in case a need arises. You can signup for the api key <a target="_blank" href="http://code.google.com/apis/ajaxsearch/signup.html">here</a>.</p>
<p>You can pass a API key and a IP address using the following.</p>

<div class="wp_codebox"><table width="100%" ><tr id="249710"><td class="code" id="2497code10"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'googleTranslate.class.php'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$apiKey</span> = <span style="color: #ff0000;">&quot;YOUR API KEY&quot;</span>;
<span style="color: #808080; font-style: italic;">/* Change to your IP address */</span>
<span style="color: #0000ff;">$ip</span> = <span style="color: #ff0000;">&quot;127.0.0.1&quot;</span>;
&nbsp;
<span style="color: #0000ff;">$gt</span> = <span style="color: #000000; font-weight: bold;">new</span> GoogleTranslateWrapper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$gt</span>-&gt;<span style="color: #006600;">setCredentials</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$apiKey</span>, <span style="color: #0000ff;">$ip</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$test</span> = <span style="color: #ff0000;">&quot;mon nom est Sameer&quot;</span>;
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$gt</span>-&gt;<span style="color: #006600;">translate</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$test</span>, <span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This concludes the short tutorial on using the &#8216;GoogleTranslateWrapper&#8217; class.</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/_T6qfGqY8F0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/google-translation-php-wrapper/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/php/google-translation-php-wrapper/</feedburner:origLink></item>
		<item>
		<title>Code Tools for Programmers</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/xMN-kk55p4k/</link>
		<comments>http://www.codediesel.com/general/tools/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 09:03:56 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/sql/tools/</guid>
		<description><![CDATA[I&#8217;ve recently designed a repository of tools and libraries, www.mycodetools.com, the primary goal of which will be to create a space wherein web developers will be able to find important new libraries and tools to help them in their daily development work.
Readers feedback will be valuable regarding color scheme, content, formatting or any additional features, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently designed a repository of tools and libraries, <a target="_blank" href="http://www.mycodetools.com">www.mycodetools.com</a>, the primary goal of which will be to create a space wherein web developers will be able to find important new libraries and tools to help them in their daily development work.</p>
<p>Readers feedback will be valuable regarding color scheme, content, formatting or any additional features, which will help me make the site more user friendly.</p>
<p><a href="http://www.mycodetools.com/"><img src="http://www.codediesel.com/wp-content/uploads/2010/02/codetools.gif" alt="mycodetools.com" title="mycodetools.com" width="398" height="628" class="alignleft size-full wp-image-2487" /></a></p>
<p><a href="http://www.mycodetools.com/">http://www.mycodetools.com/</a></p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/xMN-kk55p4k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/general/tools/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/general/tools/</feedburner:origLink></item>
		<item>
		<title>Benchmarking Wordpress SQL using FirePHP</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/G3kiMXQf3hw/</link>
		<comments>http://www.codediesel.com/mysql/benchmarking-wordpress-sql-using-firephp/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 09:10:16 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[mysql]]></category>

		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/php/benchmarking-wordpress-sql-using-firephp/</guid>
		<description><![CDATA[Benchmarking your Wordpress SQL]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.codediesel.com/wp-content/uploads/2010/02/firephp_thumb.gif" alt="firephp_thumb" title="firephp_thumb" width="331" height="185" class="alignleft size-full wp-image-2478" />Building and running a Wordpress site is a simple matter. Wordpress is a considerably fast CMS system, until you start to add more and more plugins and one day you notice that Worpdress has started to slow down. It may be the case that SQL queries within some plugins are not optimized and are taking an increased amount of time executing them, this can considerably slow down your site. The first thing you can do to rectify the situation is to find out where exactly the bottleneck resides by analyzing the time each SQL query takes to executes. Some inquisitive people among you may also be interested in knowing in what sequence the Wordpress SQL queries themselves are being run. Not that all bottlenecks occur due to unoptimized SQL, most are due to poor coding practices. Whatever the reason; the following post will show you how to look inside the SQL query execution of Wordpress.<br />
<span id="more-2443"></span></p>
<h4>Logging Wordpress SQL queries</h4>
<p>By default Wordpress doesn&#8217;t log any queries. But you can instruct Wordpress to do so  by adding the following configuration option to your Wordpress &#8216;wpconfig.php&#8217; file.</p>

<div class="wp_codebox"><table width="100%" ><tr id="244311"><td class="code" id="2443code11"><pre class="php"><span style="color: #808080; font-style: italic;">/* wp-config.php */</span>
&nbsp;
<span style="color: #000066;">define</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'SAVEQUERIES'</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Once that is done you can add the following lines at the end of your Wordpress themes &#8216;footer.php&#8217; file.</p>

<div class="wp_codebox"><table width="100%" ><tr id="244312"><td class="code" id="2443code12"><pre class="php"><span style="color: #808080; font-style: italic;">/* footer.php */</span>
..
<span style="color: #000066;">global</span> <span style="color: #0000ff;">$wpdb</span>;
<span style="color: #000066;">print_r</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$wpdb</span>-&gt;<span style="color: #006600;">queries</span><span style="color: #66cc66;">&#41;</span>;
.
.</pre></td></tr></table></div>

<p>This will display the SQL statements executed by your Wordpress installation in a raw format as shown below. It displays a total of three items - the executed query, the execution time of the query and the function that called the query.</p>

<div class="wp_codebox"><table width="100%" ><tr id="244313"><td class="code" id="2443code13"><pre class="text">Array
(
    [0] =&gt; Array
        (
            [0] =&gt; SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes'
            [1] =&gt; 0.004619836807251
            [2] =&gt; require, require_once, require_once, require_once, is_blog_installed, wp_load_alloptions
        )
&nbsp;
    [1] =&gt; Array
        (
            [0] =&gt; SELECT option_value FROM wp_options WHERE option_name = 'sidebars_widgets' LIMIT 1
            [1] =&gt; 0.0015749931335449
            [2] =&gt; require, require_once, require_once, require_once, do_action, call_user_func_array, wp_widgets_init, register_widget, WP_Widget_Factory-&gt;register, WP_Widget_Recent_Comments-&gt;WP_Widget_Recent_Comments, is_active_widget, wp_get_sidebars_widgets, get_option
        )
.
.</pre></td></tr></table></div>

<p>As you can see the raw information is not interesting to look at and it also clutters the actual site. A better way is to use FireBug and FirePHP to log the SQL queries without interfering with the Wordpress output.</p>
<h4>Installing FirePHP</h4>
<p>Before starting you need to have <a target="_blank" href="https://addons.mozilla.org/en-US/firefox/addon/6149/">FirePHP</a> and <a target="_blank" href="https://addons.mozilla.org/en-US/firefox/addon/1843">FireBug</a> installed. FirePHP uses FireBugs console window to output the debug messages.  Once you have installed FirePHP and FireBug, you need to install the FirePHP <a target="_blank" href="http://www.firephp.org/HQ/Install.htm">server libraries</a>. Copy the FirePHP server libraries to your Wordpress themes directory. For e.g &#8216;wordpress\wp-content\themes\default\FirePHPCore&#8217;.</p>
<p>If you are unfamiliar with FirePHP a nice tutorial can be found at <a target="_blank" href="http://sixrevisions.com/web-development/how-to-debug-php-using-firefox-with-firephp/">Six Revisions</a>.</p>
<h4>Displaying formatted information with FirePHP</h4>
<p>Once we are done with the above setup, you can add the following code at the end of your Wordpress themes &#8216;footer.php&#8217; file, replacing the code we previously added.</p>

<div class="wp_codebox"><table width="100%" ><tr id="244314"><td class="code" id="2443code14"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* footer.php */</span>
&nbsp;
    <span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;FirePHPCore/FirePHP.class.PHP&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #000066;">global</span> <span style="color: #0000ff;">$wpdb</span>;
&nbsp;
    <span style="color: #0000ff;">$query_table</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SQL&quot;</span>, <span style="color: #ff0000;">&quot;Execution Time&quot;</span>, <span style="color: #ff0000;">&quot;Calling Function&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$expensive_query_time</span> = <span style="color: #cc66cc;">0</span>;
    <span style="color: #0000ff;">$expensive_query</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
    <span style="color: #0000ff;">$total_time</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
    <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$wpdb</span>-&gt;<span style="color: #006600;">queries</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$query</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$query_table</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$query</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #0000ff;">$query</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #0000ff;">$query</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #808080; font-style: italic;">/* Get the most expensive query */</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$query</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> &gt; <span style="color: #0000ff;">$expensive_query_time</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$expensive_query_time</span> = <span style="color: #0000ff;">$query</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>;
            <span style="color: #0000ff;">$expensive_query</span> = <span style="color: #0000ff;">$query</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
            <span style="color: #0000ff;">$total_time</span> += <span style="color: #0000ff;">$query</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: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$total_queries</span> = <span style="color: #000066;">count</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$wpdb</span>-&gt;<span style="color: #006600;">queries</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$firephp</span> = FirePHP::<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Display the queries in a formatted table */</span>
    <span style="color: #0000ff;">$firephp</span>-&gt;<span style="color: #006600;">group</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Query'</span>, <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'Collapsed'</span> =&gt; <span style="color: #000000; font-weight: bold;">false</span>,  
                                    <span style="color: #ff0000;">'Color'</span> =&gt; <span style="color: #ff0000;">'#000'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$firephp</span>-&gt;<span style="color: #006600;">table</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$total_queries</span> . <span style="color: #ff0000;">&quot; queries took &quot;</span> . 
                    <span style="color: #0000ff;">$total_time</span> . <span style="color: #ff0000;">&quot; seconds.&quot;</span>, <span style="color: #0000ff;">$query_table</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Display the query summary */</span>
    <span style="color: #0000ff;">$firephp</span>-&gt;<span style="color: #006600;">group</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Query Summary'</span>, <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'Collapsed'</span> =&gt; <span style="color: #000000; font-weight: bold;">false</span>,  
                                            <span style="color: #ff0000;">'Color'</span> =&gt; <span style="color: #ff0000;">'#000'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$firephp</span>-&gt;<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$total_queries</span>, <span style="color: #ff0000;">'Total Queries'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$firephp</span>-&gt;<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$expensive_query</span>, <span style="color: #ff0000;">'Expensive Query'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$firephp</span>-&gt;<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$expensive_query_time</span>, <span style="color: #ff0000;">'Expensive Query Time'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Refresh your Wordpress page and you should see the following output in the FireBug console. Click on the image below to view the complete debug screen. (Some queries have been removed for size.) As you can see FirePHP displays a nicely formatted output than the previous one.</p>
<p><a href="http://www.codediesel.com/data/images/firephp_large.gif"><img src="http://www.codediesel.com/wp-content/uploads/2010/02/firephp.gif" alt="firephp" title="firephp" width="563" height="247" class="aligncenter size-full wp-image-2455" /></a></p>
<h4>Modifying the query data returned by Wordpress</h4>
<p>Wordpress by default saves and returns the following three parameters for each SQL query:<br />
1. SQL query<br />
2. Query Execution time<br />
3. The method chain that called the query.</p>
<p>The code section where this happens is located in the query() method of the &#8216;wp-db.php&#8217; file in the Wordpress &#8216;wp-includes&#8217; directory. The following snippet shows that section of code:</p>

<div class="wp_codebox"><table width="100%" ><tr id="244315"><td class="code" id="2443code15"><pre class="php"><span style="color: #808080; font-style: italic;">/* wp-db.php */</span>
.
.
<span style="color: #808080; font-style: italic;">// Log how the function was called</span>
<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">func_call</span> = <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\$</span>db-&gt;query(<span style="color: #000099; font-weight: bold;">\&quot;</span>$query<span style="color: #000099; font-weight: bold;">\&quot;</span>)&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Keep track of the last query for debug..</span>
<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">last_query</span> = <span style="color: #0000ff;">$query</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Perform the query via std mysql_query function..</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000066;">defined</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'SAVEQUERIES'</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; SAVEQUERIES <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">timer_start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">result</span> = @<span style="color: #000066;">mysql_query</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$query</span>, <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">dbh</span><span style="color: #66cc66;">&#41;</span>;
++<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">num_queries</span>;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000066;">defined</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'SAVEQUERIES'</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; SAVEQUERIES <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">queries</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$query</span>, <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">timer_stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, 
                              <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">get_caller</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
.
.</pre></td></tr></table></div>

<p>You can modify the code to add some extra parameters as per your requirements, or modify the format in which it is returned. For example you could add code to save the data to a xml or csv file for latter retrieval and analysis.</p>
<p><strong>Note:</strong> Turning on the <em>define(&#8217;SAVEQUERIES&#8217;, true)</em> option can slow down your Wordpress site, so make sure you disable it after your work is done.</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/G3kiMXQf3hw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/benchmarking-wordpress-sql-using-firephp/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/mysql/benchmarking-wordpress-sql-using-firephp/</feedburner:origLink></item>
		<item>
		<title>Reading Google Analytics data from PHP</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/jKZDdnxxrmg/</link>
		<comments>http://www.codediesel.com/php/reading-google-analytics-data-from-php/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 10:42:59 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[google]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[analytics]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/php/reading-google-analytics-data-from-php/</guid>
		<description><![CDATA[How you can access the Google Analytics data from PHP]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://www.google.com/analytics/">Google Analytics</a> has become a important part of any web sites traffic analysis strategy. And with the release of the Analytics API people have been able to create custom reports and mashups for their organizations. Although no standard library is available from Google for PHP, some small and easy interfaces are available out there. In this post we will see how to access Google Analytics data using PHP using the GAPI library.<br />
<span id="more-2377"></span></p>
<h4>Installing the GAPI class</h4>
<p>The following code uses the  GAPI - Google Analytics API PHP Interface to help us access the Analytics data. So before proceeding you need to download the class file from the <a target="_blank" href="http://code.google.com/p/gapi-google-analytics-php-interface/">GAPI home</a>.</p>
<h4>Dimensions &#038; Metrics</h4>
<p>Before we start a small primer on Google Analytics Dimensions &#038; Metrics will be helpful.</p>
<p><strong>What is a dimension?</strong><br />
Dimensions are things like browser and country, browser versions, landing and exit pages, the URL of a page, and the source website a visitor has come from.<br />
Google Analytics uses the following 5 main marketing dimensions for analysis:</p>
<p>    * Source<br />
    * Medium<br />
    * Term<br />
    * Content<br />
    * Campaign</p>
<p><strong>What is a metric?</strong><br />
Metrics are the quantitative measure of a dimension for specific data types, such as counts of new visitors, page views, unique page views etc.</p>
<h4>Getting the Google Analytics data</h4>
<p>Now that we have seen what Dimensions &#038; Metrics are we will proceed with an example. In this example we will fetch the &#8217;source&#8217; dimension, which shows the source website the visitor is coming from and the &#8216;visits&#8217; metric. This tells us how many visitors are coming from a particular source to our site.</p>

<div class="wp_codebox"><table width="100%" ><tr id="237716"><td class="code" id="2377code16"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require</span> <span style="color: #ff0000;">'gapi-1.3/gapi.class.php'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* Set your Google Analytics credentials */</span>
<span style="color: #000066;">define</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ga_account'</span>     ,<span style="color: #ff0000;">'YOUR ANALYTICS EMAIL'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">define</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ga_password'</span>    ,<span style="color: #ff0000;">'YOUR ANALYTICS PASSWORD'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">define</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ga_profile_id'</span>  ,<span style="color: #ff0000;">'ANALYTICS SITE PROFILE ID'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$ga</span> = <span style="color: #000000; font-weight: bold;">new</span> gapi<span style="color: #66cc66;">&#40;</span>ga_account,ga_password<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* We are using the 'source' dimension and the 'visits' metrics */</span>
<span style="color: #0000ff;">$dimensions</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'source'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$metrics</span>    = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'visits'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* We will sort the result be desending order of visits, 
    and hence the '-' sign before the 'visits' string */</span>
<span style="color: #0000ff;">$ga</span>-&gt;<span style="color: #006600;">requestReportData</span><span style="color: #66cc66;">&#40;</span>ga_profile_id, <span style="color: #0000ff;">$dimensions</span>, <span style="color: #0000ff;">$metrics</span>,<span style="color: #ff0000;">'-visits'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$gaResults</span> = <span style="color: #0000ff;">$ga</span>-&gt;<span style="color: #006600;">getResults</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$i</span>=<span style="color: #cc66cc;">1</span>;
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$gaResults</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%-4d %-40s %5d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,
           <span style="color: #0000ff;">$i</span>++,
           <span style="color: #0000ff;">$result</span>-&gt;<span style="color: #006600;">getSource</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,
           <span style="color: #0000ff;">$result</span>-&gt;<span style="color: #006600;">getVisits</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>-----------------------------------------<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;Total Results : {$ga-&gt;getTotalResults()}&quot;</span>;    
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Below I&#8217;ve listed the top ten results of the output.</p>

<div class="wp_codebox"><table width="100%" ><tr id="237717"><td class="code" id="2377code17"><pre class="text">No.  Source                                   Visits
------------------------------------------------------
1    google                                   10549
2    (direct)                                  1484
3    stumbleupon.com                           1338
4    webintenta.com                             159
5    bing                                       142
6    yahoo                                      115
7    feedburner                                  97
8    phpdeveloper.org                            70
9    t3n.de                                      64
10   clearspace.openqa.org                       43</pre></td></tr></table></div>

<p>As you can see the main method in the class is the &#8216;requestReportData()&#8217;, which has the following structure:</p>

<div class="wp_codebox"><table width="100%" ><tr id="237718"><td class="code" id="2377code18"><pre class="php"><span style="color: #000000; font-weight: bold;">function</span> requestReportData<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$report_id</span>,      
                           <span style="color: #0000ff;">$dimensions</span>, 
                           <span style="color: #0000ff;">$metrics</span>, 
                           <span style="color: #0000ff;">$sort_metric</span>=<span style="color: #000000; font-weight: bold;">null</span>, 
                           <span style="color: #0000ff;">$filter</span>=<span style="color: #000000; font-weight: bold;">null</span>, 
                           <span style="color: #0000ff;">$start_date</span>=<span style="color: #000000; font-weight: bold;">null</span>, 
                           <span style="color: #0000ff;">$end_date</span>=<span style="color: #000000; font-weight: bold;">null</span>, 
                           <span style="color: #0000ff;">$start_index</span>=<span style="color: #cc66cc;">1</span>, 
                           <span style="color: #0000ff;">$max_results</span>=<span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>You can get the Dimension and Metric result by prefixing &#8216;get&#8217; to the dimension or metric name. For e.g, if you metric name is &#8216;region&#8217;, you can get the result by using:</p>

<div class="wp_codebox"><table width="100%" ><tr id="237719"><td class="code" id="2377code19"><pre class="php"><span style="color: #0000ff;">$result</span>-&gt;<span style="color: #006600;">getRegion</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Although I have only provided a single Dimension and Metric in the example code, the $dimensions and $metrics parameter can take multiple parameters as shown below:</p>

<div class="wp_codebox"><table width="100%" ><tr id="237720"><td class="code" id="2377code20"><pre class="php"><span style="color: #0000ff;">$dimensions</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'source'</span>, <span style="color: #ff0000;">'region'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$metrics</span>    = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'visits'</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Bear in mind to specify a valid &#8216;dimension-metric&#8217; combination as given in the <a href="http://code.google.com/apis/analytics/docs/gdata/gdataReferenceValidCombos.html">API Docs</a>. Specifying an invalid combination will return 0 results.</p>
<p>The output after adding the &#8216;region&#8217; dimension is shown below.</p>

<div class="wp_codebox"><table width="100%" ><tr id="237721"><td class="code" id="2377code21"><pre class="text">No. Source     Region               Visits
-----------------------------------------
1   google     (not set)              965
2   google     England                796
3   google     California             650
4   google     Maharashtra            343
5   google     Karnataka              340
6   google     Tamil Nadu             295
7   google     New York               225
8   google     Andhra Pradesh         189
9   google     Texas                  172
10  google     Ontario                165</pre></td></tr></table></div>

<h4>Modifying the amount of results returned</h4>
<p>By default the class returns the first 30 results, although the raw Google API returns 1000 results by default. You can increase the same by changing the $max_results parameter. Google Analytics returns a maximum of 10,000 results, even if you set the $max_results parameter above 10,000. To get the results above that limit you need to set the $start_index parameter to 10,000, which will then get the results starting at 10,000.</p>

<div class="wp_codebox"><table width="100%" ><tr id="237722"><td class="code" id="2377code22"><pre class="php"><span style="color: #0000ff;">$ga</span>-&gt;<span style="color: #006600;">requestReportData</span><span style="color: #66cc66;">&#40;</span>ga_profile_id,
                       <span style="color: #0000ff;">$dimensions</span>,
                       <span style="color: #0000ff;">$metrics</span>,
                       <span style="color: #ff0000;">'-visits'</span>, <span style="color: #808080; font-style: italic;">// Sort by 'visits' in descending order</span>
                       <span style="color: #ff0000;">''</span>,<span style="color: #ff0000;">''</span>,<span style="color: #ff0000;">''</span>, <span style="color: #808080; font-style: italic;">// We are not using this paras yet</span>
                       <span style="color: #cc66cc;">1</span>,  <span style="color: #808080; font-style: italic;">// Start Index</span>
                       <span style="color: #cc66cc;">500</span> <span style="color: #808080; font-style: italic;">// Max results</span>
                       <span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>You can get the total number of results by using the getTotalResults() method. This can be helpful in pagination.</p>

<div class="wp_codebox"><table width="100%" ><tr id="237723"><td class="code" id="2377code23"><pre class="php"><span style="color: #0000ff;">$ga</span>-&gt;<span style="color: #006600;">getTotalResults</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<h4>Setting the Google Analytics report period</h4>
<p>By default the class returns the data for the last 1 month from today if no report period is specified. You can specify the report period as below. The date format is in the form &#8216;YYYY-MM-DD&#8217;.</p>

<div class="wp_codebox"><table width="100%" ><tr id="237724"><td class="code" id="2377code24"><pre class="php"><span style="color: #0000ff;">$ga</span>-&gt;<span style="color: #006600;">requestReportData</span><span style="color: #66cc66;">&#40;</span>ga_profile_id,
                       <span style="color: #0000ff;">$dimensions</span>,
                       <span style="color: #0000ff;">$metrics</span>,
                       <span style="color: #ff0000;">'-visits'</span>, <span style="color: #808080; font-style: italic;">// Sort by 'visits' in descending order</span>
                       <span style="color: #ff0000;">''</span>,
                       <span style="color: #ff0000;">'2009-10-01'</span>, <span style="color: #808080; font-style: italic;">// Start Date</span>
                       <span style="color: #ff0000;">'2009-12-31'</span>, <span style="color: #808080; font-style: italic;">// End Date</span>
                       <span style="color: #cc66cc;">1</span>,  <span style="color: #808080; font-style: italic;">// Start Index</span>
                       <span style="color: #cc66cc;">500</span> <span style="color: #808080; font-style: italic;">// Max results</span>
                       <span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<h4>Filtering the data</h4>
<p>You can further filter the results using the $filter parameter. Filtering is a little tricky, so be sure to read the Google <a href="http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDataFeed.html#filters">documentation</a>.</p>

<div class="wp_codebox"><table width="100%" ><tr id="237725"><td class="code" id="2377code25"><pre class="php"><span style="color: #0000ff;">$filter</span> = <span style="color: #ff0000;">'country == United States'</span>;
&nbsp;
<span style="color: #0000ff;">$ga</span>-&gt;<span style="color: #006600;">requestReportData</span><span style="color: #66cc66;">&#40;</span>ga_profile_id,
                       <span style="color: #0000ff;">$dimensions</span>,
                       <span style="color: #0000ff;">$metrics</span>,
                       <span style="color: #ff0000;">'-visits'</span>, <span style="color: #808080; font-style: italic;">// Sort by 'visits' in descending order</span>
                       <span style="color: #0000ff;">$filter</span>, <span style="color: #808080; font-style: italic;">// Filter the data</span>
                       <span style="color: #ff0000;">'2009-10-01'</span>, <span style="color: #808080; font-style: italic;">// Start Date</span>
                       <span style="color: #ff0000;">'2009-12-31'</span>, <span style="color: #808080; font-style: italic;">// End Date</span>
                       <span style="color: #cc66cc;">1</span>,  <span style="color: #808080; font-style: italic;">// Start Index</span>
                       <span style="color: #cc66cc;">500</span> <span style="color: #808080; font-style: italic;">// Max results</span>
                       <span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>You can also combine various options:</p>

<div class="wp_codebox"><table width="100%" ><tr id="237726"><td class="code" id="2377code26"><pre class="php"><span style="color: #0000ff;">$filter</span> = <span style="color: #ff0000;">'country == United States &amp;&amp; visitorType == New Visitor'</span>;
<span style="color: #808080; font-style: italic;">// OR</span>
<span style="color: #0000ff;">$filter</span> = <span style="color: #ff0000;">'country == United States &amp;&amp;
           visitorType == New Visitor &amp;&amp;
           browser == Firefox'</span>;</pre></td></tr></table></div>

<h4>Getting a list of your Google Analytics Accounts</h4>
<p>Most users have more than one profiles in their Analytics account. You can easily get a list of all your accounts (sitename - profileid) with the following code.</p>

<div class="wp_codebox"><table width="100%" ><tr id="237727"><td class="code" id="2377code27"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require</span> <span style="color: #ff0000;">'gapi-1.3/gapi.class.php'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* Set your Google Analytics credentials */</span>
<span style="color: #000066;">define</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ga_account'</span>     ,<span style="color: #ff0000;">'YOUR ANALYTICS EMAIL'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">define</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ga_password'</span>    ,<span style="color: #ff0000;">'YOUR ANALYTICS PASSWORD'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$ga</span> = <span style="color: #000000; font-weight: bold;">new</span> gapi<span style="color: #66cc66;">&#40;</span>ga_account,ga_password<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$gaResult</span> = <span style="color: #0000ff;">$ga</span>-&gt;<span style="color: #006600;">requestAccountData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$gaResult</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%-30s %15d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #0000ff;">$result</span>, <span style="color: #0000ff;">$result</span>-&gt;<span style="color: #006600;">getProfileId</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>With this we conclude this post. It will be quite helpful if you go over the below references if you are unfamiliar with the Google Analytics API.</p>
<h4>Update</h4>
<p><a target="_blank" href="http://www.webresourcesdepot.com/feedcount-like-google-analytics-counter/">Webresourcesdepot</a> features a nifty little application using the above to dispay the Google pageviews in a Feed-burner style chicklet display.</p>
<blockquote><p>
References:<br />
a. <a href="http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDataFeed.html#dataRequest">Google Analytics Data API - Data Feed</a><br />
b. <a href="http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html">Dimensions &#038; Metrics Reference</a><br />
c. <a href="http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDataFeed.html#filters">Filters</a>
</p></blockquote>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/jKZDdnxxrmg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/reading-google-analytics-data-from-php/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/php/reading-google-analytics-data-from-php/</feedburner:origLink></item>
		<item>
		<title>‘isset’ construct and multiple parameters</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/R_KiLO83wL0/</link>
		<comments>http://www.codediesel.com/php/isset-construct-and-multiple-parameters/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 10:44:09 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/php/isset-construct-and-multiple-parameters/</guid>
		<description><![CDATA[how the isset construct takes multiple parameters]]></description>
			<content:encoded><![CDATA[<p>PHP programmers are frequently into using the various <em>is_x</em> group of functions: is_int, is_null etc. All of this functions take a single parameter which leads you to believe that other similar constructs take only a single parameter. Like the <em>isset</em> and <em>unset</em> constructs, which by the way take multiple parameters as shown below, which might help you save some keystrokes.<br />
<span id="more-2368"></span><br />
So instead of writing:</p>

<div class="wp_codebox"><table width="100%" ><tr id="236828"><td class="code" id="2368code28"><pre class="php"><span style="color: #0000ff;">$var1</span> = <span style="color: #ff0000;">'a'</span>;
<span style="color: #0000ff;">$var2</span> = <span style="color: #ff0000;">'b'</span>;
<span style="color: #0000ff;">$var3</span> = <span style="color: #ff0000;">'c'</span>;
<span style="color: #0000ff;">$var4</span> = <span style="color: #ff0000;">'d'</span>;
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var1</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var2</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var3</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// All variables are set, do something</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>We can write:</p>

<div class="wp_codebox"><table width="100%" ><tr id="236829"><td class="code" id="2368code29"><pre class="php"><span style="color: #0000ff;">$var1</span> = <span style="color: #ff0000;">'a'</span>;
<span style="color: #0000ff;">$var2</span> = <span style="color: #ff0000;">'b'</span>;
<span style="color: #0000ff;">$var3</span> = <span style="color: #ff0000;">'c'</span>;
<span style="color: #0000ff;">$var4</span> = <span style="color: #ff0000;">'d'</span>;
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var1</span>, <span style="color: #0000ff;">$var2</span>, <span style="color: #0000ff;">$var3</span>, <span style="color: #0000ff;">$var4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// Will return TRUE only if all of the parameters are set</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Same with the unset() construct:</p>

<div class="wp_codebox"><table width="100%" ><tr id="236830"><td class="code" id="2368code30"><pre class="php"><span style="color: #000066;">unset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var1</span>, <span style="color: #0000ff;">$var2</span>, <span style="color: #0000ff;">$var3</span>, <span style="color: #0000ff;">$var4</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/R_KiLO83wL0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/isset-construct-and-multiple-parameters/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/php/isset-construct-and-multiple-parameters/</feedburner:origLink></item>
		<item>
		<title>Adding custom commands to Selenium</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/RhgHKoiU8Cw/</link>
		<comments>http://www.codediesel.com/testing/adding-custom-commands-to-the-selenium-ide/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 09:44:35 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[testing]]></category>

		<category><![CDATA[selenium]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/sql/extending-selenium-ide/</guid>
		<description><![CDATA[how to extend and add your own commands to the selenium ide]]></description>
			<content:encoded><![CDATA[<p>In the previous <a href="http://www.codediesel.com/php/selenium-ide-tutorial-part-1/">Selenium posts</a> we saw how to use the selenium IDE for testing web pages. In this post we will see how to extend the Selenium IDE by adding our own custom commands. </p>
<p>Selenium IDE offers many useful commands (often called Selenese) for testing purposes. You use this sequence of commands to create various tests. But many times these commands are not sufficient and you need to add your own custom commands. For example a reader recently requested on how to input unique email ids for testing. Of course you can easily do this using the Selenium RC server and a language like PHP or Java. But we want to accomplish this in the IDE itself.<br />
<span id="more-2307"></span></p>
<h4>Types of Selenium commands</h4>
<p>Selenium commands come in three varieties : Actions, Accessors and Assertions. Each Selenium command being given in the format below:</p>
<table style="background-color: #FDF7E7; height: 20px; width: 230px; border: 1px solid #c0c0c0;">
<tr>
<td style="border-right: 1px solid #c0c0c0;">command</td>
<td style="border-right: 1px solid #c0c0c0;">target</td>
<td>value</td>
</tr>
</table>
<p><strong>Actions</strong> are commands which in general modify the state of the application; <em>click</em>, <em>typeKeys</em>, <em>deleteCookie</em> etc. are all actions. Many actions also come with a sibling with a &#8220;AndWait&#8221; suffix added; e.g. ‘typeKeysAndWait’. This tells Selenium to wait until the page loads after performing the particular action.</p>
<p><strong>Accessors</strong> examine the state of the application and store the results in variables, e.g. &#8220;storeLocation&#8221;. </p>
<p><strong>Assertions</strong> are like Accessors, but they verify that the state of the application conforms to what is expected</p>
<p>In this post we will see how to write Selenium Action command using a simple example.</p>
<h4>Writing your first custom Selenium Action Command</h4>
<p>In the following example we will add a new action, ‘typeRandomEmail’, which generates a random email address and types it in an input field.</p>
<p>Every Selenium Action command starts with a &#8216;do&#8217; prefix. The corresponding action comprising &#8216;AndWait&#8217; is automatically registered.  e.g when we create the &#8216;typeRandomEmail&#8217; action, the &#8216;typeRandomEmailAndWait’ command is automatically added to the command list. An action command takes up to two parameters, which will be passed the second and third column values in the test table. In our example we will be using only one parameter - the locator id where the random email will be typed.</p>
<p>The following shows the code for the <em>typeRandomEmail</em> action.</p>

<div class="wp_codebox"><table width="100%" ><tr id="230731"><td class="code" id="2307code31"><pre class="javascript">&nbsp;
Selenium.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">doTypeRandomEmail</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>locator<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #009900; font-style: italic;">/**
    * Sets the value of an input field to a random email id, 
    * as though you typed it in.
    *
    * @param locator an &lt;a href=&quot;#locators&quot;&gt;element locator&lt;/a&gt;
    */</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">// All locator-strategies are automatically handled by &quot;findElement&quot;</span>
    <span style="color: #003366; font-weight: bold;">var</span> element = <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">page</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">findElement</span><span style="color: #66cc66;">&#40;</span>locator<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #009900; font-style: italic;">/* The following block generates a random email string */</span>
    <span style="color: #003366; font-weight: bold;">var</span> allowedChars = <span style="color: #3366CC;">&quot;abcdefghiklmnopqrstuvwxyz&quot;</span>;
    <span style="color: #003366; font-weight: bold;">var</span> stringLength = <span style="color: #CC0000;">8</span>;
    <span style="color: #003366; font-weight: bold;">var</span> randomstring = <span style="color: #3366CC;">''</span>;
&nbsp;
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i=<span style="color: #CC0000;">0</span>; i&lt;stringLength; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> rnum = Math.<span style="color: #006600;">floor</span><span style="color: #66cc66;">&#40;</span>Math.<span style="color: #006600;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> * allowedChars.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span>;
        randomstring += allowedChars.<span style="color: #006600;">substring</span><span style="color: #66cc66;">&#40;</span>rnum,rnum<span style="color: #CC0000;">+1</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">// Append a domain name</span>
    randomstring += <span style="color: #3366CC;">&quot;@somedomain.com&quot;</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">// Replace the element text with the new text</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">browserbot</span>.<span style="color: #006600;">replaceText</span><span style="color: #66cc66;">&#40;</span>element, randomstring<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>;</pre></td></tr></table></div>

<h4>Importing your custom commands in Selenium</h4>
<p>Now that we have completed writing our own action command, it is time to import it to the Selenium IDE. Your custom commands are usually added to the file ‘user-extensions.js’. By default the ‘user-extensions.js’ file does not exist, so you need to first create a blank file into which you will add the above code. Once that is done you then need to tell Selenium to load the particular extension file from the ‘Options’ menu.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2010/01/selenium_options1.gif"><img src="http://www.codediesel.com/wp-content/uploads/2010/01/selenium_options1.gif" alt="selenium_options_page" title="selenium_options_page" width="428" height="376" class="size-full wp-image-2313" /></a></p>
<p>Once you have added the file to the options page you need to restart the Selenium IDE for the changes to take effect.</p>
<p><strong>Whenever you add some new code or make changes to the exiting one in the ‘user-extensions.js’, you need to restart the Selenium IDE for the changes to take effect.</strong></p>
<p>Now once you have restarted the IDE, you can see that your command has been added to the drop-down.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2010/01/selenium_options_2.gif"><img src="http://www.codediesel.com/wp-content/uploads/2010/01/selenium_options_2.gif" alt="selenium_ide" title="selenium_ide" width="398" height="516" class="aligncenter size-full wp-image-2319" /></a></p>
<h4>Writing other Action commands</h4>
<p>You may be wondering how to go about writing other Selenium Action commands; what the <em>this.browserbot</em> object means and where it is defined. Although no clear documentation exists, the Selenium source code can itself be a valuable tool. All the Selenium commands are stored in the &#8217;selenium-api.js&#8217; file. I have included it <a href="http://www.codediesel.com/data/code/selenium-api.js">here</a> for easy download and reference. The original source file and other support files are located in the &#8217;selenium-ide.jar&#8217; file in your Selenium installation directory. Unzip that file and you have all the Selenium source code at your disposal.</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/RhgHKoiU8Cw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/testing/adding-custom-commands-to-the-selenium-ide/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/testing/adding-custom-commands-to-the-selenium-ide/</feedburner:origLink></item>
		<item>
		<title>Test dependencies in PHPUnit 3.4</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/hwMRB0QsqWc/</link>
		<comments>http://www.codediesel.com/testing/test-dependencies-in-phpunit/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 08:54:33 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[testing]]></category>

		<category><![CDATA[phpunit]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/sql/test-dependencies-in-phpunit/</guid>
		<description><![CDATA[writing dependent tests in phpunit 3.4]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://www.phpunit.de/">PHPUnit</a> has become the de-facto standard for unit testing PHP code. Now in version 3.4, it has added many new and interesting features to its repertoire.</p>
<h4>Dependencies in PHPUnit tests</h4>
<p>PHPUnit 3.4 now supports dependencies between different test methods. It allows you to execute a particular test <em>ONLY IF</em> the test that it depends on executes successfully. Take the following example (Listing 1.) where we test a <a href="http://www.codediesel.com/php/linked-list-in-php/">linked-list class</a> I developed earlier.<br />
<span id="more-2217"></span></p>
<h4>A simple unit-test with PHPUnit</h4>
<p>The test basically creates a link-list with 100 nodes, and tests a few class methods - reverseList, deleteFirstNode, deleteLastNode. As you can see the linklist creation part and method tests are combined in the single test method <em>testLinkList()</em>. </p>
<p>The primary focus here is on testing the three class functions. There is nothing wrong with this. But what if the linklist creation part itself fails before the other tests are conducted? Of course we can add another test to check that the links are being created correctly, but that mixes up the two parts.</p>
<p><b>Listing 1.</b></p>

<div class="wp_codebox"><table width="100%" ><tr id="221732"><td class="code" id="2217code32"><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: #808080; font-style: italic;">/* How many nodes to create */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$_nNodes</span> = <span style="color: #cc66cc;">100</span>;
&nbsp;
    <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>
        <span style="color: #808080; font-style: italic;">/* Linklist creation block */</span>
        <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;">$this</span>-&gt;_nNodes; <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;
&nbsp;
        <span style="color: #808080; font-style: italic;">/* 
            Linklist methods testing block :
&nbsp;
            $theList-&gt;totalNodes() is a public function 
            in the 'linklist' class that returns the total
            number of nodes in the link-list.
        */</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;">$this</span>-&gt;_nNodes, <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;">$this</span>-&gt;_nNodes - <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;">$this</span>-&gt;_nNodes - <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>;
    <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>

<h4>Splitting up tests</h4>
<p>A better way is to split the linklist creation part and class method testing part into two separate test functions as below (Listing 2). Here the linklist is created in the &#8216;testCreateList()&#8217; method and the testing of the class methods is done in the &#8216;testLinkList()&#8217; method. This allows us to keep the two testing blocks separate.</p>
<p><b>Listing 2.</b></p>

<div class="wp_codebox"><table width="100%" ><tr id="221733"><td class="code" id="2217code33"><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: #808080; font-style: italic;">/* How many nodes to create */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$_nNodes</span> = <span style="color: #cc66cc;">100</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testCreateList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <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;">$this</span>-&gt;_nNodes; <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>
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertTrue</span><span style="color: #66cc66;">&#40;</span>!<span style="color: #000066;">empty</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$theList</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$theList</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @depends testCreateList
     */</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: #0000ff;">$theList</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <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;">$this</span>-&gt;_nNodes, <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;">$this</span>-&gt;_nNodes - <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;">$this</span>-&gt;_nNodes - <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>;
    <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>The important point to note here is that we have specified that the &#8216;<em>testLinkList</em>&#8216; method depends on the &#8216;<em>testCreateList</em>&#8216; method, with a @depends annotation. This tells PHPUnit that if the &#8216;<em>testCreateList</em>&#8216;  test fails then the test dependent on it, &#8216;<em>testLinkList</em>&#8216;, will have to be skipped, which is a logical way to go.</p>

<div class="wp_codebox"><table width="100%" ><tr id="221734"><td class="code" id="2217code34"><pre class="php"><span style="color: #808080; font-style: italic;">/**
 * @depends testCreateList
 */</span></pre></td></tr></table></div>

<h4>Producers &#038; Consumers in a Test</h4>
<p>First some definitions:</p>
<blockquote><p>
a. A producer is a test method that yields its unit under test as return value.<br />
b. A consumer is a test method that depends on one or more producers and their return values.
</p></blockquote>
<p>What it means is that a &#8216;Producer&#8217; is a test method that returns some data that will be used by a &#8216;Consumer&#8217; test method. In our example the &#8216;<em>testCreateList</em>&#8216; is a Producer and the &#8216;<em>testLinkList</em>&#8216; is the Consumer.  The &#8216;<em>testCreateList</em>&#8216; method returns a linklist object which is then received by the dependent method &#8216;<em>testLinkList</em>&#8216;. So if for some reason the &#8216;<em>testCreateList</em>&#8216; fails to return some data then the test that depends on it,  &#8216;<em>testLinkList</em>&#8216;, will have to be skipped, as it has nothing to work with.</p>
<p>Bear in mind that it does not mean that every test method that depends on some other method has to accept some arguments from the method it is dependent on. Lets breakup the above example further (Listing 3). Here we have added one more test method, &#8216;<em>testNodesDefined</em>&#8216;, which makes sure that the &#8216;$_nNodes&#8217; variable is not null. The &#8216;<em>testCreateList</em>&#8216; method is dependent on this method, so that if the &#8216;$_nNodes&#8217; is null then the &#8216;<em>testCreateList</em>&#8216; is skipped, which causes the test &#8216;<em>testLinkList</em>&#8216; to be skipped.</p>
<p><b>Listing 3.</b></p>

<div class="wp_codebox"><table width="100%" ><tr id="221735"><td class="code" id="2217code35"><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: #808080; font-style: italic;">/* How many nodes to create */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$_nNodes</span> = <span style="color: #cc66cc;">100</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testNodesDefined<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;">assertNotNull</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_nNodes<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @depends testNodesDefined
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testCreateList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <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;">$this</span>-&gt;_nNodes; <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>
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">assertTrue</span><span style="color: #66cc66;">&#40;</span>!<span style="color: #000066;">empty</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$theList</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$theList</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @depends testCreateList
     */</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: #0000ff;">$theList</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <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;">$this</span>-&gt;_nNodes, <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;">$this</span>-&gt;_nNodes - <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;">$this</span>-&gt;_nNodes - <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>;
    <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 output from a failed test, when we set  &#8216;$_nNodes&#8217; to null, is shown below.<br />
<b>Listing 4.</b></p>

<div class="wp_codebox"><table width="100%" ><tr id="221736"><td class="code" id="2217code36"><pre class="text">D:\localhost\datastructures&gt;phpunit --verbose LinkListTest
PHPUnit 3.4.2 by Sebastian Bergmann.
&nbsp;
LinkListTest
FSS
&nbsp;
Time: 0 seconds
&nbsp;
There was 1 failure:
&nbsp;
1) LinkListTest::testNodesDefined
Failed asserting that &lt;null&gt; is not null.
&nbsp;
D:\localhost\test\datastructures\LinkListTest.php:13
&nbsp;
There were 2 skipped tests:
&nbsp;
1) LinkListTest::testCreateList
This test depends on &quot;LinkListTest::testNodesDefined&quot; to pass.
&nbsp;
&nbsp;
2) LinkListTest::testLinkList
This test depends on &quot;LinkListTest::testCreateList&quot; to pass.
&nbsp;
&nbsp;
FAILURES!
Tests: 1, Assertions: 2, Failures: 1, Skipped: 2.
&nbsp;
D:\localhost\test\datastructures&gt;</pre></td></tr></table></div>

<blockquote><p>
Note: The tests are executed in the sequence in which you wrote them. So take care while writing dependent tests. For example, sequencing test methods as below will not work.
</p></blockquote>

<div class="wp_codebox"><table width="100%" ><tr id="221737"><td class="code" id="2217code37"><pre class="php">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testNodesDefined<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;">assertNotNull</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_nNodes<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @depends testCreateList
     */</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: #0000ff;">$theList</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        ..
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @depends testNodesDefined
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testCreateList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        ..
    <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h4>What all of this has brought us</h4>
<p>Testing a complex class in a single humongous test method is not wrong, but can create a confusing mess of &#8216;asserts&#8217;. Splitting a single test method into multiple dependent tests can help you segregate your test methods by functionality which in turn will get you to quickly localize defects in your code.</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/hwMRB0QsqWc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/testing/test-dependencies-in-phpunit/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/testing/test-dependencies-in-phpunit/</feedburner:origLink></item>
		<item>
		<title>Creating win/win projects using Project Value Analysis</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/PrLzC95wfIY/</link>
		<comments>http://www.codediesel.com/software/creating-win-win-projects-using-project-value-analysis/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 07:41:42 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[software]]></category>

		<category><![CDATA[clients]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/software/6-maxims-to-satisfy-a-client/</guid>
		<description><![CDATA[importance of Project Value Analysis in delivering a successful project]]></description>
			<content:encoded><![CDATA[<p>From a client’s perspective, the most important criteria when developing any new software is that it adds some &#8216;value&#8217; to his company. Every software application has some financial and some intangible value attached with it. Without it there would be no reason for a client to invest in developing new software or for that matter, upgrading an existing one. The client has some financial goals in his mind when he proposes to develop some software. He plans to make some profit (financial or otherwise) from the development of the software. Whether the financial gains are immediate or peripheral is a different matter.<br />
<span id="more-2266"></span><br />
But it is a complete different scene at the typical software development house. Developers are usually in the dark about the value aspects of the software they are developing. This most often, but not always, leads to a restrained and blithe attitude of developers. Not to squarely put the blame on the developers or management only, many times  the fault also lies with the client, who fails to communicate the ‘expected value’ of the proposed software clearly.</p>
<p>As a small reminder, below are six of the most important expectations the client has from the developed software, every other thing hinges on these:</p>
<blockquote><p>
1. Revenue generation from the new application<br />
2. Cost reductions from the new application or upgrade<br />
3. Indirect revenue generation<br />
4. Increase in exiting user base due to some new features<br />
5. Increase in market share<br />
6. Revenue increase in companion application
</p></blockquote>
<p>The points raised above may come as common-sense; but how many times when developing for a client, have you given thought to these matters. We developers and usually busy thinking about the best way to structure the application, the best way to run a SQL query, the best MVC framework;  leaving the business and value aspects of the proposed software to the management; who usually do not find it prudent to enlighten the developers’ in that matter. It’s not that the technical issues are not important; what is wrong is the predominant focus on technical issues.</p>
<p>Taking the business and value aspects into consideration during development enables you to view the project from the clients perspective. This in itself makes taking decisions regarding certain technical issues easier. It is even more important for you to take the business issues into consideration if you are a freelance developer, where you are solely responsible for bad project outcome, unlike a company where you can play the blame game and walk away from the mess unscathed.</p>
<p>In a nutshell, a client does not care if you have used the latest MVC framework, or used your expert programming skills to design that perfect algorithm. What they truly care is whether the developed software will garner them some intrinsic value more then what they paid for, which in business parlance we know as ROI. Everything else is just details. </p>
<p>Consider those matters when developing your next project and you will have a win/win project in your hands with a satisfied and smiling client as a bonus.</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/PrLzC95wfIY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/software/creating-win-win-projects-using-project-value-analysis/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/software/creating-win-win-projects-using-project-value-analysis/</feedburner:origLink></item>
		<item>
		<title>7 essential php command-line options</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/K7wBykkFV8M/</link>
		<comments>http://www.codediesel.com/php/7-essential-php-commandline-options/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 05:30:24 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[addins]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[tip]]></category>

		<category><![CDATA[php cli]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2163</guid>
		<description><![CDATA[some essential php command-line options for your use]]></description>
			<content:encoded><![CDATA[<p>Most of us use PHP from a IDE or using a simple text editor with a browser, rarely dropping down to the command-line for running php programs. But php provides some interesting and quick options you can use to perform various common tasks or to debug some nasty installation problems. Below is a list of some useful options you should be familiar with.<br />
<span id="more-2163"></span><br />
There are two ways to specify options on the command line - <em>Short Option</em>, where you specify a option by a single character and a <em>Long Option</em>, where you use a more readable string to specify the option, such as &#8216;&#8211;php-ini&#8217;. In this post we will be using the <em>Short Option</em>.</p>
<h3>PHP Command-line options</h3>
<h4>1 : Using a different php.ini file</h4>
<p>The following option can be useful when you need to test various ini configurations, or you need to check if all the modules in a particular ini are loading correctly.</p>

<div class="wp_codebox"><table width="100%" ><tr id="216338"><td class="code" id="2163code38"><pre class="text">php –c  PATH/to/PHP.INI example.php</pre></td></tr></table></div>

<h4>2 : Check to see for any syntax errors in a php file.</h4>
<p>Need to quickly check for php syntax errors if you do not have access to an IDE, then this option can be quite helpful. Combine it with some shell code and you can easily check for syntax errors for multiple files in a directory.</p>

<div class="wp_codebox"><table width="100%" ><tr id="216339"><td class="code" id="2163code39"><pre class="text">php –l example.php</pre></td></tr></table></div>

<p>You can for example use the windows command line script to scan all the php files in the current directory for syntax errors.</p>

<div class="wp_codebox"><table width="100%" ><tr id="216340"><td class="code" id="2163code40"><pre class="dos">c:\localhost&gt;for %%X <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span>*.php<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">do</span> php -l %%X</pre></td></tr></table></div>

<h4>3 : Display php builtin and loaded modules</h4>

<div class="wp_codebox"><table width="100%" ><tr id="216341"><td class="code" id="2163code41"><pre class="text">php –m</pre></td></tr></table></div>

<p>Combine it with php –c to debug any module loading errors for different php.ini files</p>

<div class="wp_codebox"><table width="100%" ><tr id="216342"><td class="code" id="2163code42"><pre class="text">php –c PATH/to/PHP.INI -m</pre></td></tr></table></div>

<p>This came in handy when I was installing php 5.3 and for some reasons the xdebug extension was not loading. With the help of the above command I was able to narrow down the problem to the following reason:</p>

<div class="wp_codebox"><table width="100%" ><tr id="216343"><td class="code" id="2163code43"><pre class="text">Xdebug requires Zend Engine API version 220090115.
The Zend Engine API version 220090626 which is installed, is newer.
Contact Derick Rethans at http://xdebug.org for a later 
version of Xdebug.</pre></td></tr></table></div>

<h4>4 : Syntax Highlighting</h4>
<p>The following option will display syntax highlighted text using php’s internal mechanism.</p>

<div class="wp_codebox"><table width="100%" ><tr id="216344"><td class="code" id="2163code44"><pre class="text">php  –s  example.php</pre></td></tr></table></div>

<p>You can then write the output to another file using a redirect.</p>

<div class="wp_codebox"><table width="100%" ><tr id="216345"><td class="code" id="2163code45"><pre class="text">php  –s  example.php  &gt;  syntax.html</pre></td></tr></table></div>

<h4>5 : Strip the source code of comments and whitespace.</h4>
<p>Can be useful if you need to reduce the file size.</p>

<div class="wp_codebox"><table width="100%" ><tr id="216346"><td class="code" id="2163code46"><pre class="text">php –w example.php</pre></td></tr></table></div>

<h4>6 : Run php code from the command line</h4>
<p>Directly run php code from the command line without using the php start and end tags.</p>

<div class="wp_codebox"><table width="100%" ><tr id="216347"><td class="code" id="2163code47"><pre class="text">php -r &quot;$foo = 'hello!'; $foo = strtoupper($foo); echo $foo;&quot;</pre></td></tr></table></div>

<h4>7 : Display information related to a internal function, class or extension</h4>
<p><i>(Available as of PHP 5.1.2)</i><br />
These options can be quite handy when a PHP manual is not within reach.</p>

<div class="wp_codebox"><table width="100%" ><tr id="216348"><td class="code" id="2163code48"><pre class="text">php --r[fcei]</pre></td></tr></table></div>

<p>Show information about a function:</p>

<div class="wp_codebox"><table width="100%" ><tr id="216349"><td class="code" id="2163code49"><pre class="text">php --rf usort</pre></td></tr></table></div>

<p>Example output for the above:</p>

<div class="wp_codebox"><table width="100%" ><tr id="216350"><td class="code" id="2163code50"><pre class="php"><span style="color: #000000; font-weight: bold;">Function</span> <span style="color: #66cc66;">&#91;</span> &lt;internal:standard&gt; <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #000066;">usort</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
  - Parameters <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</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; &amp;$arg ]</span>
    Parameter <span style="color: #808080; font-style: italic;">#1 [ &lt;required&gt; $cmp_function ]</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Show information about a class:</p>

<div class="wp_codebox"><table width="100%" ><tr id="216351"><td class="code" id="2163code51"><pre class="text">php --rc EmptyIterator</pre></td></tr></table></div>

<p>Example output for the above:</p>

<div class="wp_codebox"><table width="100%" ><tr id="216352"><td class="code" id="2163code52"><pre class="php"><span style="color: #000000; font-weight: bold;">Class</span> <span style="color: #66cc66;">&#91;</span> &lt;internal:SPL&gt; &lt;iterateable&gt; <span style="color: #000000; font-weight: bold;">class</span> EmptyIterator 
          implements Iterator, Traversable <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
  - Constants <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #66cc66;">&#125;</span>
  - <span style="color: #000066;">Static</span> properties <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #66cc66;">&#125;</span>
  - <span style="color: #000066;">Static</span> methods <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #66cc66;">&#125;</span>
  - Properties <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #66cc66;">&#125;</span>
  - Methods <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
    Method <span style="color: #66cc66;">&#91;</span> &lt;internal:SPL, prototype Iterator&gt; <span style="color: #000000; font-weight: bold;">public</span> method <span style="color: #000066;">rewind</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#125;</span>
    Method <span style="color: #66cc66;">&#91;</span> &lt;internal:SPL, prototype Iterator&gt; <span style="color: #000000; font-weight: bold;">public</span> method valid <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#125;</span>
    Method <span style="color: #66cc66;">&#91;</span> &lt;internal:SPL, prototype Iterator&gt; <span style="color: #000000; font-weight: bold;">public</span> method <span style="color: #000066;">key</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#125;</span>
    Method <span style="color: #66cc66;">&#91;</span> &lt;internal:SPL, prototype Iterator&gt; <span style="color: #000000; font-weight: bold;">public</span> method <span style="color: #000066;">current</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#125;</span>
    Method <span style="color: #66cc66;">&#91;</span> &lt;internal:SPL, prototype Iterator&gt; <span style="color: #000000; font-weight: bold;">public</span> method <span style="color: #000066;">next</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Show information about a extension:</p>

<div class="wp_codebox"><table width="100%" ><tr id="216353"><td class="code" id="2163code53"><pre class="text">php  --re json</pre></td></tr></table></div>

<p>Example output for the above:</p>

<div class="wp_codebox"><table width="100%" ><tr id="216354"><td class="code" id="2163code54"><pre class="php">xtension <span style="color: #66cc66;">&#91;</span> &lt;persistent&gt; extension <span style="color: #808080; font-style: italic;">#10 json version 1.2.1 ] {</span>
&nbsp;
  - Functions <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">Function</span> <span style="color: #66cc66;">&#91;</span> &lt;internal:json&gt; <span style="color: #000000; font-weight: bold;">function</span> json_encode <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">Function</span> <span style="color: #66cc66;">&#91;</span> &lt;internal:json&gt; <span style="color: #000000; font-weight: bold;">function</span> json_decode <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Show configuration information for a particular extension:</p>

<div class="wp_codebox"><table width="100%" ><tr id="216355"><td class="code" id="2163code55"><pre class="text">php  --ri tidy</pre></td></tr></table></div>

<p>Example output for the above:</p>

<div class="wp_codebox"><table width="100%" ><tr id="216356"><td class="code" id="2163code56"><pre class="php">tidy
&nbsp;
Tidy support =&gt; enabled
<span style="color: #ff0000">libTidy Release =&gt; <span style="color: #cc66cc;">15</span> August <span style="color: #cc66cc;">2007</span>
Extension Version =&gt; <span style="color: #cc66cc;">2.0</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$Id</span>: tidy.c,v <span style="color: #cc66cc;">1.66</span><span style="color: #cc66cc;">.2</span><span style="color: #cc66cc;">.8</span><span style="color: #cc66cc;">.2</span><span style="color: #cc66cc;">.24</span> <span style="color: #cc66cc;">2007</span>/<span style="color: #cc66cc;">09</span>/<span style="color: #cc66cc;">20</span> <span style="color: #cc66cc;">22</span>:<span style="color: #cc66cc;">25</span>:<span style="color: #cc66cc;">05</span> nlopess <span style="color: #000066;">Exp</span> $<span style="color: #66cc66;">&#41;</span>
</span>
Directive =&gt; Local Value =&gt; Master Value
tidy.default_config =&gt; no value =&gt; no value
tidy.clean_output =&gt; no value =&gt; no value</pre></td></tr></table></div>

<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/K7wBykkFV8M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/7-essential-php-commandline-options/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/php/7-essential-php-commandline-options/</feedburner:origLink></item>
		<item>
		<title>Sorting PHP arrays with substrings</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/fZZnQSxplnc/</link>
		<comments>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 12:35:17 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[algorithms]]></category>

		<category><![CDATA[data]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[algorithm]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2141</guid>
		<description><![CDATA[Sorting PHP arrays using usort with substrings]]></description>
			<content:encoded><![CDATA[<p>A couple of days back I encountered the following request on a forum for sorting a single dimensional array. The programmer wanted to sort the following array by the substring after the colon. For example &#8216;CDF&#8217; in the string &#8216;66345:CDF&#8217;. Those values that do not have any colon should be ignored and pushed to the end of the array. Also, the length of the strings are not constant.</p>

<div class="wp_codebox"><table width="100%" ><tr id="214157"><td class="code" id="2141code57"><pre class="php"><span style="color: #0000ff;">$values</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;66345:CDF&quot;</span>, <span style="color: #ff0000;">&quot;61179:HGT&quot;</span>, <span style="color: #ff0000;">&quot;64146:ABA&quot;</span>,
                <span style="color: #ff0000;">&quot;68768:BNG&quot;</span>, <span style="color: #ff0000;">&quot;68015:ZCZ&quot;</span>, <span style="color: #ff0000;">&quot;80231:LPO&quot;</span>,
                <span style="color: #ff0000;">&quot;64146:QWP&quot;</span>, <span style="color: #ff0000;">&quot;68736:HHB&quot;</span>, <span style="color: #ff0000;">&quot;86801:MNV&quot;</span>,
                <span style="color: #ff0000;">&quot;80178:OIU&quot;</span>, <span style="color: #ff0000;">&quot;80178:ASE&quot;</span>, <span style="color: #ff0000;">&quot;88178:BRT&quot;</span>,
                <span style="color: #ff0000;">&quot;801782OIU&quot;</span>, <span style="color: #ff0000;">&quot;801378ASE&quot;</span>, <span style="color: #ff0000;">&quot;881578BRT&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p><span id="more-2141"></span></p>
<p>A quick solution was the following. But I&#8217;m sure that this is not the fastest, as a matter of fact it is quite slow for larger arrays.</p>

<div class="wp_codebox"><table width="100%" ><tr id="214158"><td class="code" id="2141code58"><pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #0000ff;">$values</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;66345:CDF&quot;</span>, <span style="color: #ff0000;">&quot;61179:HGT&quot;</span>, <span style="color: #ff0000;">&quot;64146:ABA&quot;</span>,
                <span style="color: #ff0000;">&quot;68768:BNG&quot;</span>, <span style="color: #ff0000;">&quot;68015:ZCZ&quot;</span>, <span style="color: #ff0000;">&quot;80231:LPO&quot;</span>,
                <span style="color: #ff0000;">&quot;64146:QWP&quot;</span>, <span style="color: #ff0000;">&quot;68736:HHB&quot;</span>, <span style="color: #ff0000;">&quot;86801:MNV&quot;</span>,
                <span style="color: #ff0000;">&quot;80178:OIU&quot;</span>, <span style="color: #ff0000;">&quot;80178:ASE&quot;</span>, <span style="color: #ff0000;">&quot;88178:BRT&quot;</span>,
                <span style="color: #ff0000;">&quot;801782OIU&quot;</span>, <span style="color: #ff0000;">&quot;801378ASE&quot;</span>, <span style="color: #ff0000;">&quot;881578BRT&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> my_cmp<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span>, <span style="color: #0000ff;">$b</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$pieces_a</span> = <span style="color: #000066;">explode</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;:&quot;</span>, <span style="color: #0000ff;">$a</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$pieces_b</span> = <span style="color: #000066;">explode</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;:&quot;</span>, <span style="color: #0000ff;">$b</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!<span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pieces_a</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> &amp;&amp; <span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pieces_b</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: #b1b100;">return</span> <span style="color: #cc66cc;">1</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">elseif</span><span style="color: #66cc66;">&#40;</span>!<span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pieces_b</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> &amp;&amp; <span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pieces_a</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: #b1b100;">return</span> <span style="color: #cc66cc;">-1</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">elseif</span><span style="color: #66cc66;">&#40;</span>!<span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pieces_a</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> &amp;&amp; !<span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pieces_b</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: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000066;">strcasecmp</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pieces_a</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #0000ff;">$pieces_b</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;">&#125;</span>
&nbsp;
<span style="color: #000066;">usort</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$values</span>, <span style="color: #ff0000;">&quot;my_cmp&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>So what will be the fastest algorithm for the same?</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/fZZnQSxplnc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/</feedburner:origLink></item>
	</channel>
</rss>
