<?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>Joseph Scott &#187; php</title>
	<atom:link href="http://josephscott.org/archives/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://josephscott.org</link>
	<description></description>
	<lastBuildDate>Thu, 29 Jul 2010 16:56:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
	<atom:link rel='hub' href='http://josephscott.org/?pushpress=hub'/>
<cloud domain='josephscott.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Finding PHP Short Tags</title>
		<link>http://josephscott.org/archives/2010/06/finding-php-short-tags/</link>
		<comments>http://josephscott.org/archives/2010/06/finding-php-short-tags/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 16:31:19 +0000</pubDate>
		<dc:creator>Joseph Scott</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[grep]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://josephscott.org/?p=2458</guid>
		<description><![CDATA[There are a number of ways to escape between PHP and output (usually HTML) mode. Personally I recommend sticking with the traditional &#60;?php style. One method that can cause problems is the so called &#8216;short tag&#8217; style &#8211; &#60;? and I recommend avoiding it. So how do you know if there are PHP short tags [...]


Related posts:<ol><li><a href='http://josephscott.org/archives/2010/02/php-helpers-html_print_r/' rel='bookmark' title='Permanent Link: PHP Helpers: html_print_r'>PHP Helpers: html_print_r</a></li>
<li><a href='http://josephscott.org/archives/2008/12/problems-with-libxml2-for-wordpress-xml-rpc-users/' rel='bookmark' title='Permanent Link: Problems With libxml2 For WordPress XML-RPC Users'>Problems With libxml2 For WordPress XML-RPC Users</a></li>
<li><a href='http://josephscott.org/archives/2008/11/makeitlink-detecting-urls-in-text-and-making-them-links/' rel='bookmark' title='Permanent Link: MakeItLink &#8211; Detecting URLs In Text And Making Them Links'>MakeItLink &#8211; Detecting URLs In Text And Making Them Links</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>There are a number of ways to <a href="http://www.php.net/manual/en/language.basic-syntax.phpmode.php">escape between PHP and output (usually HTML) mode</a>.  Personally I recommend sticking with the traditional <code>&lt;?php</code> style.  One method that can cause problems is the so called &#8216;short tag&#8217; style &#8211; <code>&lt;?</code> and I recommend avoiding it.</p>
<p>So how do you know if there are PHP short tags being used some where in your code?  I use grep to search for them: <code>grep -rn "&lt;?[^p]" *</code></p>


<p>Related posts:<ol><li><a href='http://josephscott.org/archives/2010/02/php-helpers-html_print_r/' rel='bookmark' title='Permanent Link: PHP Helpers: html_print_r'>PHP Helpers: html_print_r</a></li>
<li><a href='http://josephscott.org/archives/2008/12/problems-with-libxml2-for-wordpress-xml-rpc-users/' rel='bookmark' title='Permanent Link: Problems With libxml2 For WordPress XML-RPC Users'>Problems With libxml2 For WordPress XML-RPC Users</a></li>
<li><a href='http://josephscott.org/archives/2008/11/makeitlink-detecting-urls-in-text-and-making-them-links/' rel='bookmark' title='Permanent Link: MakeItLink &#8211; Detecting URLs In Text And Making Them Links'>MakeItLink &#8211; Detecting URLs In Text And Making Them Links</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://josephscott.org/archives/2010/06/finding-php-short-tags/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>WordCamp SF 2010 Presentation Video</title>
		<link>http://josephscott.org/archives/2010/06/wordcamp-sf-2010-presentation-video/</link>
		<comments>http://josephscott.org/archives/2010/06/wordcamp-sf-2010-presentation-video/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 23:53:00 +0000</pubDate>
		<dc:creator>Joseph Scott</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[themes]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[wordcamp]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://josephscott.org/?p=2410</guid>
		<description><![CDATA[From WordPress.TV. I&#8217;ll never get used to watching myself on video. Related posts:WordCamp Utah 2010 WordCamp San Francisco &#8211; 2010 WordCamp Utah 2010 Update &#8211; Genius Bar


Related posts:<ol><li><a href='http://josephscott.org/archives/2010/06/wordcamp-utah-2010/' rel='bookmark' title='Permanent Link: WordCamp Utah 2010'>WordCamp Utah 2010</a></li>
<li><a href='http://josephscott.org/archives/2010/04/wordcamp-san-francisco-2010/' rel='bookmark' title='Permanent Link: WordCamp San Francisco &#8211; 2010'>WordCamp San Francisco &#8211; 2010</a></li>
<li><a href='http://josephscott.org/archives/2010/07/wordcamp-utah-2010-update-genius-bar/' rel='bookmark' title='Permanent Link: WordCamp Utah 2010 Update &#8211; Genius Bar'>WordCamp Utah 2010 Update &#8211; Genius Bar</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><embed src="http://v.wordpress.com/wp-content/plugins/video/flvplayer.swf?ver=1.21" type="application/x-shockwave-flash" width="400" height="224" wmode="transparent" seamlesstabbing="true" allowfullscreen="true" allowscriptaccess="always" overstretch="true" flashvars="guid=Tbz6KTre&amp;width=400&amp;height=224&amp;locksize=no&amp;dynamicseek=false&amp;qc_publisherId=p-18-mFEk4J448M" title="Joseph Scott - Writing Secure Plugins"></embed></p>
<p>From <a href="http://wordpress.tv/2010/05/01/joseph-scott-secure-plugins-sf10/">WordPress.TV</a>.</p>
<p>I&#8217;ll never get used to watching myself on video.</p>


<p>Related posts:<ol><li><a href='http://josephscott.org/archives/2010/06/wordcamp-utah-2010/' rel='bookmark' title='Permanent Link: WordCamp Utah 2010'>WordCamp Utah 2010</a></li>
<li><a href='http://josephscott.org/archives/2010/04/wordcamp-san-francisco-2010/' rel='bookmark' title='Permanent Link: WordCamp San Francisco &#8211; 2010'>WordCamp San Francisco &#8211; 2010</a></li>
<li><a href='http://josephscott.org/archives/2010/07/wordcamp-utah-2010-update-genius-bar/' rel='bookmark' title='Permanent Link: WordCamp Utah 2010 Update &#8211; Genius Bar'>WordCamp Utah 2010 Update &#8211; Genius Bar</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://josephscott.org/archives/2010/06/wordcamp-sf-2010-presentation-video/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Slides: Anatomy of a PHP Request</title>
		<link>http://josephscott.org/archives/2010/04/slides-anatomy-of-a-php-request/</link>
		<comments>http://josephscott.org/archives/2010/04/slides-anatomy-of-a-php-request/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 15:15:58 +0000</pubDate>
		<dc:creator>Joseph Scott</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[slides]]></category>
		<category><![CDATA[uphpu]]></category>

		<guid isPermaLink="false">http://josephscott.org/?p=2197</guid>
		<description><![CDATA[Here are the slides from my presentation last week at UPHPU - Anatomy of a PHP Request View more presentations from josephscott. I added it to my slides page. Related posts:Slides Utah PHP Users Group Presentation: Anatomy of a PHP Request Presenting At Utah PHP Users (UPHPU) February Meeting


Related posts:<ol><li><a href='http://josephscott.org/slides/' rel='bookmark' title='Permanent Link: Slides'>Slides</a></li>
<li><a href='http://josephscott.org/archives/2010/04/utah-php-users-group-presentation-anatomy-of-a-php-request/' rel='bookmark' title='Permanent Link: Utah PHP Users Group Presentation: Anatomy of a PHP Request'>Utah PHP Users Group Presentation: Anatomy of a PHP Request</a></li>
<li><a href='http://josephscott.org/archives/2009/02/presenting-at-utah-php-users-uphpu-february-meeting/' rel='bookmark' title='Permanent Link: Presenting At Utah PHP Users (UPHPU) February Meeting'>Presenting At Utah PHP Users (UPHPU) February Meeting</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Here are the slides from my presentation last week at UPHPU -</p>
<div style="width:425px" id="__ss_3768942"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/josephscott/anatomy-of-a-php-request" title="Anatomy of a PHP Request">Anatomy of a PHP Request</a></strong><object width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=anatomy-php-request-100418155923-phpapp01&#038;stripped_title=anatomy-of-a-php-request" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=anatomy-php-request-100418155923-phpapp01&#038;stripped_title=anatomy-of-a-php-request" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="padding:5px 0 12px">View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/josephscott">josephscott</a>.</div>
</div>
<p>I added it to my <a href="http://josephscott.org/slides/">slides page</a>.</p>


<p>Related posts:<ol><li><a href='http://josephscott.org/slides/' rel='bookmark' title='Permanent Link: Slides'>Slides</a></li>
<li><a href='http://josephscott.org/archives/2010/04/utah-php-users-group-presentation-anatomy-of-a-php-request/' rel='bookmark' title='Permanent Link: Utah PHP Users Group Presentation: Anatomy of a PHP Request'>Utah PHP Users Group Presentation: Anatomy of a PHP Request</a></li>
<li><a href='http://josephscott.org/archives/2009/02/presenting-at-utah-php-users-uphpu-february-meeting/' rel='bookmark' title='Permanent Link: Presenting At Utah PHP Users (UPHPU) February Meeting'>Presenting At Utah PHP Users (UPHPU) February Meeting</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://josephscott.org/archives/2010/04/slides-anatomy-of-a-php-request/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utah PHP Users Group Presentation: Anatomy of a PHP Request</title>
		<link>http://josephscott.org/archives/2010/04/utah-php-users-group-presentation-anatomy-of-a-php-request/</link>
		<comments>http://josephscott.org/archives/2010/04/utah-php-users-group-presentation-anatomy-of-a-php-request/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 15:28:45 +0000</pubDate>
		<dc:creator>Joseph Scott</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[uphpu]]></category>

		<guid isPermaLink="false">http://josephscott.org/?p=2169</guid>
		<description><![CDATA[I&#8217;ll be presenting at the next Utah PHP Users Group (UPHPU) meeting on Anatomy of a PHP Request: Ever wondered what really happens when your PHP script runs? I’ll cover the major milestones in the life and times of a PHP request (read, parse, compile, execute, output ) and where to look for road bumps [...]


Related posts:<ol><li><a href='http://josephscott.org/archives/2009/02/presenting-at-utah-php-users-uphpu-february-meeting/' rel='bookmark' title='Permanent Link: Presenting At Utah PHP Users (UPHPU) February Meeting'>Presenting At Utah PHP Users (UPHPU) February Meeting</a></li>
<li><a href='http://josephscott.org/archives/2010/04/slides-anatomy-of-a-php-request/' rel='bookmark' title='Permanent Link: Slides: Anatomy of a PHP Request'>Slides: Anatomy of a PHP Request</a></li>
<li><a href='http://josephscott.org/archives/2010/04/firebug-feature-request/' rel='bookmark' title='Permanent Link: Firebug Feature Request'>Firebug Feature Request</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll be presenting at the next <a href="http://uphpu.org/">Utah PHP Users Group (UPHPU)</a> meeting on <a href="http://uphpu.org/2010/04/08/uphpu-april-meeting-7pm-april-15-anatomy-of-a-php-request-joseph-scott-php-fundamentals-with-justin-carmony/#comments">Anatomy of a PHP Request</a>:</p>
<blockquote><p>
Ever wondered what really happens when your PHP script runs? I’ll cover the major milestones in the life and times of a PHP request (read, parse, compile, execute, output ) and where to look for road bumps along the way. There are lots of tools available to make your PHP purr like a kitten, so I’ll dive into op code caches (with APC), op code dumps (with VLD – Vulcan Logic Dumper), and profiling (with Xdebug and webgrind). It will be a great time digging a little deeper in to PHP!
</p></blockquote>
<p>The meeting is at:</p>
<p>April 15, 2010 @7pm</p>
<p>Bill Good Marketing<br />
12393 Gateway Park Place<br />
Suite 600<br />
Draper, UT 84020</p>
<p><a href="http://maps.google.com/maps?f=q&#038;source=s_q&#038;hl=en&#038;geocode=&#038;q=12393+Gateway+Park+Place+Suite+600+Draper,+UT+84020&#038;sll=40.525201,-111.901299&#038;sspn=0.00681,0.010772&#038;ie=UTF8&#038;hq=&#038;hnear=12393+Gateway+Park+Pl+%23600,+Draper,+Salt+Lake,+Utah+84020&#038;ll=40.525185,-111.902994&#038;spn=0.007307,0.010772&#038;t=k&#038;z=17&#038;iwloc=A">Google Map</a></p>


<p>Related posts:<ol><li><a href='http://josephscott.org/archives/2009/02/presenting-at-utah-php-users-uphpu-february-meeting/' rel='bookmark' title='Permanent Link: Presenting At Utah PHP Users (UPHPU) February Meeting'>Presenting At Utah PHP Users (UPHPU) February Meeting</a></li>
<li><a href='http://josephscott.org/archives/2010/04/slides-anatomy-of-a-php-request/' rel='bookmark' title='Permanent Link: Slides: Anatomy of a PHP Request'>Slides: Anatomy of a PHP Request</a></li>
<li><a href='http://josephscott.org/archives/2010/04/firebug-feature-request/' rel='bookmark' title='Permanent Link: Firebug Feature Request'>Firebug Feature Request</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://josephscott.org/archives/2010/04/utah-php-users-group-presentation-anatomy-of-a-php-request/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Efficient PHP: Don&#8217;t Abuse dirname( __FILE__ )</title>
		<link>http://josephscott.org/archives/2010/04/efficient-php-dont-abuse-dirname-__file__/</link>
		<comments>http://josephscott.org/archives/2010/04/efficient-php-dont-abuse-dirname-__file__/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 15:26:39 +0000</pubDate>
		<dc:creator>Joseph Scott</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[dirname]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://josephscott.org/?p=2147</guid>
		<description><![CDATA[Every now and then I run across a chunk of PHP code at the top of a file that looks something like this: require dirname( __FILE__ ) . '/path/to/something.php'; require dirname( __FILE__ ) . '/path/to/another.php'; require dirname( __FILE__ ) . '/path/to/me-too.php'; require dirname( __FILE__ ) . '/path/to/sure-why-not.php'; require dirname( __FILE__ ) . '/path/to/kitchen-sink.php'; and what [...]


Related posts:<ol><li><a href='http://josephscott.org/archives/2010/01/php-count-performance/' rel='bookmark' title='Permanent Link: PHP Count Performance'>PHP Count Performance</a></li>
<li><a href='http://josephscott.org/archives/2010/03/database-powered-css-in-wordpress-themes/' rel='bookmark' title='Permanent Link: Database Powered CSS in WordPress Themes'>Database Powered CSS in WordPress Themes</a></li>
<li><a href='http://josephscott.org/code/php/makeitlink/' rel='bookmark' title='Permanent Link: MakeItLink'>MakeItLink</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Every now and then I run across a chunk of PHP code at the top of a file that looks something like this:</p>
<pre class="brush: php;">
require dirname( __FILE__ ) . '/path/to/something.php';
require dirname( __FILE__ ) . '/path/to/another.php';
require dirname( __FILE__ ) . '/path/to/me-too.php';
require dirname( __FILE__ ) . '/path/to/sure-why-not.php';
require dirname( __FILE__ ) . '/path/to/kitchen-sink.php';
</pre>
<p>and what jumps out at me is the repeated use of <code>dirname( __FILE__ )</code> for each require statement (for now we&#8217;ll avoid asking why anyone would need to include the kitchen-sink in their code base).  My gut instinct is to call <code>dirname( __FILE__ )</code> once, save that in a variable and then reference the variable to build the path.  Not wanting to go on instinct alone I put together a small test to see if it really would make any difference.</p>
<p>The contrived test code will compare approach A:</p>
<pre class="brush: php;">
$var = dirname( __FILE__ ) . '/path/to/something.php';
$var = dirname( __FILE__ ) . '/path/to/another.php';
$var = dirname( __FILE__ ) . '/path/to/me-too.php';
$var = dirname( __FILE__ ) . '/path/to/sure-why-not.php';
$var = dirname( __FILE__ ) . '/path/to/kitchen-sink.php';
</pre>
<p>with approach B:</p>
<pre class="brush: php;">
$path = dirname( __FILE__ );

$var = $path . '/path/to/something.php';
$var = $path . '/path/to/another.php';
$var = $path . '/path/to/me-too.php';
$var = $path . '/path/to/sure-why-not.php';
$var = $path . '/path/to/kitchen-sink.php';
</pre>
<p>I&#8217;m not testing with <code>require</code> in an effort to focus just on the difference repeated <code>dirname( __FILE__ )</code> calls make, not how fast the filesystem can slurp in PHP libraries.</p>
<h3>VLD</h3>
<p>My first test was to pass each approach through <a href="http://derickrethans.nl/projects.html#vld">VLD</a> to see how much &#8220;work&#8221; PHP was doing.    For that I pulled out the number of operations needed for each approach:</p>
<p><b>approach A:</b> 37 ops<br />
<b>approach B:</b> 23 ops</p>
<p>Calling <code>dirname( __FILE__ )</code> once required 37% fewer operations.  This is a bit of blunt measurement since it doesn&#8217;t attempt to give individual weights to the different operations, but it gives a good general view.  The rule of thumb is that fewer ops is better than more ops.</p>
<h3>Time</h3>
<p>I tried running each approach in a simple loop, but it always ran so quickly that I didn&#8217;t see any useful difference.</p>
<h4>Memory</h4>
<p>Next up was a look at <a href="http://us.php.net/manual/en/function.memory-get-peak-usage.php">memory_get_peak_usage</a>.  Turns out there was a small difference:</p>
<p><b>approach A:</b> 57,312 bytes<br />
<b>approach B:</b> 56,992 bytes</p>
<p>Sure 320 bytes isn&#8217;t a big deal in the world of servers with 16GB of memory, but it&#8217;s one more reason why approach B is just that tiny bit better.</p>
<h4>Conclusion</h4>
<p>If you see this pattern creeping into your code base and you can easily convert it then you&#8217;ll likely be better off for it.  Remember that everything is a trade off though, if it takes you several hours to go through and make this kind of change it may not be worth it.</p>
<p>I&#8217;d certainly file this away for new projects though so that you can avoid repetitive <code>dirname( __FILE__ )</code> calls from the start.  For that matter, if you can get away with running on 5.3 or higher then you&#8217;d probably want to skip this entirely and look at using the new <a href="http://us.php.net/manual/en/language.constants.predefined.php">__DIR__</a> constant.  I haven&#8217;t tested how it compares to the two approaches listed above, but I&#8217;d expect it to be at least as good as approach B, perhaps even better.</p>
<p>For reference I tested this using PHP 5.2.10-2ubuntu6.4 with Suhosin-Patch 0.9.7 (cli) (built: Jan  6 2010 22:41:56).  Your specific version of PHP may behave differently.</p>


<p>Related posts:<ol><li><a href='http://josephscott.org/archives/2010/01/php-count-performance/' rel='bookmark' title='Permanent Link: PHP Count Performance'>PHP Count Performance</a></li>
<li><a href='http://josephscott.org/archives/2010/03/database-powered-css-in-wordpress-themes/' rel='bookmark' title='Permanent Link: Database Powered CSS in WordPress Themes'>Database Powered CSS in WordPress Themes</a></li>
<li><a href='http://josephscott.org/code/php/makeitlink/' rel='bookmark' title='Permanent Link: MakeItLink'>MakeItLink</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://josephscott.org/archives/2010/04/efficient-php-dont-abuse-dirname-__file__/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>PHP Tip: Spaces Are Not empty()</title>
		<link>http://josephscott.org/archives/2010/03/php-tip-spaces-are-not-empty/</link>
		<comments>http://josephscott.org/archives/2010/03/php-tip-spaces-are-not-empty/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 18:06:14 +0000</pubDate>
		<dc:creator>Joseph Scott</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[empty]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://josephscott.org/?p=2108</guid>
		<description><![CDATA[It can be really easy to get tripped up by PHP&#8217;s empty and isset functions. I just came across a bug in some code that was using empty() to check for something so I thought this was a good time to remind others about this. First a quick refresher course on isset() and empty(). The [...]


Related posts:<ol><li><a href='http://josephscott.org/archives/2010/01/php-helpers-debug_log/' rel='bookmark' title='Permanent Link: PHP Helpers: debug_log'>PHP Helpers: debug_log</a></li>
<li><a href='http://josephscott.org/archives/2010/01/php-helpers-esc_html/' rel='bookmark' title='Permanent Link: PHP Helpers: esc_html'>PHP Helpers: esc_html</a></li>
<li><a href='http://josephscott.org/archives/2009/02/i-dont-like-phps-extract-function/' rel='bookmark' title='Permanent Link: I Don&#8217;t Like PHP&#8217;s extract() Function'>I Don&#8217;t Like PHP&#8217;s extract() Function</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>It can be really easy to get tripped up by PHP&#8217;s <a href="http://us.php.net/empty/">empty</a> and <a href="http://us.php.net/manual/en/function.isset.php">isset</a> functions.  I just came across a bug in some code that was using <code>empty()</code> to check for something so I thought this was a good time to remind others about this.</p>
<p>First a quick refresher course on <code>isset()</code> and <code>empty()</code>.  The isset function only checks to see if a specific variable was been created and has a value other than NULL.  Other than the NULL check isset doesn&#8217;t make any determinations on what value the variable holds.  The empty function on the other hand looks at the value of a variable to see if it has been set to something that could be considered &#8220;empty&#8221;.  The <a href="http://us.php.net/empty/">empty</a> doc page mentions what PHP considers &#8220;empty&#8221;:</p>
<blockquote><p>
Returns FALSE if var has a non-empty and non-zero value.</p>
<p>The following things are considered to be empty:</p>
<p>&#8220;&#8221; (an empty string)<br />
0 (0 as an integer)<br />
&#8220;0&#8243; (0 as a string)<br />
NULL<br />
FALSE<br />
array() (an empty array)<br />
var $var; (a variable declared, but without a value in a class)
</p></blockquote>
<p>So with all that in mind, what would you expect from the following code:</p>
<pre class="brush: php;">
$test_me = ' ';

if ( empty( $test_me ) ) {
    echo &quot;This is empty!\n&quot;;
} else {
    echo &quot;NOT empty\n&quot;;
}
</pre>
<p>Where $test_me is set to a string that contains a single space character.  If you said &#8216;NOT empty&#8217; was the result, you are correct.  If you said &#8216;This is empty!&#8217;, well, I can understand why you&#8217;d think that, but that turned out to be the wrong answer.</p>
<p>This is exactly what happened in the bug I saw.  An empty check was being done inside a foreach() loop to see if any work on that item in the array really needed to be done.  If empty returned TRUE then continue was called to skip work on that item and go on to the next.  Unfortunately it didn&#8217;t expect the condition where the value might just be a string full of spaces.  To people that looks empty, to PHP, not so much.</p>
<pre class="brush: php;">
foreach ( $stuff as $a_thing ) {
    if ( empty( $a_thing ) ) {
        continue;
    }

    ...
}
</pre>
<p>A good approach, until $a_thing includes our friend the string of spaces.</p>
<p>So how do you deal with this?  PHP provides an <a href="http://us.php.net/rtrim">rtrim</a> function to remove trailing spaces (and space line items) and you might be tempted to try this:</p>
<pre class="brush: php;">
if ( empty( rtrim( $test_me ) ) ) {
    ...
}
</pre>
<p>But that won&#8217;t work.  The empty function can only check variables, thankfully this is noted on the <a href="http://us.php.net/empty/">empty docs page</a>:</p>
<blockquote><p>
Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).
</p></blockquote>
<p>Not the end of the world, just means you have to write it like this:</p>
<pre class="brush: php;">
$test_me = rtrim( $test_me );
if ( empty( $test_me ) ) {
    ...
}
</pre>
<p>That will remove any trailing spaces from $test_me before the empty check, so strings that consist of just spaces (or space like characters) will truly become empty in the PHP sense.</p>
<p>If you&#8217;ve been writing PHP long enough you&#8217;ve likely run into this problem, it&#8217;s an easy one to get tripped on.  I&#8217;ve lost count the number of times I&#8217;ve been bitten by this over the years.  Perhaps writing this will save me (and hopefully others) from this mistake in the future.</p>


<p>Related posts:<ol><li><a href='http://josephscott.org/archives/2010/01/php-helpers-debug_log/' rel='bookmark' title='Permanent Link: PHP Helpers: debug_log'>PHP Helpers: debug_log</a></li>
<li><a href='http://josephscott.org/archives/2010/01/php-helpers-esc_html/' rel='bookmark' title='Permanent Link: PHP Helpers: esc_html'>PHP Helpers: esc_html</a></li>
<li><a href='http://josephscott.org/archives/2009/02/i-dont-like-phps-extract-function/' rel='bookmark' title='Permanent Link: I Don&#8217;t Like PHP&#8217;s extract() Function'>I Don&#8217;t Like PHP&#8217;s extract() Function</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://josephscott.org/archives/2010/03/php-tip-spaces-are-not-empty/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP Helpers: curl_http_request</title>
		<link>http://josephscott.org/archives/2010/03/php-helpers-curl_http_request/</link>
		<comments>http://josephscott.org/archives/2010/03/php-helpers-curl_http_request/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 22:07:18 +0000</pubDate>
		<dc:creator>Joseph Scott</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[curl_http_request]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php-helpers]]></category>

		<guid isPermaLink="false">http://josephscott.org/?p=2029</guid>
		<description><![CDATA[cURL is one very handy program and library, I love having access to in PHP. It has a ton of options though, and I can never seem to remember to flip the right knobs without reviewing the options list. 99% of the time I just want a simple function that does the right thing when [...]


Related posts:<ol><li><a href='http://josephscott.org/archives/2010/01/php-helpers-debug_log/' rel='bookmark' title='Permanent Link: PHP Helpers: debug_log'>PHP Helpers: debug_log</a></li>
<li><a href='http://josephscott.org/archives/2010/02/php-helpers-make_slug/' rel='bookmark' title='Permanent Link: PHP Helpers: make_slug'>PHP Helpers: make_slug</a></li>
<li><a href='http://josephscott.org/archives/2010/02/php-helpers-redirect_url/' rel='bookmark' title='Permanent Link: PHP Helpers: redirect_url'>PHP Helpers: redirect_url</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://curl.haxx.se/">cURL</a> is one very handy program and library, I love having access to in PHP.  It has a ton of options though, and I can never seem to remember to flip the right knobs without reviewing the options list.  99% of the time I just want a simple function that does the right thing when making an HTTP request.  So here&#8217;s the new PHP Helpers function that sets up reasonable defaults for making HTTP requests using cURL:</p>
<pre class="brush: php;">
if ( !function_exists( 'curl_http_request' ) ) {
    function curl_http_request( $url, $req = 'GET', $arg = array( ) ) {
        $body       = '';
        $cookies    = array( );
        $err_no     = 0;
        $err_msg    = '';
        $headers    = array( );
        $info       = array( );

        $opt = array(
            'connect_timeout'   =&gt; 5,
            'cookies'           =&gt; array( ),
            'follow_location'   =&gt; TRUE,
            'http_headers'      =&gt; array( ),
            'max_redirects'     =&gt; 5,
            'timeout'           =&gt; 15,
            'password'          =&gt; '',
            'post_fields'       =&gt; array( ),
            'user_agent'        =&gt; 'PHP curl_http_get',
            'username'          =&gt; '',
            'verify_ssl'        =&gt; TRUE
        );
        foreach ( $opt as $k =&gt; $v ) {
            if ( isset( $arg[$k] ) ) {
                $opt[$k] = $arg[$k];
            }
        }

        $curl_opt = array(
            CURLOPT_AUTOREFERER     =&gt; TRUE,
            CURLOPT_CONNECTTIMEOUT  =&gt; $opt['connect_timeout'],
            CURLOPT_CUSTOMREQUEST   =&gt; $req,
            CURLOPT_ENCODING        =&gt; '',
            CURLOPT_FOLLOWLOCATION  =&gt; $opt['follow_location'],
            CURLOPT_HEADER          =&gt; TRUE,
            CURLOPT_MAXREDIRS       =&gt; $opt['max_redirects'],
            CURLOPT_RETURNTRANSFER  =&gt; TRUE,
            CURLOPT_TIMEOUT         =&gt; $opt['timeout'],
            CURLOPT_USERAGENT       =&gt; $opt['user_agent'],
        );

        if (
            is_array( $opt['cookies'] )
            &amp;&amp; count( $opt['cookies'] ) &gt; 0
        ) {
            $curl_opt[CURLOPT_COOKIE] = implode( ';', $opt['cookies'] );
        }

        if ( !empty( $opt['username'] ) ) {
            $curl_opt[CURLOPT_USERPWD] = $opt['username'] . ':' . $opt['password'];
        }

        if (
            is_array( $opt['post_fields'] )
            &amp;&amp; count( $opt['post_fields'] ) &gt; 0
        ) {
            $curl_opt[CURLOPT_POST] = TRUE;
            $curl_opt[CURLOPT_POSTFIELDS] = $opt['post_fields'];
        }

        if ( $req == 'HEAD' ) {
            $curl_opt[CURLOPT_NOBODY] = TRUE;
        }

        if (
            is_array( $opt['http_headers'] )
            &amp;&amp; count( $opt['http_headers'] ) &gt; 0
        ) {
            $curl_opt[CURLOPT_HTTPHEADER] = $opt['http_headers'];
        }

        if ( $opt['verify_ssl'] === FALSE ) {
            $curl_opt[CURLOPT_SSL_VERIFYPEER] = FALSE;
            $curl_opt[CURLOPT_SSL_VERIFYHOST] = 0;
        }

        $curl = curl_init( $url );
        curl_setopt_array( $curl, $curl_opt );

        $body       = curl_exec( $curl );
        $err_no     = curl_errno( $curl );
        $err_msg    = curl_error( $curl );
        $info       = curl_getinfo( $curl );
        curl_close( $curl );

        $header_string = trim( substr( $body, 0, $info['header_size'] ) );
        $body = substr( $body, $info['header_size'] );

        if ( strpos( $header_string, &quot;\r\n\r\n&quot; ) !== FALSE ) {
            $header_string = end( explode( &quot;\r\n\r\n&quot;, $header_string ) );
            $header_string = str_replace( &quot;\r\n&quot;, &quot;\n&quot;, $header_string );
        }

        foreach ( explode( &quot;\n&quot;, $header_string ) as $line ) {
            list( $k, $v ) = explode( ':', $line, 2 );
            if ( empty( $v ) ) {
                continue;
            }

            if ( strtolower( $k ) == 'set-cookie' ) {
                $cookies[] = trim( $v );
            } else {
                $headers[$k] = trim( $v );
            }
        }

        return array(
            'body'      =&gt; $body,
            'err_no'    =&gt; $err_no,
            'err_msg'   =&gt; $err_msg,
            'headers'   =&gt; $headers,
            'cookies'   =&gt; $cookies,
            'info'      =&gt; $info
        );
    } // function curl_http_request
}
</pre>
<p>It&#8217;s a bit longer that then other PHP Helper functions :-) .</p>


<p>Related posts:<ol><li><a href='http://josephscott.org/archives/2010/01/php-helpers-debug_log/' rel='bookmark' title='Permanent Link: PHP Helpers: debug_log'>PHP Helpers: debug_log</a></li>
<li><a href='http://josephscott.org/archives/2010/02/php-helpers-make_slug/' rel='bookmark' title='Permanent Link: PHP Helpers: make_slug'>PHP Helpers: make_slug</a></li>
<li><a href='http://josephscott.org/archives/2010/02/php-helpers-redirect_url/' rel='bookmark' title='Permanent Link: PHP Helpers: redirect_url'>PHP Helpers: redirect_url</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://josephscott.org/archives/2010/03/php-helpers-curl_http_request/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Database Indexes on Domain Names</title>
		<link>http://josephscott.org/archives/2010/02/database-indexes-on-domain-names/</link>
		<comments>http://josephscott.org/archives/2010/02/database-indexes-on-domain-names/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 04:20:43 +0000</pubDate>
		<dc:creator>Joseph Scott</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://josephscott.org/?p=1935</guid>
		<description><![CDATA[As part of a separate conversation Matt suggested something I had not given much thought to before. Say you have a database column that is used to store domain names. Something as simple as `domain` varchar(255) NOT NULL. Of course you&#8217;ll want to search all of that data. Perhaps you want to lookup all of [...]


Related posts:<ol><li><a href='http://josephscott.org/archives/2010/01/php-helpers-debug_log/' rel='bookmark' title='Permanent Link: PHP Helpers: debug_log'>PHP Helpers: debug_log</a></li>
<li><a href='http://josephscott.org/archives/2010/04/the-easy-way-to-get-recent-comments-in-wordpress/' rel='bookmark' title='Permanent Link: The Easy Way To Get Recent Comments In WordPress'>The Easy Way To Get Recent Comments In WordPress</a></li>
<li><a href='http://josephscott.org/archives/2010/06/wordpress-pretty-permalinks-with-nginx/' rel='bookmark' title='Permanent Link: WordPress Pretty Permalinks with Nginx'>WordPress Pretty Permalinks with Nginx</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>As part of a separate conversation <a href="http://ma.tt/">Matt</a> suggested something I had not given much thought to before.  Say you have a database column that is used to store domain names.  Something as simple as <code>`domain` varchar(255) NOT NULL</code>.  </p>
<p>Of course you&#8217;ll want to search all of that data.  Perhaps you want to lookup all of the sub-domains for example.com.  That query might look like </p>
<pre class="brush: sql;">
SELECT * FROM table_name WHERE domain LIKE '%.example.com'
</pre>
<p>Having a wildcard ( % ) at the front of a LIKE comparison generally results in poor performance.  Depending on the database you may find that either it can&#8217;t make use of an index on that column, or it must do a full index scan in order to find the right entry.  To make better use of an index the wildcard should be at the end of the string, which we could do if we reverse the domain name ordering:</p>
<pre class="brush: sql;">
SELECT * FROM table_name WHERE domain LIKE 'com.example.%'
</pre>
<p>So that&#8217;s what we do.  This of course brings up the question of how to correctly reverse the domain name parts.  In PHP this can be done with a combination of <a href="http://us.php.net/explode/">explode()</a>, <a href="http://us.php.net/array_reverse">array_reverse()</a>, and <a href="http://us.php.net/implode">implode()</a>:</p>
<pre class="brush: php;">
$domain = 'joseph.example.com';
$reverse_domain = implode( '.', array_reverse( explode( '.', $domain ) ) );
</pre>
<p>That turns <code>joseph.example.com</code> into <code>com.example.joseph</code>.  The reversed domain name gets stored in the database and that allows us to use the more efficient database query.</p>


<p>Related posts:<ol><li><a href='http://josephscott.org/archives/2010/01/php-helpers-debug_log/' rel='bookmark' title='Permanent Link: PHP Helpers: debug_log'>PHP Helpers: debug_log</a></li>
<li><a href='http://josephscott.org/archives/2010/04/the-easy-way-to-get-recent-comments-in-wordpress/' rel='bookmark' title='Permanent Link: The Easy Way To Get Recent Comments In WordPress'>The Easy Way To Get Recent Comments In WordPress</a></li>
<li><a href='http://josephscott.org/archives/2010/06/wordpress-pretty-permalinks-with-nginx/' rel='bookmark' title='Permanent Link: WordPress Pretty Permalinks with Nginx'>WordPress Pretty Permalinks with Nginx</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://josephscott.org/archives/2010/02/database-indexes-on-domain-names/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHP Helpers: make_slug</title>
		<link>http://josephscott.org/archives/2010/02/php-helpers-make_slug/</link>
		<comments>http://josephscott.org/archives/2010/02/php-helpers-make_slug/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 00:19:16 +0000</pubDate>
		<dc:creator>Joseph Scott</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[make_slug]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php-helpers]]></category>

		<guid isPermaLink="false">http://josephscott.org/?p=1918</guid>
		<description><![CDATA[New function, make_slug - if ( !function_exists( 'make_slug' ) ) { function make_slug( $str ) { $url_str = strtolower( trim( $str ) ); $url_str = preg_replace( '/[\s_]+/', '-', $url_str ); $url_str = preg_replace( '/-{2,}/', '-', $url_str ); $url_str = preg_replace( '/[^a-z0-9-]/', '', $url_str ); return $url_str; } } This takes a string and returns a [...]


Related posts:<ol><li><a href='http://josephscott.org/archives/2010/02/php-helpers-redirect_url/' rel='bookmark' title='Permanent Link: PHP Helpers: redirect_url'>PHP Helpers: redirect_url</a></li>
<li><a href='http://josephscott.org/archives/2010/01/php-helpers-debug_log/' rel='bookmark' title='Permanent Link: PHP Helpers: debug_log'>PHP Helpers: debug_log</a></li>
<li><a href='http://josephscott.org/archives/2010/03/php-tip-spaces-are-not-empty/' rel='bookmark' title='Permanent Link: PHP Tip: Spaces Are Not empty()'>PHP Tip: Spaces Are Not empty()</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>New function, make_slug -</p>
<pre class="brush: php;">
if ( !function_exists( 'make_slug' ) ) {
    function make_slug( $str ) {
        $url_str = strtolower( trim( $str ) );

        $url_str = preg_replace( '/[\s_]+/', '-', $url_str );
        $url_str = preg_replace( '/-{2,}/', '-', $url_str );
        $url_str = preg_replace( '/[^a-z0-9-]/', '', $url_str );

        return $url_str;
    }
}
</pre>
<p>This takes a string and returns a slug that is more URL friendly.  For example &#8220;<code>Do    This_Today -- Please</code>&#8221; would become &#8220;<code>do-this-today-please</code>&#8220;.</p>
<p>The approach is pretty simple:</p>
<ul>
<li>lower case everything (line 3)</li>
<li>replace space and underscore with dash (line 5)</li>
<li>replace two or more dashes with a single dash (line 6)</li>
<li>remove any other characters that aren&#8217;t a-z, 0-9 or a dash (line 7)</li>
</ul>


<p>Related posts:<ol><li><a href='http://josephscott.org/archives/2010/02/php-helpers-redirect_url/' rel='bookmark' title='Permanent Link: PHP Helpers: redirect_url'>PHP Helpers: redirect_url</a></li>
<li><a href='http://josephscott.org/archives/2010/01/php-helpers-debug_log/' rel='bookmark' title='Permanent Link: PHP Helpers: debug_log'>PHP Helpers: debug_log</a></li>
<li><a href='http://josephscott.org/archives/2010/03/php-tip-spaces-are-not-empty/' rel='bookmark' title='Permanent Link: PHP Tip: Spaces Are Not empty()'>PHP Tip: Spaces Are Not empty()</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://josephscott.org/archives/2010/02/php-helpers-make_slug/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP Helpers: redirect_url</title>
		<link>http://josephscott.org/archives/2010/02/php-helpers-redirect_url/</link>
		<comments>http://josephscott.org/archives/2010/02/php-helpers-redirect_url/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:55:35 +0000</pubDate>
		<dc:creator>Joseph Scott</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php-helpers]]></category>
		<category><![CDATA[redirect_url]]></category>

		<guid isPermaLink="false">http://josephscott.org/?p=1898</guid>
		<description><![CDATA[New function: redirect_url if ( !function_exists( 'redirect_url' ) ) { function redirect_url( $url, $status = 302 ) { header( &#34;Location: {$url}&#34;, TRUE, $status ); exit; } } A special case wrapper around the header function to do URL redirection. I specifically added the exit call inside the function so that redirect happens immediately. That way [...]


Related posts:<ol><li><a href='http://josephscott.org/archives/2010/01/php-helpers-esc_html/' rel='bookmark' title='Permanent Link: PHP Helpers: esc_html'>PHP Helpers: esc_html</a></li>
<li><a href='http://josephscott.org/archives/2010/01/php-helpers-debug_log/' rel='bookmark' title='Permanent Link: PHP Helpers: debug_log'>PHP Helpers: debug_log</a></li>
<li><a href='http://josephscott.org/archives/2010/03/php-helpers-curl_http_request/' rel='bookmark' title='Permanent Link: PHP Helpers: curl_http_request'>PHP Helpers: curl_http_request</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>New function: redirect_url</p>
<pre class="brush: php;">
if ( !function_exists( 'redirect_url' ) ) {
    function redirect_url( $url, $status = 302 ) {
        header( &quot;Location: {$url}&quot;, TRUE, $status );
        exit;
    }
}
</pre>
<p>A special case wrapper around the <a href="http://us.php.net/manual/en/function.header.php">header</a> function to do URL redirection.  I specifically added the exit call inside the function so that redirect happens immediately.  That way I don&#8217;t have a chance to forget about it later on :-)</p>


<p>Related posts:<ol><li><a href='http://josephscott.org/archives/2010/01/php-helpers-esc_html/' rel='bookmark' title='Permanent Link: PHP Helpers: esc_html'>PHP Helpers: esc_html</a></li>
<li><a href='http://josephscott.org/archives/2010/01/php-helpers-debug_log/' rel='bookmark' title='Permanent Link: PHP Helpers: debug_log'>PHP Helpers: debug_log</a></li>
<li><a href='http://josephscott.org/archives/2010/03/php-helpers-curl_http_request/' rel='bookmark' title='Permanent Link: PHP Helpers: curl_http_request'>PHP Helpers: curl_http_request</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://josephscott.org/archives/2010/02/php-helpers-redirect_url/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>