php 具有功能和开关的php基本计算器,但结果是
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14937524/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php basic calculator with function and switch but result is
提问by Christine Javier
i am currently doing some simple calculator for a practice but it the output or result is not showing here is my code guys hope you can help me :/
我目前正在做一些简单的计算器来练习,但这里没有显示输出或结果是我的代码,希望你能帮助我:/
    <input type="radio" value= "Addition" name="calcu"> Addition .<br />
    <input type="radio" value= "Subtraction" name="calcu"> Subtraction .<br />
    <input type="radio" value= "Multiplication" name="calcu"> Multiplication .<br />
    <input type="radio" value= "Division" name="calcu"> Division .<br />
<?php
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$calcu = $_POST['calcu'];
    function calculate($n1,$n2)
    {
        switch('$calcu')
        {
        case "Addition";
            $compute = $n1 + $n2; 
            break;
        case "Subtraction";
            $compute = $n1 - $n2; 
            break;
        case "Multiplication";
            $compute = $n1 * $n2; 
            break;
        case "Division";
            $compute = $n1 / $n2; 
            break;
        }
    }
echo "$calcu <br /> <br /> 1st Number: $num1 <br /> 2nd Number: $num2 <br /><br />";
echo "Answer is:" .calculate($num1,$num2);
?>
回答by Muhammad Talha Akbar
Here is complete code:
这是完整的代码:
<?php
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$calcu = $_POST['calcu'];
    function calculate($n1,$n2, $calcu) // set $calcu as parameter
    {
        switch($calcu)
        {
        case "Addition": // here you have to use colons not semi-colons
            $compute = $n1 + $n2; 
            break;
        case "Subtraction":
            $compute = $n1 - $n2; 
            break;
        case "Multiplication":
            $compute = $n1 * $n2; 
            break;
        case "Division":
            $compute = $n1 / $n2; 
            break;
        }
        return $compute; // returning variable
    }
echo "$calcu <br /> <br /> 1st Number: $num1 <br /> 2nd Number: $num2 <br /><br />";
echo "Answer is:" .calculate($num1,$num2, $calcu); // you need to pass $calcu as argument of that function
?>
回答by Alex
Change switch('$calcu')to switch($calcu). It should be this way. 
更改switch('$calcu')为switch($calcu)。应该是这样。
But not only that. Your variables are undefined because you are trying to address them before form is submited, i.e they don't exist yet.
但不仅如此。您的变量未定义,因为您试图在提交表单之前处理它们,即它们尚不存在。
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$calcu = $_POST['calcu'];
And there you address them
在那里你解决他们
echo "$calcu <br /> <br /> 1st Number: $num1 <br /> 2nd Number: $num2 <br /><br />";
echo "Answer is:" .calculate($num1,$num2);
The right way to implement this is to check if form was submitted:
实现这一点的正确方法是检查表单是否已提交:
    <input type="radio" value= "Addition" name="calcu"> Addition .<br />
    <input type="radio" value= "Subtraction" name="calcu"> Subtraction .<br />
    <input type="radio" value= "Multiplication" name="calcu"> Multiplication .<br />
    <input type="radio" value= "Division" name="calcu"> Division .<br />
<?php
if (isset($_POST)){
    $num1 = $_POST['num1'];
    $num2 = $_POST['num2'];
    $calcu = $_POST['calcu'];
        function calculate($n1,$n2)
        {
            switch('$calcu')
            {
            case "Addition";
                $compute = $n1 + $n2; 
                break;
            case "Subtraction";
                $compute = $n1 - $n2; 
                break;
            case "Multiplication";
                $compute = $n1 * $n2; 
                break;
            case "Division";
                $compute = $n1 / $n2; 
                break;
            }
        }
    echo "$calcu <br /> <br /> 1st Number: $num1 <br /> 2nd Number: $num2 <br /><br />";
    echo "Answer is:" .calculate($num1,$num2);
    unset($_POST);
}
?>
回答by Justin John
Change switch('$calcu')to switch($calcu).
更改switch('$calcu')为switch($calcu)。
As @PeterM mentioned, you are accessing variable $calcuout of scope. Either you pass the $calcuvariable to fun calculateor access directly by $_POSTarray.
正如@PeterM 所提到的,您正在访问$calcu超出范围的变量。要么将$calcu变量传递给 funcalculate要么直接通过$_POST数组访问。
use switch($_POST['calcu']).
使用switch($_POST['calcu']).
OR
或者
function calculate($n1,$n2, $calcu) {
...
}
Call the fun by calculate($n1,$n2, $calcu).
通过调用乐趣calculate($n1,$n2, $calcu)。

