为什么外部变量超出了PHP的作用范围?

时间:2017-03-06 19:09:54

标签: php

我的PHP函数存在一个小问题,无法访问在其代码块之外定义的变量,我无法看到问题所在。重新定义功能块内的变量工作正常,但似乎是一个多余的步骤:

<?php
    //constants for database connection
    define(DB_RESOURCE, "192.168.99.67");
    define(DB_USERNAME, "myUsername");
    define(DB_PASSWORD, "p@ssword");
    define(DB_NAME, "myDatabase");
    //variable defined here so why can't function see it?
    $dbConnect = mysqli_connect(DB_RESOURCE, DB_USERNAME, DB_PASSWORD, DB_NAME);


    function readData($table, $column) {
        $sql = "SELECT {$column} FROM {table}";
        //redefining this solves the problem but why is this needed?
        // $dbConnect = mysqli_connect(DB_URL, DB_USER, DB_PASS, DB_NAME);
        //the $dbConnect variable is not defined and error is flagged in PHPStorm in the line below
        $data =  mysqli_query($dbConnect, $sql);
    }
?>

为什么会发生这种情况?

非常感谢你的帮助,

千瓦

5 个答案:

答案 0 :(得分:2)

只需将数据库连接传递给函数:

<?php
    //constants for database connection
    define(DB_RESOURCE, "192.168.99.67");
    define(DB_USERNAME, "myUsername");
    define(DB_PASSWORD, "p@ssword");
    define(DB_NAME, "myDatabase");

    $dbConnect = mysqli_connect(DB_RESOURCE, DB_USERNAME, DB_PASSWORD, DB_NAME);


    function readData($table, $column, $dbConnect) {
        $sql = "SELECT {$column} FROM {$table}";
        $data =  mysqli_query($dbConnect, $sql);
        // additional code here...
    }

    readData('table_name', 'column_name', $dbConnect);

?>

答案 1 :(得分:1)

你必须写

global $dbConnect;

在你的函数中。函数具有自己的范围,这意味着它们只能看到在其中定义或使用全局化全局化的Varibales。

答案 2 :(得分:1)

如果你想在任何函数中使用任何变量而不在参数中传递它,在php中

。你需要将它定义为全局。在使用之前,您需要global $dbConnect

<?php
    //constants for database connection
    define(DB_RESOURCE, "192.168.99.67");
    define(DB_USERNAME, "myUsername");
    define(DB_PASSWORD, "p@ssword");
    define(DB_NAME, "myDatabase");
    //variable defined here so why can't function see it?
    $dbConnect = mysqli_connect(DB_RESOURCE, DB_USERNAME, DB_PASSWORD, DB_NAME);


    function readData($table, $column) {
        $sql = "SELECT {$column} FROM {table}";
        //redefining this solves the problem but why is this needed?
        global  $dbConnect;
        // now you can use $dbConnect. In php you need to specify it as global once inside the block after it you can use 

        $data =  mysqli_query($dbConnect, $sql);
    }
?>

答案 3 :(得分:1)

这是PHP's scoping rules的一部分。 PHP不是JavaScript。在JavaScript中,这可以正常工作:

var myVar = 12;
alertVar();

function alertVar() {
    alert(myVar);
}

此功能将运行,并且将立即警告值12。 PHP不像这样。 PHP中的上述函数如下所示:

$myVar = 12;
echoVar();

function echoVar() use($myVar) {
    echo $myVar;
}

您也可以只引用全局范围或use construct超级全局,或者只是将变量作为参数传递,而不是使用$GLOBALS

function echoVar() {
    global $myVar;
    echo $myVar;
}

// or

function echoVar() {
    echo $GLOBALS['myVar'];
}

// or

echoVar($myVar);
function echoVar($var) {
    echo $var;
}

手册中的相关引用:

  

[...]在用户定义的函数中引入了本地函数范围。函数内使用的任何变量默认限制为本地函数范围。 [...]在PHP中,如果要在函数中使用全局变量,则必须在函数内声明全局变量。

答案 4 :(得分:1)

你正在尝试的是绝对正确的。只需将所有代码包含在类中。

示例:

class YourClassName{

  //constants for database connection
  define(DB_RESOURCE, "192.168.99.67");
  define(DB_USERNAME, "myUsername");
  define(DB_PASSWORD, "p@ssword");
  define(DB_NAME, "myDatabase");

  //variable defined here so why can't function see it?
  $dbConnect = mysqli_connect(DB_RESOURCE, DB_USERNAME, DB_PASSWORD, DB_NAME);


  function readData($table, $column) {
    $sql = "SELECT {$column} FROM {table}";
    //redefining this solves the problem but why is this needed?
    global  $dbConnect;
    // now you can use $dbConnect. In php you need to specify it as global once inside the block after it you can use 

    $data =  mysqli_query($this->dbConnect, $sql); //CHANGE THIS LINE AS SHOWN HERE.
  }
}