PHP全局变量 - 无法分配新值

时间:2017-10-30 10:53:21

标签: php

此代码适用于Telegram bot。 我正在尝试全局变量但由于某种原因它不存储值。 我所做的是在函数之外声明变量,然后在内部声明这些变量全局,但我不能分配任何新值。他们仍然返回我输入的初始值。

我做错了什么?

  $title=9; $cat=8; //to test global variable
  function processMessage($message) {
  global $title, $cat; //declaring global
  // process incoming message
  $message_id = $message['message_id'];
  $chat_id = $message['chat']['id'];
  $sender_id = $message['from']['id'];
  $sender_first_name = $message['from']['first_name'];
  $sender_last_name = $message['from']['last_name'];
  $message_date = $message['date'];

  if (isset($message['text'])) {
    // incoming text message
    $text = $message['text'];

//when user starts the bot
    if (strpos($text, "/start") === 0) {
      apiRequestJson("sendmessage", array('chat_id' => $chat_id, "text" => 'Please select an option:', 'reply_markup' => array(
        'keyboard' => array(array('Category Check', 'Uncategorized')),
        'one_time_keyboard' => false,
        'resize_keyboard' => true)));

    }

    //category check that returns news title and category question to user
    else if ($text === "Category Check") {

      //MySQL Connection Details
        $servername = "localhost";
        $username = "user";
        $password = "pass";
        $dbname = "dbname";
        $conn = new mysqli($servername, $username, $password, $dbname);

        $sql = "SELECT * FROM `newsdata` WHERE `nb_count` OR `svm_count` <> 0 ORDER BY RAND() LIMIT 1 ";

        $row = $result->fetch_assoc();

        $nb_business = $row["nb_business"];
        $nb_entertainment = $row["nb_entertainment"];
        $nb_health = $row["nb_health"];
        $nb_politics = $row["nb_politics"];
        $nb_science = $row["nb_science"];
        $nb_technology = $row["nb_technology"];
        $nb_world = $row["nb_world"];
        $svm_business = $row["svm_business"];
        $svm_entertainment = $row["svm_entertainment"];
        $svm_health = $row["svm_health"];
        $svm_politics = $row["svm_politics"];
        $svm_science = $row["svm_science"];
        $svm_technology = $row["svm_technology"];
        $svm_world = $row["svm_world"];
        $title=$row["title"]; //assigning title
        $nb_count=$row["nb_count"];
        $svm_count=$row["svm_count"];

        if($nb_business!=0)
        {$cat='BUSINESS'; $track=1;}
        else if ($nb_entertainment!=0)
        {$cat='ENTERTAINMENT'; $track=1;}
        else if ($nb_health!=0)
        {$cat='HEALTH'; $track=1;}
        else if ($nb_politics!=0)
        {$cat='POLITICS'; $track=1;}
        else if ($nb_science!=0)
        {$cat='SCIENCE'; $track=1;}
        else if ($nb_technology!=0)
        {$cat='TECHNOLOGY'; $track=1;}
        else if ($nb_world!=0)
        {$cat='WORLD'; $track=1;}

        else if ($svm_business!=0)
        {$cat='BUSINESS'; $track=0;}
        else if ($svm_entertainment!=0)
        {$cat='ENTERTAINMENT'; $track=0;}
        else if ($svm_health!=0)
        {$cat='HEALTH'; $track=0;}
        else if ($svm_politics!=0)
        {$cat='POLITICS'; $track=0;}
        else if ($svm_science!=0)
        {$cat='SCIENCE'; $track=0;}
        else if ($svm_technology!=0)
        {$cat='TECHNOLOGY'; $track=0;}
        else if ($svm_world!=0)
        {$cat='WORLD'; $track=0;}
          // output data of each row

//asking user if the category is correct
          apiRequestJson("sendmessage", array('chat_id' => $chat_id, "text" => 'Is this news belongs to '.
          $cat.' category? '.
          $title.' '.$row["link"],
          'reply_markup' => array(
          'keyboard' => array(array('yes', 'no')),
          'one_time_keyboard' => false,
          'resize_keyboard' => true)));
    }
    //user answers
    if ($text == "yes" || $text == "no"){
          switch ($text) {
            case "yes":
                $nb_count=$nb_count--;
                $svm_count=$svm_count--;
                apiRequestJson("sendmessage", array('chat_id' => $chat_id, "text" => $cat.$title)); //broken here, output: 98

                break;
            case "no":
                //dothat; break;
          }

0 个答案:

没有答案