Jump to content

Tommo

New Members
  • Posts

    3
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Tommo's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thankyou for your reply. I've just updated the original post with a new version of the script which includes a validator (thanks to your help) and a few other adjustments.
  2. [i]By the way, SMF is my favourite forum, I use it on all my sites.[/i] I have written my first script from scratch. It's a calculator and it works great. But would there be a more effecient way of writing this code (for learning purposes). I know I don't need the function, but It's there as I'm practicing my function skills. So if there is a better/smaller way of writing this code, please post it below. Here's the script with html... [code]<?php // Calculates the number of times the form has been resubmitted by the user. $count_final is needed as otherwise the count is one number too high. I use a hidden field to keep the count value everytime the form is resubmitted. $counter = (int) $_POST['counter']; $counter++; $count_final = $counter - 1 ?> <form action="<?php $_SERVER['PHP_SELF']?>" method="post">   <label>First Number:   <input type="text" name="value1">   </label>   <label>Second Number:   <input type="text" name="value2">   </label> Operator: <input type="submit" name="add" value="Add">  <input type="submit" name="subtract" value="Subtract">  <input type="submit" name="multiply" value="Multiply"> <input type="submit" name="divide" value="Divide"> <input type="hidden" name="counter" value="<?php print $counter ?>"> </form> <?php $value1 = $_POST['value1']; $value2 = $_POST['value2']; // Checks for what button you have pressed and then performs the maths function process($val_1, $val_2) { $add = $_POST['add']; $subtract = $_POST['subtract']; $multiply = $_POST['multiply']; $divide = $_POST['divide']; if ( isset($add) ) { $result = $val_1 + $val_2; } else if ( isset($subtract) ) { $result = $val_1 - $val_2; } else if ( isset($multiply) ) { $result = $val_1 * $val_2; } else if ( isset($divide) ) { $result = $val_1 / $val_2; } return $result; } // Prints the result to the browser print @process($value1, $value2); // Prints the number of times the form has been resubmitted by the user print "<br /><br />"; if ($count_final == 1) { print "You have used the calculator: 1 time"; } else if ($count_final > 1 || $count_final == 0) { print "You have used the calculator: $count_final times"; } ?>[/code] Notice I put an @ sign infront of the function call as if the user divides 0 by 0 or something else like that, it generates an error. And so the @ sign surpresses the error (I learnt that little trick shortly after I wrote this script so I implemented it). Look how skillful I sound :) Can I also ask, what would be the best way to check whether the user has submitted a number rather than text? Cheers!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.