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:
<input type='text' name='numb1'><br>
Enter the second number:
<input type='text' name='numb2'><br>
Choose the operation (+, -, *, /)
<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
basic function and get
Moderator: General Moderators
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: basic function and get
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
Re: basic function and get
@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?
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?
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: basic function and get
Honestly i have no idea, i just tinkered with your scriptwhkowiak 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?
“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
Re: basic function and get
ok thank you anyway
it was fast and now the script iis working like a charm
it was fast and now the script iis working like a charm