donglu4159
2011-06-30 20:20
浏览 82
已采纳

在if条件下更改全局变量值,然后在else中访问值

I have a global variable being changed in if statement then accessed in else, but it does not have the same value and loses its value:

global $reqUserID;
    if ($_SERVER['REQUEST_METHOD'] == 'POST'){

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

            $requests = new requests();
            $results = $requests->getRequest($_POST['reqID']);

            $result = mysql_fetch_array($results, MYSQL_ASSOC))

            $GLOBALS['reqUserID'] = $result['USER_ID']; //CORRECT VALUE HERE

        }
        else if (isset($_POST['approveBtn'])) {

            $var1 = $GLOBALS['reqUserID'];
            $sql = "SELECT EMAIL FROM user_info WHERE USER_ID='$var1'";//SOME OTHER VALUE HERE
            [...]
        }
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanjing9339 2011-06-30 20:47
    已采纳

    page was being posted to itself, and Global variable involved was not being posted to be used, so i made a hidden field filled it with the value of global variable on the first pass and posted the value and used it late using _POST array.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dtmtu02882 2011-06-30 20:23

    If you set a value in the 'if' clause, then the 'else' clause will not be executed, and vice versa. So, you could only get to the 'else' clause on a second iteration of the statement. Then you need to understand where variables are defined, and when they become undefined.

    评论
    解决 无用
    打赏 举报
  • duanfu7004 2011-06-30 20:26

    In one pass over the if-else construct you can either enter in the if-construct or the else construct. So, the change you make in if-part will not be visible in the code in else-if construct.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题