<?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>Mike Branski &#187; PHP</title>
	<atom:link href="http://mikebranski.com/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://mikebranski.com</link>
	<description>Aspiring photographer. Web developer extraordinaire. Stormtrooper in a past life.</description>
	<lastBuildDate>Tue, 01 May 2012 00:34:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Utilizing PHP&#8217;s print_r() function</title>
		<link>http://mikebranski.com/blog/utilizing-phps-print_r-function/</link>
		<comments>http://mikebranski.com/blog/utilizing-phps-print_r-function/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 15:04:51 +0000</pubDate>
		<dc:creator>Mike Branski</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://mikebranski.com/?p=145</guid>
		<description><![CDATA[This is just a quick tip, but ever since discovering it I use it constantly. A lot of developers use PHP&#8217;s print_r() function to display human-readable information about a variable, which makes it great for debugging arrays and objects (among other things) very quickly. However, you have to wrap the output in &#60;pre&#62;&#60;/pre&#62; tags otherwise [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a quick tip, but ever since discovering it I use it constantly. A lot of developers use PHP&#8217;s <a href="http://php.net/print_r"><code>print_r()</code> function</a> to display human-readable information about a variable, which makes it great for debugging arrays and objects (among other things) very quickly. However, you have to wrap the output in <code>&lt;pre&gt;&lt;/pre&gt;</code> tags otherwise it gets spit out in a jumbled mess and doesn&#8217;t display nicely.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;pre&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/pre&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<p>That works just fine, but you can shorten it up by passing true to <code>print_r()</code>&#8216;s second parameter, the <code>$return</code> flag, and mixing it in with your <code>echo</code> statements. What this does is tells <code>print_r()</code> to return the human-readable variable data instead of printing it to the screen right away.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;pre&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/pre&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Earth-shattering code breakthrough? Absolutely (not). However, it does turn three lines of code into one and makes it quicker (for me, anyway) to write quick debugging statements and move them around easier.</p>
<p>As an added bonus, you can &#8220;queue up&#8221; a bunch of <code>print_r()</code>&#8216;s by capturing their output in a variable and then echo&#8217;ing it just once.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$debug</span> <span style="color: #339933;">=</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$debug</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bar</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;pre&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$debug</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/pre&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Voila, shorter debugging statements that are easier to work with.</p>
]]></content:encoded>
			<wfw:commentRss>http://mikebranski.com/blog/utilizing-phps-print_r-function/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

