<?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>amptools &#187; strpos</title>
	<atom:link href="http://www.amptools.net/tag/strpos/feed" rel="self" type="application/rss+xml" />
	<link>http://www.amptools.net</link>
	<description>Simplify your life.</description>
	<lastBuildDate>Sat, 08 May 2010 07:40:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP, why it might be harder for some programmers.</title>
		<link>http://www.amptools.net/blog/php-why-it-might-be-harder-for-some-programmers</link>
		<comments>http://www.amptools.net/blog/php-why-it-might-be-harder-for-some-programmers#comments</comments>
		<pubDate>Sun, 07 Jun 2009 09:12:07 +0000</pubDate>
		<dc:creator>Michael Herndon</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[indexOf]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[strpos]]></category>

		<guid isPermaLink="false">http://www.amptools.net/?p=52</guid>
		<description><![CDATA[disclaimer: this is not to start a flame war but for me to rant on things that have cost me time out of my life but should not have.

PHP die hard fans galore and actual reputable programmers that use PHP press forward for how great a tool it is to build websites and that it [...]]]></description>
			<content:encoded><![CDATA[<img class='gravatar' style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=899a978b0f02774a965aec11eeb0b8f4&amp;default=http://amppotls.net' alt='No Gravatar' width=48 height=48/><p>disclaimer: this is not to start a flame war but for me to rant on things that have cost me time out of my life but should not have.</p>
<p>
PHP die hard fans galore and actual reputable programmers that use PHP press forward for how great a tool it is to build websites and that it is enterprise ready.  While it is a moderately decent tool that does have it&#8217;s uses, I might argue that enterprise point one day once I get my house in order. Which is hard to do cause I lack time, because PHP and some technologies just suck my time. Vicious Cycle.
</p>
<p> Even though method names slightly vary, today, its easier to be navigate and productively use 5 or 6 programing languages through common conventions and consistencies through those languages.  Even more so with frameworks that pushes these common consistencies, it adds to that performance boost of just getting things done.  However, while working on a PHP framework to help overcome its continual IE6 like blight on the web, I constantly run into little things that just make me writhe in mental anguish. </p>
<p>
PHP has no common object types, though it looks like a pecl extension is working on this issue with <a href="http://us.php.net/manual/en/book.spl-types.php">SPL Types</a> which sadly isn&#8217;t bundled inside of PHP5.3. PHP has a ton of functions to manipulate its values, str_x functions, array_x functions, etc.  On top of that, those functions were aimed at low barrier to entry and suffer from the &#8220;I&#8217;m going to be so clever with this&#8221; design syndrom that it not only trains new programmers learning PHP to learn bad concepts. Its also hard to remember and guess functionality since quite a few of these functions deviate from common practice, thus leaving not so easy to remember caveats that makes it harder for developers to switch languages.
</p>
<p> To demonstrate this issue, I bring up the almighty &#8220;indexOf&#8221; method that is typically found on a String object/class. &#8220;IndexOf&#8221; is known to take a string or char and search a string for the index of the specified value. It returns the index position of this search or a -1 if the value is not found&#8230;. ahh but not so in PHP. Even Javascript has a string class with an indexOf method that returns a -1, but not PHP, it has a <a rel="tag" href="http://us3.php.net/function.strpos">strpos</a> function that returns false instead of -1 inside of a language that has dynamic typing. &lt;sarcasm&gt;GENIUS&lt;/sarcasm&gt;</p>
<p>This presents a problem for doing conditional statements because &#8220;false&#8221; equates to &#8220;0&#8243;  with dynamic typing. When using strpos or indexOf, any return value over -1, including 0, means that search found something.<br />
 So if you  incorrectly check for false, which can also mean 0 in php,  you can see where this would cause bugs in your software. Developers who do more than one language might easily overlook this.</p>
<h4>c#</h4>
<pre class="brush: csharp;">
    var index =  &quot;Don't objectify me, sidekick.&quot;.IndexOf(&quot;test&quot;); // -1
    if(index &gt; -1)
         Console.WriteLine(&quot;I've been objectified&quot;);
</pre>
<h4>ruby</h4>
<pre class="brush: ruby;">
    index =  &quot;Don't objectify me, sidekick.&quot;.index('test') // -1
    if !!index # testing for null well nil is better than false
      puts &quot;I've been objectified&quot;
</pre>
<h4>Javascript</h4>
<pre class="brush: jscript;">
    var index = &quot;Don't objectify me, sidekick.&quot;.indexOf('test');
    if(index &gt; -1)
        console.log(&quot;I've been objectified&quot;);
</pre>
<h4>php</h4>
<pre class="brush: php;">
    $index = strpos(&quot;Don't objectify me, sidekick.&quot;, 'test'); //false or equates to 0
    if($index !== false) // um ick totally not easy to remember.
        echo &quot; I've been objectified &quot;;
    if($index != false)
        echo &quot; The index has to be 1, if the index is 0... epoch fail &quot;;
    if(index &gt; -1) //  could run the risk of being casted as a 0, if strpos returns false
        echo &quot; oops now i'm all buggy, cause i ducktyped false into 0 &quot;;
</pre>
<p>
There are other functions that do this same kind of scenario in PHP returning an unexpected value than what other languages are doing in some consistent fashion across the board.  This kind of thing makes the barrier a little higher for programmers that do various languages, increases the room for bugs to be introduced and even increases the barrier to entry for hard core PHP programmers into other languages.  </p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amptools.net/blog/php-why-it-might-be-harder-for-some-programmers/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->