Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd take advantage of <a href="http://php.net/manual/en/function.is-numeric.php" rel="nofollow">is_numeric</a> and also test for 0 before dividing like so:</p> <pre><code>&lt;?php $valuea=''; $valueb=''; $valuec=''; $answer=''; if (isset($_POST['valuea'])) $valuea = $_POST['valuea']; if (isset($_POST['valueb'])) $valueb = $_POST['valueb']; if (isset($_POST['valuec'])) $valuec = $_POST['valuec']; if(is_numeric($valuea) &amp;&amp; is_numeric($valueb) &amp;&amp; is_numeric($valuec)) { if($valuec=='0') { $answer ="Error: Dividing by zero"; } else{ $answer = ($valuea - $valueb) / $valuec; } } echo &lt;&lt;&lt;_END &lt;form method='post'&gt; &lt;table border='0' width='500px' cellpadding='3' cellspacing='1' class="table"&gt; &lt;tr class="calcheading"&gt;&lt;td colspan="2"&gt;&lt;strong&gt;Stright Line Depreciation&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr class="calcrow"&gt;&lt;td&gt;Enter Cost:&lt;/td&gt;&lt;td align="center"&gt;&lt;input type="text" name="valuea" value="$valuea" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr class="calcrow2"&gt;&lt;td&gt;End Salvage Value:&lt;/td&gt;&lt;td align="center"&gt;&lt;input type="text" name="valueb" value="$valueb" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr class="calcrow"&gt;&lt;td&gt;Enter Estimated Life:&lt;/td&gt;&lt;td align="center"&gt;&lt;input type="text" name="valuec" value="$valuec" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr class="submit"&gt;&lt;td colspan="2"&gt;&lt;input type="submit" value="Calculate" /&gt;&lt;/td&gt;&lt;/tr&gt; _END; ?&gt; &lt;tr class="calcrow"&gt;&lt;td&gt;&lt;em&gt;The answer is:&lt;/em&gt;&lt;/td&gt;&lt;td align="center"&gt;&lt;input type="text" value="&lt;?php echo is_numeric($answer)?round($answer):$answer; ?&gt;"&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p>Here's a <a href="http://phpfiddle.org/main/code/f2u-cir" rel="nofollow">phpfiddle</a> and <a href="http://phpfiddle.org/api/run/f2u-cir" rel="nofollow">its results</a>.</p>
 

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