在函数中包含我无法在 HTML 中回显的变量

Having variables in a function that I can't echo in HTML

我有一个带有 "submit" 按钮的表单,单击它会调用 display() 函数。

display() 是一个包含另一个 .php 文件的函数,我可以在其中对 id 进行不同的转换。

我现在的问题是,我想 "echo" 转换后的 ID 到我的 html,我不能这样做,因为转换的变量在函数内部。

目前我正在执行以下操作,我在一部分 "php tags" 中具有转换功能(带有我想回显到我的 HTML 的变量),然后我包含 "php tags, echo" 在我的 HTML.

但遗憾的是,我尝试了很多方法,将变量设置为全局变量,尝试在其他地方定义它们,但变量将保持未定义状态。

基本上我是在问,我是否做错了什么,或者是否有 better/more 合乎逻辑的方式来做这件事,这样我就可以调用这些变量。 (记住我每次都需要它们,因为提交表单时会提交 URL。)

这是我的代码 (index.php)

            <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <meta http-equiv="X-UA-Compatible" content="ie=edge">
                <title>STEAM CONV</title>
                <link rel="stylesheet" href="style.css">
            </head>

            <body>
                <form method="post">
                    <input type="text" name="profile_url">
                    <input type="submit" value="click" name="submit">
                </form>

                <h1><?php echo $userid;   ?></h1>

               <img onerror="this.style.display='none'" src="<?php

                    require_once 'steamid.class.php';
                    $input = $_POST["profile_url"];
                    $api_key = "XXXXXXXXXXXXXXXXXXXX";
                    $id = new SteamID($input,$api_key);

                    if ($id->resolveVanity()) {
                        $avatar = $id->toAvatar();
                        echo $avatar;
                    }

               ?>">

            </body>


    <?php

    require_once 'steamid.class.php';


    function urlCheck() {
        $input = $_POST["profile_url"];
        if(substr_count($input, ' ') === strlen($input)) {
          echo "Enter URL";

          return false;
        } else {
          return true;
        }

    }

    if(isset($_POST['submit']))
    {
       display();
    }

    function display() {

        require_once 'steamid.class.php';
        $input = $_POST["profile_url"];
        $api_key = "XXXXXXXXXXXX";
        $id = new SteamID($input,$api_key);

        if(urlCheck()) {

            if ($id->resolveVanity()) {



            $communityid = $id->toCommunityID();
            echo $communityid . ", " . " ";

            $steamid = $id->toSteamID();
            echo $steamid . ", " . " ";

            global $userid;

            $userid = '[U:1:'.$id->toUserID().']';
            echo $userid . ", " . " ";



            } else {
                echo "Profile wasnt found!";
            }

        }
    }


    ?>

steamid.class.php

<?php

        class SteamID {

          protected $id;
          protected $key;

          public function __construct($id,$api_key = '') {
            $this->id = $id;
            $this->key = $api_key;
            return $this;
          }

          public function isID32() {
            if(preg_match('/^STEAM_0:[01]:[0-9]{8,9}$/', $this->id)) {
                return true;
            }
            return false;
          }

          public function isID64() {
            if(preg_match('/7656119[0-9]{10}/i', $this->id)) {
              $this->id = $this->cleanOutput(str_replace('https://steamcommunity.com/profiles/', '', $this->id));
              return true;
            }
            return false;
          }

          public function resolveVanity() {
            $search = $this->cleanOutput(str_replace('https://steamcommunity.com/id/', '', $this->id));
            $api = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key='.$this->key.'&vanityurl='.$search;
            $vanity = $this->getCURL($api);
            if ($vanity['response']['success'] === 1) {
              $this->id = $vanity['response']['steamid'];
              return true;
            }
            else {
              return false;
            }
          }

          public function toAvatar() {
            if ($this->isID32() || $this->isID64() || $this->resolveVanity()) {
              $key = $this->toCommunityID();
              $api = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.$this->key.'&steamids='.$key;
              $data = $this->getCURL($api);
              $image = $data['response']['players'][0]['avatarfull'];
              return $image;
            }
            else {
              return false;
            }
          }

          public function toCommunityID() {
              if (preg_match('/^STEAM_/', $this->id)) {
                  $parts = explode(':', $this->id);
                  return bcadd(bcadd(bcmul($parts[2], '2'), '76561197960265728'), $parts[1]);
              } elseif (is_numeric($this->id) && strlen($this->id) < 16) {
                  return bcadd($this->id, '76561197960265728');
              } else {
                  return $this->id;
              }
          }

          public function toSteamID() {
              if (is_numeric($this->id) && strlen($this->id) >= 16) {
                        $this->id = bcsub($this->id, '76561197960265728');
                        //If subtraction goes negative, shift value up
                        if ($this->id < 0) {
                            $this->id += 4294967296;
                        }
                  $z = bcdiv($this->id, '2');
              } elseif (is_numeric($this->id)) {
                  $z = bcdiv($this->id, '2');
              } else {
                  return $this->id;
              }
              $y = bcmod($this->id, '2');
              return 'STEAM_0:' . $y . ':' . floor($z);
          }

          public function toUserID() {
              if (preg_match('/^STEAM_/', $this->id)) {
                  $split = explode(':', $this->id);
                  echo $split;
                  return $split[2] * 2 + $split[1];
              } elseif (preg_match('/^765/', $this->id) && strlen($this->id) > 15) {

                        $uid = bcsub($this->id, '76561197960265728');
                        if ($uid < 0) {

                            $uid += 4294967296;
                        }
                        return  $uid;

              } else {
                  return $this->id;

              }
          }

          //Function to sent request to SteamAPI
          private function getCURL($url) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
            curl_setopt($ch, CURLOPT_TIMEOUT, 2);
            $request = curl_exec($ch);
            curl_close($ch);
            $json = json_decode($request, true);
            return $json;
          }

          //Remove end slash if present as well as trailing whitespace
          private function cleanOutput($input) {
            $specialInput = htmlspecialchars(str_replace('/', '', $input));
            $cleanInput = preg_replace('/\s/', '', $specialInput);
            return $cleanInput;
          }

        }

此致:)

执行此操作的正确方法是 return 函数中的对象并根据需要使用它。请注意,我使用的是 PHP shorthand echo (<?=)。一个更好的方法是让另一个函数用预期的字符串格式化你的变量,这样你就不需要在 HTML.

function urlCheck() {
    $input = $_POST["profile_url"];
    if(substr_count($input, ' ') === strlen($input)) {
      echo "Enter URL";

      return false;
    } else {
      return $input;
    }
}

function display() {
    $input = urlCheck();
    if($input) {
      require_once 'steamid.class.php';
      $api_key = "XXXXXXXXXXXX";
      $id = new SteamID($input,$api_key);
        if ($id->resolveVanity()) {
            return $id;
        } else {
            echo "Profile wasn't found!";

            return false;
        }
    }
}

if(isset($_POST['submit']))
{
  $id = display();
}
...
<h1><?= (isset($id)) ? "[U:1:".$id->toUserID()."], " : "" ?></h1>
...
<img onerror="this.style.display='none'" src="<?= (isset($id)) ? {$id->toAvatar()} : "" ?>">