如何在全局范围PHP中访问局部变量?

时间:2016-11-23 21:43:51

标签: php variables scope

我一直在尝试访问全局范围内的局部变量。我尝试过global $var方法,但这似乎不起作用。 我尝试访问的变量是$word1Txt变量。 这是我的代码:

HTML:

<form class="form-inline" method="post">
                        <input class="form-control" type="text" placeholder="First Word" name="word1" autofocus>
                        <input class="form-control" type="text" placeholder="Second Word" name="word2" autofocus>
                        <input class="btn btn-primary form-submit" type="submit" value="Compare">
                    </form>

PHP:

<?php
            require('./wordnik/Swagger.php');
            $APIKey = '342eac9900e703079b0050d5f7008eab962195189e75bfbcb';
            $client = new APIClient($APIKey, 'http://api.wordnik.com/v4');

            if (!empty($_POST['word1'])) {
                $word1 = $_POST['word1'];
                $wordApi = new WordApi($client);
                $word1 = $wordApi->getDefinitions($word1, null, null, 1);
                global $word1Txt; 
                global $word10;
                $word1Txt = $_POST['word1'];
                $word10 = $word1[0]->text;
            }
            if (!empty($_POST['word2'])) {
                $word2 = $_POST['word2'];
                $wordApi = new WordApi($client);
                $word2 = $wordApi->getDefinitions($word2, null, null, 1);
                global $word2Txt; 
                global $word20;
                $word2Txt = $_POST['word2'];
                $word20 = $word2[0]->text;
            }
            print $word1Txt;
        ?>

JS:

$(document).ready(function() {
                var word1Txt = <?php echo $word1Txt; ?>;
                var word2Txt = <?php echo $word2Txt; ?>;  

                $('div.word1').prepend("<h3 class='header'>hi" + word1Txt + "</h3>");
                $('div.word2').prepend("<h3 class='header'>hi" + word2Txt + "</h3>");
            });

编辑: 我尝试添加var_dump($ _ POST);在if语句面前。我得到以下输出。

array(2) { ["word1"]=> string(2) "hi" ["word2"]=> string(2) "no" } 

Fatal error: Uncaught Exception: Unauthorized API request to
http://api.wordnik.com/v4/word.json/hi/definitions?limit=1:
unauthorized in
C:\xampp\htdocs\DictionaryCompare\wordnik\Swagger.php:111 Stack trace:
#0 C:\xampp\htdocs\DictionaryCompare\wordnik\WordApi.php(176): APIClient->callAPI('/word.json/hi/d...', 'GET', Array, NULL, Array) #1
C:\xampp\htdocs\DictionaryCompare\index.php(40):
WordApi->getDefinitions('hi', NULL, NULL, 1) #2 {main} thrown in
C:\xampp\htdocs\DictionaryCompare\wordnik\Swagger.php on line 111

2 个答案:

答案 0 :(得分:1)

我不知道你的$_POST中是否有正确的值,但这与范围有关,因为你看到它有效!

$_POST['word1'] = 'word1';
$_POST['word2'] = 'word2';
var_dump($_POST); // here you make sure you have posted right values 

// array(2) { ["word1"]=> string(5) "word1" ["word2"]=> string(5) "word2" } <---- this should have values

            if (!empty($_POST['word1'])) {
                $word1 = $_POST['word1'];

                global $word1Txt; 
                global $word10;
                $word1Txt = $_POST['word1'];
                $word10 = $word1[0]->text;
            }
            if (!empty($_POST['word2'])) {
                $word2 = $_POST['word2'];
                global $word2Txt; 
                global $word20;
                $word2Txt = $_POST['word2'];
                $word20 = $word2[0]->text;
            }
            print $word1Txt;
        ?>

因此,请在IF语句之前使用$_POST确保var_dump($_POST)中的值正确。

更新:我们在这里,您错过了action属性:)!

 <form class="form-inline" method="post" action="result.php">
                            <input class="form-control" type="text" placeholder="First Word" name="word1" autofocus>
                            <input class="form-control" type="text" placeholder="Second Word" name="word2" autofocus>
                            <input class="btn btn-primary form-submit" type="submit" value="Compare">
                        </form>

答案 1 :(得分:0)

有一个名为$GLOBALS的全局变量(原谅双关语)。见官方PHP manual

// in the middle of some context
$GLOBALS['foo'] = 'some text';
// in another context and scope, in the same run
echo $GLOBALS['foo'];
// echoes `some text`