basic function and get

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
whkowiak
Forum Newbie
Posts: 8
Joined: Tue Dec 14, 2010 3:44 am

basic function and get

Post by whkowiak »

hey there

I am a beginner and I cannot figure out why this basic thing doesn´t work (it try to create a basic calculator as to practice the GET statement, the use of functions and of other statements such a switch).
I first thought about the operation signs (for the op variable) that should be converted (%2F etc) or even wirtten in plain text (like plus, minus etc) but it didn´t change anything. I tried to simplify it also without success. It seems to be that the switch statement is breaking the thing as when I have it, nothing works.
I know it is basic but as a beginner, I am only facing a blank page which only tells me that I made a mistake (are there actually any tools to troubleshoot our code?).
So here is the code:

<html>
<body>
<form action='php.php' method='GET'>
Enter the first number:&nbsp;
<input type='text' name='numb1'><br>
Enter the second number:&nbsp;
<input type='text' name='numb2'><br>
Choose the operation (+, -, *, /)&nbsp;
<input type='text' name='op'><br>
<input type='submit' value='Calculate!'>
</form><br><br>

<?php

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

if ($num1 && $num2 && $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 operation";
}
}
echo $total;

?>
</body>
</html>

Thank you very much in advance
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: basic function and get

Post by social_experiment »

Remove all the return $total lines from the switch statement.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
whkowiak
Forum Newbie
Posts: 8
Joined: Tue Dec 14, 2010 3:44 am

Re: basic function and get

Post by whkowiak »

@social_experiment: thank you very much for this
Could you explain me why is such thing happening?
When I learned the switch statement, I had a standalone example with the return component and it worked well
is it that it doesn´t goes together with the GET or does it come from the fact that it creates a redundancy when the variable is echoed at the end of the script?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: basic function and get

Post by social_experiment »

whkowiak wrote:is it that it doesn´t goes together with the GET or does it come from the fact that it creates a redundancy when the variable is echoed at the end of the script?
Honestly i have no idea, i just tinkered with your script :) . I think the return() stops the case 'statements' from being completed and that seems to be the problem but i might almost certainly be wrong.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
whkowiak
Forum Newbie
Posts: 8
Joined: Tue Dec 14, 2010 3:44 am

Re: basic function and get

Post by whkowiak »

ok thank you anyway
it was fast and now the script iis working like a charm :)
Post Reply