Is conversion necessary for integer comparisons in PHP?
<p>Throughout much of the code I review, I often see things such as:</p>

<pre><code>// $myvar could be anything

if (intval($myvar) &gt; 0) {
  // do stuff
}
</code></pre>

<p>Or similarly:</p>

<pre><code>if ($myvar != null &amp;&amp; intval($myvar) &gt; 0) {
  // do stuff
}
</code></pre>

<p>In an ongoing quest to produce more elegant and less-wasteful code, I am wondering if it is necessary to call intval(), for example, when performing a numeric comparison. My understanding from reading the documentation and from various responses here is that when a numeric comparison is asked for, it checks the type of the var and then performs either an integer cast or a string conversion depending on what it finds -- or are these the same as far as PHP is concerned, operations-wise?</p>

<p>Based on that, it seems the least expensive way is to tell PHP to cast it directly as an integer, as such:</p>

<pre><code>if ((int)$myvar &gt; 0) {
  // do stuff
}
</code></pre>

<p>However, I almost never see it written that way. Am I missing anything? Does PHP automatically do this any time it seems a comparison operator?</p>

<p>I realize in 99.9% of situations it is sufficient to just perform the comparison and let PHP type juggle on its own, but I see this so often that either I am missing something or many others are!</p>
