<?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>Internet Strategy Guide &#187; fail whale</title>
	<atom:link href="http://phpprotip.com/tag/fail-whale/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpprotip.com</link>
	<description>Together we can defeat the internet</description>
	<lastBuildDate>Tue, 08 Nov 2011 06:19:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>string searching, you&#8217;re doing it wrong</title>
		<link>http://phpprotip.com/2008/12/string-searching-youre-doing-it-wrong/</link>
		<comments>http://phpprotip.com/2008/12/string-searching-youre-doing-it-wrong/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 15:01:35 +0000</pubDate>
		<dc:creator>chance</dc:creator>
				<category><![CDATA[noobtip]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web dev]]></category>
		<category><![CDATA[corrected]]></category>
		<category><![CDATA[fail hammer]]></category>
		<category><![CDATA[fail whale]]></category>
		<category><![CDATA[peer evaluation]]></category>
		<category><![CDATA[php noob]]></category>
		<category><![CDATA[php noobtip]]></category>
		<category><![CDATA[php strpos tutorial]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[string search]]></category>
		<category><![CDATA[strpos]]></category>
		<category><![CDATA[tutorial review]]></category>

		<guid isPermaLink="false">http://phpprotip.com/?p=86</guid>
		<description><![CDATA[First off, I'm not trying to be a complete jack-ass, just a helpful one. This article, by Stefan Ashwell, popped up on my news feed and I wanted to comment on it but can't remember my login for the site and after 10 minutes of waiting the password reset email has yet to arrive. The [...]]]></description>
			<content:encoded><![CDATA[<p>First off, I'm not trying to be a complete jack-ass, just a helpful one. <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy50b3RhbC1waHAuY29tL2FydGljbGUvMTgvcGVyZm9ybWluZy1zZWFyY2hlcy1vbi1zdHJpbmdzLXVzaW5nLXN0cnBvcy8=">This article</a>, by Stefan Ashwell, popped up on my news feed and I wanted to comment on it but can't remember my login for the site and after 10 minutes of waiting the password reset email has yet to arrive. The non-arrival of the password process is irritating me and shows in this article title and my tone. </p>
<p>In <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy50b3RhbC1waHAuY29tL2FydGljbGUvMTgvcGVyZm9ybWluZy1zZWFyY2hlcy1vbi1zdHJpbmdzLXVzaW5nLXN0cnBvcy8=">the article</a>, Stefan shows how <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzLnBocC5uZXQvbWFudWFsL2VuL2Z1bmN0aW9uLnN0cnBvcy5waHA=">strpos</a> or <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzLnBocC5uZXQvbWFudWFsL2VuL2Z1bmN0aW9uLnN0cmlwb3MucGhw">stripos</a> to locate a desired string or block of text.</p>
<p>The example he gives is as follows:</p>
<pre>
if ( stripos($sentence, 'string') ) {
   // yes it does
} else {
   // no it doesn't
}
</pre>
<p>The problem with this example is that it will yield a false positive. This is a common mistake made by a lot of people. If we examine the php manual entry further we see, "Returns the <strong>numeric</strong> position of the first occurrence of needle  in the haystack  string." The key word being numeric. This means if the string position is 0, then the expression <code>( stripos($sentence, 'string') )</code> will evaluate to false.<br />
<strong><br />
EDIT: Expanded explaination on why this can yield a false positive. If the string your searching for is at the beginning of the sentence, then the position returned is <code>0</code>. When php reaches the statement <code>if ( stripos($sentence, 'string') )</code> it interprets the return value (<code>0</code>) as false.<br />
</strong></p>
<p>Here is a more expanded example:</p>
<pre>

$eval=(stripos('The quick brown fox jumped over the lazy dog','The'));

echo '($eval) : ';
echo ($eval) ? "pass\n" : "fail whale\n";
echo '($eval==0) : ';
echo ($eval==0) ? "pass\n" : "fail whale\n";
echo '($eval===false)';
echo ($eval===false) ? "pass\n" : "fail whale\n";
echo 'eval is '.$eval."\n";

/**
output is:

($eval) : fail whale
($eval==0) : pass
($eval===false)fail whale
eval is 0

*/
</pre>
<p>As you can see, if you want to properly search for a string using <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzLnBocC5uZXQvbWFudWFsL2VuL2Z1bmN0aW9uLnN0cmlwb3MucGhw">stripos</a> or <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzLnBocC5uZXQvbWFudWFsL2VuL2Z1bmN0aW9uLnN0cnBvcy5waHA=">strpos</a>, you must test for the boolean value of false ($eval===false). Alternately, you can use <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzMi5waHAubmV0L21hbnVhbC9lbi9ib29rLnBjcmUucGhw">regex</a>, if you're comfortable with it. I'm of the opinion that learning some basic regex doesn't ever hurt you.</p>
<p>I want to re-iterate that I wanted to leave this whole post as a comment but that doesn't seem possible right now since that email has yet to arrive and the article requires login for commenting. </p>
 <img src="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=86" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://phpprotip.com/2008/12/string-searching-youre-doing-it-wrong/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

