Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found a solution at <a href="http://forums.phpfreaks.com/topic/283274-simple-math-problem/?p=1455892" rel="nofollow">this site</a>. Here is the code from that post:</p> <pre><code> &lt;?php $expression = ''; $input = array( 'a' =&gt; 5, 'b' =&gt; 10, 'c' =&gt; 15, 'd' =&gt; 20); $total = 0; $sum = ''; // Override default expression and input values if(isset($_POST['Calculate'])) { $input = $_POST['input']; $expression = $_POST['expression']; } // make sure the users expression is safe to use if(preg_match('#^[a-z+-\s]+$#i', $expression)) { $sum = $expression; $input_letters = array(); $input_operators = array(); for ($i = 0; $i &lt; strlen($expression) ; $i++) { if (($i % 2) == 0) { $input_letters[] = $expression[$i]; } else { $input_operators[] = $expression[$i]; } } // for ($i = 0 ; $i &lt; sizeof($input_letters); $i++) { $value = $input[$input_letters[$i]]; if ($i == 0) { $sum += $value; } else { if ($input_operators[$i-1] == '+') { $sum += $value; } else if ($input_operators[$i-1] == '-') { $sum -= $value; } } } } else { echo "Error: expression not correct form"; } ?&gt; &lt;form action="" method="post"&gt; Number A: &lt;input type="text" name="input[a]" value="&lt;?php echo $input['a']; ?&gt;" size=3&gt; &lt;br/&gt; Number B: &lt;input type="text" name="input[b]" value="&lt;?php echo $input['b']; ?&gt;" size=3&gt; &lt;br&gt; Number C: &lt;input type="text" name="input[c]" value="&lt;?php echo $input['c']; ?&gt;" size=3&gt; &lt;br&gt; Number D: &lt;input type="text" name="input[d]" value="&lt;?php echo $input['d']; ?&gt;" size=3&gt; &lt;br&gt; &lt;br&gt; Expression: &lt;input type = "text" name = "expression" value="&lt;?php echo $expression ?&gt;"&gt; = &lt;?php echo $sum; ?&gt;&lt;br&gt; Sum: &lt;?php echo $sum; ?&gt;&lt;br&gt; &lt;input type="submit" name="Calculate" /&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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