<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Book and the Cover &#187; PHP</title>
	<atom:link href="http://benjaminsterling.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://benjaminsterling.com</link>
	<description>Ok, you did not judge the book by it&#039;s cover, great.  Now, read the whole thing before passing judgment.</description>
	<lastBuildDate>Tue, 24 Mar 2009 19:25:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Things I&#8217;ve Learned While building a WordPress Plugin</title>
		<link>http://benjaminsterling.com/things-ive-learned-while-building-a-wordpress-plugin/</link>
		<comments>http://benjaminsterling.com/things-ive-learned-while-building-a-wordpress-plugin/#comments</comments>
		<pubDate>Sat, 15 Mar 2008 03:47:52 +0000</pubDate>
		<dc:creator>Benjamin Sterling</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://benjaminsterling.com/things-ive-learned-while-building-a-wordpress-plugin/</guid>
		<description><![CDATA[<img src="wp-content/themes/sterling/common/img/icon_wordpress.jpg" class="excerptImg"/>After building my first few WordPress plugins I learned quite a few things that I just could not find good information on.  So what I will be doing over a couple or so posts is explaining what the problem/issue was that I had and show you how I went about resolving the problem/issue.]]></description>
			<content:encoded><![CDATA[<div class='series_toc'><h3>Table of contents for What I learned while building a WordPress Plugin</h3><ol><li>Things I&#8217;ve Learned While building a WordPress Plugin</li></ol></div> <p>After building my first few WordPress plugins I learned quite a few things that I just could not find good information on.  So what I will be doing over a couple or so posts is explaining what the problem/issue was that I had and show you how I went about resolving the problem/issue.</p>
<h4>How do I add a sub menu for my plugins admin and what is a "unique identifier"?</h4>
<p>Ok, creating a submenu is not very hard and <a href="http://codex.wordpress.org/Adding_Administration_Menus#Sub-Menus" rel="nofollow">WordPress docs</a> does an ok job of explaining it.  But when you are building your plugin using a class structure it is not very clear what you should do.</p>
<p>Lets assume that you have a class called "myClass" and a function with in it that is called "adminMenu."  Let's also assume that this class is also on the same file that is your main plugin file that has the version information on it. Your typical php4 based class would look like (php5 based classes have __construct instead of calling myClass and some other minor differences):</p>
<div class="igBar"><span id="lphp-1"><a href="#" onclick="javascript:showPlainTxt('php-1'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-1">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">class</span> myClass<span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> myClass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; add_action<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'admin_menu'</span>, <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>&amp;amp;<span style="color:#0000FF;">$this</span>, <span style="color:#FF0000;">'adminMenu'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> adminMenu<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/function_exists"><span style="color:#000066;">function_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'add_menu_page'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add_menu_page<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'My Class'</span>,<span style="color:#FF0000;">'My Class'</span>, <span style="color:#CC66CC;color:#800000;">7</span>, <span style="color:#000000; font-weight:bold;">__FILE__</span>, <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>&amp;<span style="color:#0000FF;">$this</span>, <span style="color:#FF0000;">'adminSubPage1'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/function_exists"><span style="color:#000066;">function_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'add_submenu_page'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add_submenu_page<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">__FILE__</span>, <span style="color:#FF0000;">'SubMenu 1'</span>, <span style="color:#FF0000;">'SubMenu 1'</span>, <span style="color:#CC66CC;color:#800000;">7</span>, <span style="color:#000000; font-weight:bold;">__FILE__</span>, <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>&amp;<span style="color:#0000FF;">$this</span>, <span style="color:#FF0000;">'adminSubPage1'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add_submenu_page<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">__FILE__</span>, <span style="color:#FF0000;">'SubMenu 2'</span>, <span style="color:#FF0000;">'SubMenu 2'</span>, <span style="color:#CC66CC;color:#800000;">7</span>, <span style="color:#FF0000;">'mc_adminSubPage2'</span>, <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>&amp;<span style="color:#0000FF;">$this</span>, <span style="color:#FF0000;">'adminSubPage2'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>At line three we add an action that points to the "adminMenu" function and let me clarify what I have found happens with "array(&amp;$this, 'adminMenu')".  WordPress will take "array(&amp;$this, 'adminMenu')" and make it into myClass::adminMenu() and execute it like that - the <font color="#ff0000">&amp;this</font> is a reference to <font color="#ff0000">myClass.</font></p>
<p>At line 8 we have the main link that gets put at the top next to "Plugins," "Comments" and so on.  You will notice the <font color="#ff0000">__FILE__</font> constant, this allows WordPress to set that menu item to reference the plugins main file.   You also see <font color="#ff0000">array(&amp;$this, 'adminSubPage1') </font>will execute as <font color="#ff0000">myClass::adminSubPage1()</font> (this function is not shown).</p>
<p>At line 11 we create our first submenu for our plugin which also points to the  <font color="#ff0000">adminSubPage1</font> function, which is needed I found.  If you don't have a submenu item that points to the same "page" as the main menu item, the submenu items will not show up.</p>
<p>At line 12 we create our second submenu item that points to the  <font color="#ff0000">adminSubPage2</font> function (also not shown).    You will notice a three differences between the two submenus, two being the Names and the function they point to, and the third is the <font color="#ff0000">mc_adminSubPage2 </font>which is our <font color="#ff0000">unique identifier</font>.  This will give us a url the looks something like <font color="#ff0000">admin.php?page=mc_adminSubPage2</font>.</p>
<p>Nothing real fancy with this one, just wanted show how to create your plugin menu items while using a Object Oriented approach.  Feel free to let me know if there is an easier way or if I am all around incorrect.  Next post will be about loading of jQuery and your other JavaScript without messing up the Prototype code that WordPress and other WP plugins use.</p>
 <div class='series_links'> </div>]]></content:encoded>
			<wfw:commentRss>http://benjaminsterling.com/things-ive-learned-while-building-a-wordpress-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using JSON in PHP4 and PHP5 with an included class or json_decode</title>
		<link>http://benjaminsterling.com/using-json-in-php4-and-php5-with-an-included-class-or-json_decode/</link>
		<comments>http://benjaminsterling.com/using-json-in-php4-and-php5-with-an-included-class-or-json_decode/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 03:38:42 +0000</pubDate>
		<dc:creator>Benjamin Sterling</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Plugin]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[json_decode]]></category>
		<category><![CDATA[php4]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://benjaminsterling.com/2007/11/18/using-json-in-php4-and-php5-with-an-included-class-or-json_decode/</guid>
		<description><![CDATA[When building my first real plugin for WordPress, I wanted to utilize some Ajax approaches that would use JSON and which would make for a speedy process, but quickly found that 1) PHP4 does not support the "json_decode" function, 2) the classes that json.org recommended were too hard to put into play, and 3) once [...]]]></description>
			<content:encoded><![CDATA[<p>When building my first real plugin for WordPress, I wanted to utilize some Ajax approaches that would use <a href="http://json.org/" title="JSON at json.org" target="_blank">JSON</a> and which would make for a speedy process, but quickly found that 1) PHP4 does not support the "<a href="http://php.net/json_decode" title="json_decode at php.net" target="_blank">json_decode</a>" function, 2) the classes that json.org recommended were too hard to put into play, and 3) once you got the proper JSON communications going back and forth between the server and the webpage, each platform handle/expect JSON in a slightly different way.</p>
<div style="float:right;padding:10px;background:#fff;border:1px solid #ccc;margin-left:1em;">
<script type="text/javascript">var dzone_url = "http://benjaminsterling.com/using-json-in-php4-and-php5-with-an-included-class-or-json_decode/";</script><br />
<script type="text/javascript">var dzone_title = "Using JSON in PHP4 and PHP5 with an included class or json_decode";</script><br />
<script type="text/javascript">var dzone_blurb = "A good post on how to play with JSON in PHP4 and PHP5 while using json_decode and jQuery";</script><br />
<script type="text/javascript">var dzone_style = "1";</script><br />
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script>
</div>
<p>Let's talk about my first two points, PHP4 does not support the json_decode function and the resources the json.org points you too are not all that great.  I actually wasted a good four or so hours trying to first figure out which one was best for my situation and once I did that, trying to get the on to work with out hacking and adding a bunch of crap.  After doing some research and searching I came across phpclasses.org, which is a great resource for finding anything you need for PHP, but the site is so weighed down with advertising, what should take you five minutes to do, takes you fifteen minutes because you are waiting for the site to load, dodging the multiple pop-ups (yes, even with a pop-up blocker).  With that said, it is a great resource and I came across a <a href="http//cesars.users.phpclasses.org/json" title="JSON class at phpclasses.org" target="_blank">JSON class</a> that returns that same empty class or "stdClass."  I may be wrong in my interpretation of what an "stdClass" is, so don't quote me on it.</p>
<p>So with with that class in hand, my problem with getting my plugin to work in both PHP4 and PHP5 what solved and all the code I need to add to my app was:</p>
<div class="igBar"><span id="lphp-14"><a href="#" onclick="javascript:showPlainTxt('php-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-14">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$params</span> = <span style="color:#FF0000;">'{&quot;key&quot;:&quot;value&quot;}'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>!<a href="http://www.php.net/extension_loaded"><span style="color:#000066;">extension_loaded</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'json'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">include_once</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'common/inc/JSON.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$json</span> = <span style="color:#000000; font-weight:bold;">new</span> JSON;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$objs</span> = <span style="color:#0000FF;">$json</span>-&gt;<span style="color:#006600;">unserialize</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$params</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">else</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$objs</span> = json_decode<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$params</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The if (!extension_loaded('json')){ line checks to see if the JSON extensions are loaded and if not, it will include the JSON.php that contains the JSON class we need.  And once that code was ran, it was as simple as doing the following to get the correct content:</p>
<div class="igBar"><span id="lphp-15"><a href="#" onclick="javascript:showPlainTxt('php-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-15">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$objs</span>-&gt;<span style="color:#006600;">key</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// returns value </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>There are a few notes first, on php.net, the note that <em>"This function will return false if the JSON encoded data is deeper than 127 elements."</em>  And both functions will return false if the json string is not in proper form, check out <a href="http://json.org">json.org</a> for in depth information about proper form, but I will go over some that I ran into right now.</p>
<p><!--adsense--></p>
<p>If you do something like below:</p>
<div class="igBar"><span id="lphp-16"><a href="#" onclick="javascript:showPlainTxt('php-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-16">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$jsonString</span> = <span style="color:#FF0000;">'{&quot;a&quot;:true,&quot;b&quot;:2,&quot;c&quot;:3,&quot;d&quot;:4,&quot;e&quot;:5}'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$json_decodeString</span> = json_decode<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$jsonString</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">'&lt;pre&gt;'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/print_r"><span style="color:#000066;">print_r</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$json_decodeString</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">'&lt;/pre&gt;'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">include_once</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'JSON.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$json</span> = <span style="color:#000000; font-weight:bold;">new</span> JSON;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$jsonClassString</span> = <span style="color:#0000FF;">$json</span>-&gt;<span style="color:#006600;">unserialize</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$jsonString</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">'&lt;pre&gt;'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/print_r"><span style="color:#000066;">print_r</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$jsonClassString</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">'&lt;/pre&gt;'</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>You will get:</p>
<div class="igBar"><span id="lhtml-17"><a href="#" onclick="javascript:showPlainTxt('html-17'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-17">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">stdClass Object</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">(</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; [a] =&gt; 1</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; [b] =&gt; 2</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; [c] =&gt; 3</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; [d] =&gt; 4</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; [e] =&gt; 5</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">)</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">stdClass Object</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">(</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; [a] =&gt; 1</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; [b] =&gt; 2</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; [c] =&gt; 3</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; [d] =&gt; 4</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; [e] =&gt; 5</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">) </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Notice that each key is wrapped with parentheses and since we are dealing with with numbers and boolean in the values, we don't need to wrap them.  Also notice that the <strong>true</strong> value returned 1, remember in the computer world, true == 1 and false == 0.</p>
<p>All fairly simple right?  No suppose we are pulling the above json string into the browser via a Ajax call, in the following example we have a jQuery Ajax call that is running a runTest function and passing the data returned by the json/Ajax call:</p>
<div class="igBar"><span id="ljavascript-18"><a href="#" onclick="javascript:showPlainTxt('javascript-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-18">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; $.<span style="color: #006600;">ajax</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url : <span style="color: #3366CC;">'dump.php'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType:<span style="color: #3366CC;">'json'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data :<span style="color: #3366CC;">'go=test1'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success : <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; runTest<span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; runTest = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>data.<span style="color: #006600;">a</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// run more code</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>If we take the string below and pass that thru to the runTest function, the "run more code" section would get executed:</p>
<div class="igBar"><span id="lphp-19"><a href="#" onclick="javascript:showPlainTxt('php-19'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-19">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$jsonString</span> = <span style="color:#FF0000;">'{&quot;a&quot;:true,&quot;b&quot;:2,&quot;c&quot;:3,&quot;d&quot;:4,&quot;e&quot;:5}'</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now, if we take the same string and returned the follow instead, the "run more code" section will still get executed:</p>
<div class="igBar"><span id="lphp-20"><a href="#" onclick="javascript:showPlainTxt('php-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-20">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">"a"</span>:<span style="color:#FF0000;">"false"</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>You would think that if you change the <strong>if</strong> statement to the following that it would not get executed:</p>
<div class="igBar"><span id="ljavascript-21"><a href="#" onclick="javascript:showPlainTxt('javascript-21'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-21">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>data.<span style="color: #006600;">a</span> == <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This is not true because we are passing back a <strong>String</strong> and not a <strong>Boolean</strong> and the reason if(data.a){ works with a non-empty string is because an empty string is considered <strong>NULL</strong> which is in turn considered <strong>false</strong>.</p>
<p>With all that background out of the way, lets take some of my real world examples when dealing with getting back the correct format on the Javascript side.</p>
<p><!--adsense--></p>
<p>From the database, I have all the parameters for a jQuery plugin that needs to be echoed out, in this particulare situation, I am not using Ajax, I am simple build our the plugin's needs and echoing them out to the browser.  The format needs looks something like:</p>
<div class="igBar"><span id="ljavascript-22"><a href="#" onclick="javascript:showPlainTxt('javascript-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-22">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span>fx:<span style="color: #3366CC;">"scrollDown"</span>,timeout:<span style="color: #CC0000;color:#800000;">1000</span>,height:<span style="color: #CC0000;color:#800000;">97</span>,random:<span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>but the sever would print out something like:</p>
<div class="igBar"><span id="ljavascript-23"><a href="#" onclick="javascript:showPlainTxt('javascript-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-23">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">"fx"</span>:<span style="color: #3366CC;">"scrollDown"</span>,<span style="color: #3366CC;">"timeout"</span>:<span style="color: #CC0000;color:#800000;">1000</span>,<span style="color: #3366CC;">"height"</span>:<span style="color: #CC0000;color:#800000;">97</span>,<span style="color: #3366CC;">"random"</span>:<span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>So, I did I get the correct format?  I did the following:</p>
<div class="igBar"><span id="lphp-24"><a href="#" onclick="javascript:showPlainTxt('php-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-24">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>!<a href="http://www.php.net/extension_loaded"><span style="color:#000066;">extension_loaded</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'json'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">include_once</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'common/inc/JSON.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$json</span> = <span style="color:#000000; font-weight:bold;">new</span> JSON;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$objs</span> = <span style="color:#0000FF;">$json</span>-&gt;<span style="color:#006600;">unserialize</span><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/stripslashes"><span style="color:#000066;">stripslashes</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$params</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">else</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$objs</span> = json_decode<span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/stripslashes"><span style="color:#000066;">stripslashes</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$params</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$tmp</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$objs</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$objs</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$obj</span> =&gt; <span style="color:#0000FF;">$k</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$k</span> = <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/is_numeric"><span style="color:#000066;">is_numeric</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$k</span><span style="color:#006600; font-weight:bold;">&#41;</span> || <a href="http://www.php.net/is_bool"><span style="color:#000066;">is_bool</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$k</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#0000FF;">$k</span> : <span style="color:#FF0000;">'&quot;'</span>.<span style="color:#0000FF;">$k</span>.<span style="color:#FF0000;">'&quot;'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array_push"><span style="color:#000066;">array_push</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$tmp</span>, <span style="color:#0000FF;">$obj</span> . <span style="color:#FF0000;">':'</span> . <span style="color:#0000FF;">$k</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">'</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">&lt;script type=&quot;text/javascript&quot;&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">&nbsp; &nbsp; $(document).ready(function(){</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">&nbsp; &nbsp; &nbsp; &nbsp; $(&quot;ul#theGallery&quot;).'</span>.<span style="color:#0000FF;">$r</span>.<span style="color:#FF0000;">'({'</span>.<a href="http://www.php.net/join"><span style="color:#000066;">join</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">','</span>,<span style="color:#0000FF;">$tmp</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#FF0000;">'});</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">&nbsp; &nbsp; });</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">&lt;/script&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">'</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Let me try to explain what the above is doing, I already explained what line 1 trhu 8 are doing up above.  Line 11 we check to see if $objs is not <strong>false</strong> or <strong>NULL</strong>.  At line 12 we start our foreach loop and line 13 is where we check to see if the value is numeric or a boolean and if the value is either, return the value as is, but if it isn't, wrap it in parentheses.  Line 14 we are pushing it into an array much like the Javascript method myArray.push('text').  Once we are done building the array and cycling through the objects, in this situation, we echo out the array while joining the array with the <strong>Join</strong> method just like the Javascript method myArray.join(",").</p>
<p><!--adsense--></p>
<p>This approach can be used for sending back a json formated string to Javascript:</p>
<div class="igBar"><span id="lphp-25"><a href="#" onclick="javascript:showPlainTxt('php-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-25">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$query</span>&nbsp; = <span style="color:#FF0000;">"SELECT name, subject, message FROM contact"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$query</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$tmp</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$row</span> = <a href="http://www.php.net/mysql_fetch_array"><span style="color:#000066;">mysql_fetch_array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$result</span>, MYSQL_ASSOC<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/array_push"><span style="color:#000066;">array_push</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$tmp</span>, <span style="color:#FF0000;">'&quot;name&quot;:'</span>.<span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/is_numeric"><span style="color:#000066;">is_numeric</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'name'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> || <a href="http://www.php.net/is_bool"><span style="color:#000066;">is_bool</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'name'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'name'</span><span style="color:#006600; font-weight:bold;">&#93;</span> : <span style="color:#FF0000;">'&quot;'</span>.<span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'name'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#FF0000;">'&quot;'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/array_push"><span style="color:#000066;">array_push</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$tmp</span>, <span style="color:#FF0000;">'&quot;name&quot;:'</span>.<span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/is_numeric"><span style="color:#000066;">is_numeric</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'subject'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> || <a href="http://www.php.net/is_bool"><span style="color:#000066;">is_bool</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'subject'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'subject'</span><span style="color:#006600; font-weight:bold;">&#93;</span> : <span style="color:#FF0000;">'&quot;'</span>.<span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'subject'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#FF0000;">'&quot;'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/array_push"><span style="color:#000066;">array_push</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$tmp</span>, <span style="color:#FF0000;">'&quot;name&quot;:'</span>.<span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/is_numeric"><span style="color:#000066;">is_numeric</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'message'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> || <a href="http://www.php.net/is_bool"><span style="color:#000066;">is_bool</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'message'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'message'</span><span style="color:#006600; font-weight:bold;">&#93;</span> : <span style="color:#FF0000;">'&quot;'</span>.<span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'message'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#FF0000;">'&quot;'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"{"</span>.<a href="http://www.php.net/join"><span style="color:#000066;">join</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">','</span>,<span style="color:#0000FF;">$tmp</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#FF0000;">"}"</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This particular subject can be expanded much more, but I would suggest that you experiment and see what works and what does not and ask questions, lots of them.  Feel free to post questions and comments, if I made any mistakes with my assumptions, call me on them.  It is the only way we all learn.</p>
<p>Thanks again.</p>
<p><!--adsense#between_2--></p>
]]></content:encoded>
			<wfw:commentRss>http://benjaminsterling.com/using-json-in-php4-and-php5-with-an-included-class-or-json_decode/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
