Jump to content


Photo

First Script - Please Check It


  • Please log in to reply
3 replies to this topic

#1 Tommo

Tommo
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 19 September 2006 - 05:08 AM

By the way, SMF is my favourite forum, I use it on all my sites.

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

<?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";
}
?>

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!

#2 Tommo

Tommo
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 19 September 2006 - 06:59 AM

Anyone?

#3 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 19 September 2006 - 07:35 AM

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.

#4 Tommo

Tommo
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 19 September 2006 - 08:16 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users