首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >practice函数($result)不能调用

practice函数($result)不能调用
EN

Stack Overflow用户
提问于 2021-02-23 06:16:35
回答 1查看 35关注 0票数 0

我正在做PHP实践,我被困在这部分。

我的问题是,当我做简单的数学,如1+1或1/1等,$result将不会显示。我对代码的哪一部分引起了问题感到困惑。请告诉我我的错误

谢谢。

代码语言:javascript
复制
       <?php
         $first_num = $_POST['first_num'];
         $second_num = $_POST['second_num'];
         $operator = $_POST['operator'];
         $result = '';

         function cal_result($first_num,$second_num,$operator){
              if (is_numeric($first_num) && is_numeric($second_num)) {
                    switch ($operator) {
                        case "Add":
                        $result = $first_num + $second_num;
                        break;
                        case "Subtract":
                        $result = $first_num - $second_num;
                        break;
                        case "Multiply":
                        $result = $first_num * $second_num;
                        break;
                        case "Divide":
                $result = $first_num / $second_num;
                }
            return $result;        
           }  
         }
      ?>

    <form action="simple_calculator.php" method="post" id="quiz-form">
            <p>
                <input type="number" name="first_num" id="first_num" required="required" value="<?php  cal_result($first_num,$second_num,$operator); ?>" /> <b>First Number</b>
            </p>
            <p>
                <input type="number" name="second_num" id="second_num" required="required" value="<?php cal_result($first_num,$second_num,$operator); ?>" /> <b>Second Number</b>
            </p>
            <p>
                <input readonly="readonly" name="result" value="<?php  echo $result; ?>"> <b>Result</b>
            </p>
            <input type="submit" name="operator" value="Add" />
            <input type="submit" name="operator" value="Subtract" />
            <input type="submit" name="operator" value="Multiply" />
            <input type="submit" name="operator" value="Divide" />
    </form>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-23 10:48:01

我想你被这个简短的标签弄糊涂了

如果我打印

代码语言:javascript
复制
<?=$result;?>

代码语言:javascript
复制
<?=cal_result($first_num,$second_num,$operator);?>

这是一样的事情

代码语言:javascript
复制
<?php echo $result;?>

代码语言:javascript
复制
<?php echo cal_result($first_num,$second_num,$operator);?>

如果您调用一个“返回”值的函数,并且要使用它们,您还必须将它们打印出来.

还可以查看全局变量。如果要在函数作用域之外使用$result,则必须将其声明为全局变量。

代码语言:javascript
复制
<?php
  $result = '';
  $first_num = 0;
  $second_num = 0;
  
  $ops = array( "+", 
                "-",
                "×",  // HTML : &times; , ASCII CODE : 158 
                "÷"); //  HTML : &divide; , ASCII CODE : 246

  // checking if the first number is what he should be
  if ( isset($_POST['first_num']) && 
      !empty($_POST['first_num']) && 
      is_numeric($_POST['first_num']) )
  {
    $first_num = $_POST['first_num'];
  }

  // checking if the second number is what he should be
  if ( isset($_POST['second_num']) && 
      !empty($_POST['second_num']) && 
      is_numeric($_POST['second_num']) )
  {
    $second_num = $_POST['first_num'];
  }
  
  function cal_result($first_num,$second_num,$operator){
    
    // optionally you can assign global
    global $result;
    
    // here we prevent injection by acceptint only operators from the ops array
    if (isset($_GET['operator']) && in_array($_GET['operator'],$ops)) {
      switch ($_GET['operator']) {
        case $ops[0]: // first element of array ops is Add
          $result = $first_num + $second_num;
          // remember this function is just returning the $result 
          // variable it is not printing it, you will have to assiging 
          // the function to a variable if you want to use the  
          return $result;
        break;
        case $ops[1]: // second element of array ops is Subtract
          $result = $first_num - $second_num;
          return $result;
        break;
        case $ops[2]: // third element of array ops is Multiply
          $result = $first_num * $second_num;
          return $result;
        break;
        case $ops[3]: // fourth element of array ops is Divide
          // divide by zero problem
          if($second_num !== 0){
            $result = $first_num / $second_num;
          }else{
            $result = "Err: Div By Zero";
          }
          return $result;
        break;
      }
    }    
  }
}



?>
<p>
    <!--// now you have 2 possibilities :                 //-->
    <!--// first you can call the global variable $result //-->
    <label for="quiz-form"><?=$result;?></label>

    <!--// the second possibility is to call the function //-->
    <label for="quiz-form"><?=cal_result($first_num,$second_num,$operator);?></label>

    <b>Result</b>
</p>
<!-- for post you should also set enctype for security reasons -->
<form action="simple_calculator.php" 
      method="post" 
      id="quiz-form" 
      enctype="application/x-www-form-urlencoded">
        <p>
            <input  type="number" 
                    name="first_num" 
                    id="first_num" 
                    required="required" 
                    value="<?=$first_num;?>" />
            <b>First Number</b>
        </p>
        <p>
            <input  type="number" 
                    name="second_num" 
                    id="second_num" 
                    required="required" 
                    value="<?=$second_num;?>" />
            <b>Second Number</b>
        </p>
        <input type="submit" name="operator" value="+" alt="Add" />
        <input type="submit" name="operator" value="-" alt="Subtract" />
        <input type="submit" name="operator" value="×" alt="Multiply" />
        <input type="submit" name="operator" value="÷" alt="Divide" />
</form>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66328016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档