Jump to content

Recommended Posts

I'm really new to php. I've only been messing with it for a couple of days now, and i tried making my own calculator kinda thing but its not working right and i cant figure it out :(. could anyone help me out and tell me whats wrong and why its not working?

1st post on this forum too btw =D

 

<html>

<form action='calc.php' method='GET'>
      Number 1:<input type='text' name='num1'><br>
      OP:<input type='text' name='op'><br>
      Number 2:<input type='text' name='num2'><br>
      <input type='submit' value='Equals'>
</form>

</html>


<?php



$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$op = $_GET['op'];

switch ($op)
{
case "+":
$total = $num1 + $num2;
return $total;
break;

case "-":
$total = $num1 - $num2;
return $total;
break;

case "*":
$total = $num1 * $num2;
return $total;
break;

case "/":
$total = $num1 / $num2;
return $total;
break;

default:
echo "unknown operator";

}
echo $total;
}

Link to comment
https://forums.phpfreaks.com/topic/155755-solved-new-to-php-calc-help/
Share on other sites

you dont really need to return $total, since you are assigning it a value (I'm pretty sure, someone please correct me if i'm wrong) but I dont know if using that return would mess up the script. however, you probably want to encase all your php in the following if statement

 

if (isset($_GET['num1'] && isset($_GET['num2'] && isset($_GET['num3']){
...your code
}

 

that way you dont run that switch statement if the user first accesses the page.

Do not do return $total, you should use return more or less for methods and functions. You are not using it in the way you think.

 

Remove the return's and see if that fixes it.

 

You also have an extra closing brace ( } ) and next time explain how it is not working. It will result in better help.

awesome

i put in the if code, i wasnt really sure at first what it was for but now i do lol. thanks mikesta707.

and i took out the returns and its working just fine now.at first it wasnt but then i saw it said get num3, i switched it to op and then it worked lol.

 

thanks for the help guys. sorry i didnt really explain what it was doing. when i 1st tried it, id put in the numbers and the op and after i hit submit the get part worked, everything was coming up in the URL ok but it wasnt echoing out anything and i wasnt sure if it was even calculating anything.

 

thanks for the help guys, i appreciate it =D

i might be in here alot lol

 

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.