Page 1 of 1

Function problems

Posted: Mon Jan 05, 2009 3:53 am
by opalcandy
I am close to completing this basic calculator script, however, I'm stuck with my current results of no returned response from a inputed form.

Here is my script. any help would be greatly appreciated.
thanks, :banghead:

<?php
//--------------------------------------------------------------------------------------------//
// I"m trying to create a calculator script that enables the user to submit two numbers and //
// choose an operation (addition, multiplication, division, or subtraction) //
// to perform on them. //
//-------------------------------------------------------------------------------------------//

//---------------------------//
// Define Variables //
//--------------------------//
$num1 = "$_POST[num1]";
$num2 = "$_POST[num2]";
$calculate = "$_POST[math_func]";
$results = "";
settype($num1, 'double');
settype($num2, 'double');

//-----------------------------------//
//Begin Validation Section //
//-----------------------------------//
//change default php error number in ini file or script
ini_set("error_reporting", E_ALL);

if (!isset($num1,$num2)){
$results = "PLEASE INPUT 2 NUMBERS and SELECT AN OPERATION TO PERFORM";
}

// ---------------------------------//
// Define Math Functions //
//---------------------------------//
function addNum($num1, $num2){
global $num1;
global $num2;
$results = $num1 + $num2;
return $results;
}
function subNum($num1, $num2){
global $num1;
global $num2;
$results = $num1 - $num2;
return $results;
}
function multiNum($num1, $num2) {
global $num1;
global $num2;
$results = $num1 * $num2;
return $results;
}
function divideNum($num1, $num2) {
global $num1;
global $num2;
$results = $num1 / $num2;
return $results;
}

//----------------------------------//
// Calculation Script //
//---------------------------------//
if ($calculate == "add_it") {
addNum($num1,$num2);
echo "$results";
} else if ($calculate == "sub_it") {
subNum($num1, $num2);
echo "$results";
} else if ($calculate == "multi_it") {
multiNum($num1, $num2);
echo "$results";
} else {
if ($num2 <= 0) {
// Denominator cannot be less than or equal to 0
$results = "You cannnot have a zero as a denominator";
} else {
divideNum($num1, $num2);
echo "$results";
}
}
//---------------------------------//
// End of Script //
//---------------------------------//
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="Adobe GoLive" />
<title>Basic Calculator Script</title>
</head>

<body bgcolor="#ffffff">
<form action="<?php echo $_SERVER[PHP_SELF] ?>" method="POST">
<b><u>Calculator Script</u></b><br>
<table>
<tr>
<td><strong>Number 1:</strong><input type="text" name="num1"><br><strong>Number 2:</strong><input type="text" name="num2"></td>
<td><select name="math_func">
<option value="add_it"> +</option>
<option value="sub_it"> -</option>
<option value="multi_it"> *</option>
<option value="div_it"> /</option>
</select></td>
</tr>

</table>
<input type="Submit" value="compute">



</form>
<br>Results: <?php echo $results ?>
<p></p>
</body>

</html>

Re: Function problems

Posted: Mon Jan 05, 2009 4:02 am
by Mark Baker

Code: Select all

 
function addNum($num1, $num2){
global $num1;
global $num2;
$results = $num1 + $num2;
return $results;
}
 
Why are you passing in $num1 and $num2 as parameters, and then immediately overiding them by referencing the global variables?
Why are you even using global anyway?

and why are you discarding your returned result when you call the function with

Code: Select all

addNum($num1,$num2);
and expecting to get anything meaningful?

Code: Select all

 
function addNum($num1, $num2){
   $sum = $num1 + $num2;
   return $sum;
}
 
$result = addNum($num1,$num2);
echo $result;
 

Re: Function problems

Posted: Mon Jan 05, 2009 4:57 am
by opalcandy
<?php
//---------------------------------------------------------------------------------------------------//
//
// I'm Trying to Create a calculator script that enables the user to submit two numbers and //
// choose an operation (addition, multiplication, division, or subtraction) //
// to perform on them. //
// --------------------------------------------------------------------------------------------------//

//---------------------------//
//Define Variables //
//---------------------------//
$numd1 = "$_POST[num1]";
$numd2 = "$_POST[num2]";
$calculate = "$_POST[math_func]";
$results = "";
$num1 = (double)$numd1;
$num2 = (double)$numd2;
//-----------------------------------//
//Begin Validation Section //
//-----------------------------------//
//change default php error number in ini file or script
ini_set("error_reporting", E_ALL);

if (!isset($num1,$num2)){
$results = "PLEASE INPUT 2 NUMBERS and SELECT AN OPERATION TO PERFORM";
}

settype($num1, 'double');
settype($num2, 'double');



//Create statement to check for string. Cannot compute strings.
//if ($numd1

//ctype_alnum($c) look under php.net

// ---------------------------------//
// Define Math Functions //
//----------------------------------//
function addNum($num1, $num2){
$sum = $num1 + $num2;
return $sum;
}
function subNum($num1, $num2){
$sum = $num1 - $num2;
return $sum;
}
function multiNum($num1, $num2) {
$sum = $num1 * $num2;
return $sum;
}
function divideNum($num1, $num2) {
$sum = $num1 / $num2;
return $sum;
}

//----------------------------------//
// Calculation Script //
//----------------------------------//
if ($calculate == "add_it") {
$results = addNum($num1,$num2);
echo "$results";
} else if ($calculate == "sub_it") {
$results = subNum($num1, $num2);
echo "$results";
} else if ($calculate == "multi_it") {
$results = multiNum($num1, $num2);
echo "$results";
} else {
if ($num2 <= 0) {
// Denominator cannot be less than or equal to 0
$results = "You cannnot have a zero as a denominator";
} else {
$results = divideNum($num1, $num2);
echo "$results";
}
}
//---------------------------------//
// End of Script //
//---------------------------------//
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="Adobe GoLive" />
<title>Simple calc script</title>
</head>

<body bgcolor="#ffffff">
<form action="<?php echo $_SERVER[PHP_SELF] ?>" method="POST">
<b><u>Calculator Script</u></b><br>
<table>
<tr>
<td><strong>Number 1:</strong><input type="text" name="num1"><br><strong>Number 2:</strong><input type="text" name="num2"></td>
<td><select name="math_func">
<option value="add_it"> +</option>
<option value="sub_it"> -</option>
<option value="multi_it"> *</option>
<option value="div_it"> /</option>
</select></td>
</tr>

</table>
<input type="Submit" value="compute">



</form>
<br>Results: <?php echo $results ?>
<p></p>
</body>

</html>

Re: Function problems

Posted: Mon Jan 05, 2009 6:25 am
by Mark Baker
Change

Code: Select all

<form action="<?php echo $_SERVER[PHP_SELF] ?>" method="POST">
to

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">

Re: Function problems

Posted: Mon Jan 05, 2009 8:47 am
by susrisha
Change these

Code: Select all

$numd1 = "$_POST[num1]";
$numd2 = "$_POST[num2]";
$calculate = "$_POST[math_func]";
to

Code: Select all

 
$numd1 = $_POST['num1'];
$numd2 = $_POST['num2'];
$calculate = $_POST['math_func'];
 

Re: Function problems

Posted: Mon Jan 05, 2009 2:40 pm
by opalcandy
Thank you all so much.

Here is the working script.


<?php
//--------------------------------------------------------------------------------- //
// //
// Create a calculator script that enables the user to submit two numbers and //
// choose an operation (addition, multiplication, division, or subtraction) //
// to perform on them. //
// --------------------------------------------------------------------------------//

//---------------------------//
//Define Variables //
/---------------------------//
$numd1 = $_POST['num1'];
$numd2 = $_POST['num2'];
$calculate = $_POST['math_func'];
$results = "";
$num1 = (double)$numd1;
$num2 = (double)$numd2;
//------------------------------------//
//Begin Validation Section //
//-----------------------------------//
//change default php error number in ini file or script
ini_set("error_reporting", E_ALL);

if (!isset($num1,$num2)){
$results = "PLEASE INPUT 2 NUMBERS and SELECT AN OPERATION TO PERFORM";
}

settype($num1, 'double');
settype($num2, 'double');



// ---------------------------------//
// Define Math Functions //
//---------------------------------//
function addNum($num1, $num2){
$sum = $num1 + $num2;
return $sum;
}
function subNum($num1, $num2){
$sum = $num1 - $num2;
return $sum;
}
function multiNum($num1, $num2) {
$sum = $num1 * $num2;
return $sum;
}
function divideNum($num1, $num2) {
$sum = $num1 / $num2;
return $sum;
}

//----------------------------------//
// Calculation Script //
//---------------------------------//
if ($calculate == "add_it") {
$results = addNum($num1,$num2);
} else if ($calculate == "sub_it") {
$results = subNum($num1, $num2);
} else if ($calculate == "multi_it") {
$results = multiNum($num1, $num2);
} else {
if ($num2 <= 0) {
// Denominator cannot be less than or equal to 0
$results = "You cannnot have a zero as a denominator";
} else {
$results = divideNum($num1, $num2);
}
}
//---------------------------------//
// End of Script //
//---------------------------------//
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="Adobe GoLive" />
<title>Chapter 9 - Activities 1</title>
</head>

<body bgcolor="#ffffff">
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<b><u>Calculator Script</u></b><br>
<table>
<tr>
<td><strong>Number 1:</strong><input type="text" name="num1"><br><strong>Number 2:</strong><input type="text" name="num2"></td>
<td><select name="math_func">
<option value="add_it"> +</option>
<option value="sub_it"> -</option>
<option value="multi_it"> *</option>
<option value="div_it"> /</option>
</select></td>
</tr>

</table>
<input type="Submit" value="compute">
</form>
<br>Results: <?php echo $results ?>
<p></p>
</body>

</html>