Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>Short answer</h1> <p>This will solve your problem</p> <pre><code>function make_integer($num){ if ((int)$num != $num){ $temp = $num; while ((int)$temp != $temp){ $temp *= 10; } return $temp; } return $num; } </code></pre> <h1>Long Answer</h1> <p>The problem is that <code>is_int</code> does not test if your value is an integer or not. It will test if its <em>type</em> is an integer or not. So <code>is_int($var)</code> will do the same as if you ran <code>gettype($var) == 'integer'</code>. </p> <p>Some basic tests:</p> <pre><code>is_int((int) 0.57); # returns true, because the type is an int. is_int((float) 4); # returns false, because the type is a float is_int(10 * 0.2); # returns false, because int*float will be typecasted to a float </code></pre> <p>To make your code work, and to test if a <em>value</em> is an integer, you should cast your variable to an integer and then test if it is still equal to the original variable, like this:</p> <pre><code>(int) $var == $var; # will return true if the value is int </code></pre> <p>To fix your code, substitute the <code>!is_int($num)</code> with <code>(int) $num != $num</code> and <code>!is_int($temp)</code> with <code>(int) $temp != $temp</code>, just like the code in the short answer above.</p> <p>But, I can't resist to give an alternative solution to your problem. Personally, I would opt to use recursion for this problem. Like this:</p> <pre><code>function make_integer($num) { return (int)$num == $num ? $num : make_integer($num * 10); } </code></pre> <p>Or if you dislike the ternary operator:</p> <pre><code>function make_integer($num) { if ((int)$num == $num) return $num; # We're all done! else return make_integer($num * 10); # Try to increase by 10 } </code></pre> <h1>Even longer answer</h1> <p>The type casting of php will take some time to learn. In general, PHP will allways try to type cast if it can. When you multiply an integer with a float, the result will be a float, even if it "looks" like an integer.</p> <p>So try this code and pay special attention to the output:</p> <pre><code>$var = 0.03; var_dump($var); # float(0.03) var_dump(gettype($var)); # string(6) "double" var_dump(is_int($var)); # bool(false) </code></pre> <p>Now, if you multiply by the integer 100, php will stick the float, as <code>&lt;float&gt;*&lt;int&gt;</code> multiplication allways will result in a float, regardless of the value.</p> <pre><code>$var *= 100; # (100 * 0.03) var_dump($var); # float(3) var_dump(gettype($var)); # string(6) "double" var_dump(is_int($var)); # bool(false) </code></pre> <p>Note that the value is a natural number, but the type is still a float, and thus is_int will not return true.</p> <p>To test if a actual <em>value</em> of a variable is indeed an integer, we will need to do our own little trick with manual typecasting.</p> <pre><code>$var = 2.33; var_dump($var); # float(2.33) $var = (int) $var; var_dump($var); # int(2) </code></pre> <p>Note that when we tried to cast a float to an int the value changed. But if we try to cast a float that <em>is</em> an integer to an int, the value remains unaffected, only the <em>type</em> gets changed:</p> <pre><code>$var = 2.0; var_dump($var); # float(2) $var = (int) $var; var_dump($var); # int(2) </code></pre> <p>And now, remember how <code>&lt;int&gt;*&lt;float&gt;</code> resulted in a float? PHP will work in the same way when you do comparisons. In php, <code>"2.0" == 2</code> will be <code>true</code> because of this automatic typecasting. So when you do <code>&lt;int&gt; == &lt;float&gt;</code> what really happens is <code>(float)&lt;int&gt; == &lt;float&gt;</code>.</p> <p>So what happens if we do <code>(float)(int)0.3</code>. First we typecast 0.3 to int(0), then we typecast back to float(0). Obviously, <code>float(0) == float(0.3)</code> will be <code>false</code>. But <code>(float)(int)3.0</code> will be cast first to int(3) then to float(3), which was the same value we started with.</p> <pre><code>$var = 3.0; var_dump($var); # float(3) $var = (int) $var; var_dump($var); # int(3) $var = (float) $var; var_dump($var); # float(3) </code></pre> <p>So if we do <code>(int) 3.0 == 3.0</code>, it will result in <code>(float)(int) 3.0 == 3.0</code> which is <code>true</code>.</p> <p>So the way to test if a value is integer is to do</p> <pre><code>(int)$var == $var </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload