【问题标题】:Work with variables in two conditions在两种情况下使用变量
【发布时间】:2023-03-26 06:08:02
【问题描述】:

我在处理第二个条件中的一个条件的变量时遇到问题。我有类似的东西:

<form name="exampleForm" method="post">
...
<input type="submit" name="firstSubmit" value="Send">
<input type="submit" name="secondSubmit" value="Show">
</form>

<?php
if(isset($_POST['firstSubmit'])) {

 function a() {
  $a = 5;
  $b = 6;
  $c = $a + $b;
  echo "The result is $c";
  return $c;
 }

 $functionOutput = a();
}

if(isset($_POST['secondSubmit'])) {

 echo $functionOutput;
}
?>

当我需要在第一个条件下使用变量 $functionOutput 时,我总是会收到一条错误消息(未定义的变量)。我该如何解决这个问题?

【问题讨论】:

    标签: php variables conditional-statements


    【解决方案1】:

    我不确定您到底要做什么,但是当您按下第二个按钮时,变量 $functionOutput 未定义,因为第一个条件是 false,因此整个部分都被跳过了。

    请注意,脚本一结束,变量就会丢失。你可以查看sessions 并使用会话变量来解决这个问题,但这有点取决于你想要做什么。

    要使用会话,您必须将整个 php 块移动到开始输出 html 之前的位置并执行以下操作:

    <?php
    session_start();
    
    if(isset($_POST['firstSubmit'])) {
    
     function a() {
      $a = 5;
      $b = 6;
      $c = $a + $b;
      return $c;
     }
    
     $_SESSION['output'] = a();
    }
    
    
    // start html output
    ?>
    <doctype .....
    <html ....
    
    // and where you want to echo
    if(isset($_POST['firstSubmit'])) {
      echo "The result is {$_SESSION['output']}";
    }
    
    if(isset($_POST['secondSubmit'])) {
    
     echo $_SESSION['output'];
    }
    

    【讨论】:

    • 感谢您的回答。我还有一个问题。使用会话安全吗?
    • @Mato 当然,所有信息都存储在服务器端。还有更多内容,您可以添加会话和表单变量以避免会话劫持,但您必须查找更多详细信息。
    【解决方案2】:
    <?php
    $functionOutput = "";
    
    if(isset($_POST['firstSubmit'])) {
    
     function a() {
      $a = 5;
      $b = 6;
      $c = $a + $b;
      echo "The result is $c";
      return $c;
     }
    
     $functionOutput = a();
    }
    
    if(isset($_POST['secondSubmit'])) {
    
     echo $functionOutput;
    }
    ?>
    

    应该修复它。发生这种情况是因为您在第一个 IF 语句中声明了 $functionOutput。

    【讨论】:

      【解决方案3】:

      因为$functionOutput在你调用if(isset($_POST['secondSubmit']))时没有初始化

      <?php
      if(isset($_POST['firstSubmit'])) {
      
       function a() {
        $a = 5;
        $b = 6;
        $c = $a + $b;
        echo "The result is $c";
        return $c;
       }
      
       $functionOutput = a();
      }
      $functionOutput='12';//intialize
      if(isset($_POST['secondSubmit'])) {
      
       echo $functionOutput;
      }
      ?> 
      
               **OR**
      
      <?php
      if(isset($_POST['firstSubmit'])) {
      
       function a() {
        $a = 5;
        $b = 6;
        $c = $a + $b;
        echo "The result is $c";
        return $c;
       }
      
       $functionOutput = a();
      }
      
      if(isset($_POST['secondSubmit'])) {
       function a() {
        $a = 5;
        $b = 6;
        $c = $a - $b;
        echo "The result is $c";
        return $c;
       }
      
       $functionOutput = a();
       echo $functionOutput;
      }
      ?> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多