Jump to content

First Script - Please Check It


Tommo

Recommended Posts

[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!
Link to comment
Share on other sites

Looks pretty good to me.

As far as php is concerned, a number from a form is text which only has digits in it.  Both is_numeric() and ctype_digit() will do this check for you.  But is_int() will FAIL, because it's a string of digits, rather than an integer.  PHP will automatically convert those strings into integers when necessary (which is why the calculations work), but is_int() will still return false.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.