【问题标题】:PHP Variable scope and globalsPHP 变量范围和全局变量
【发布时间】:2013-05-26 02:46:02
【问题描述】:

我有两个 php 文件。第一个文件将包括第二个文件。 这一切都有效。但是,在第二个文件中我有一个数组:

//set required items
$reqSettings = array(
    "apiUser" => true,
    "apiPass" => true,
    "apiKey" => true,
);

在第一个文件中调用的函数中,我想遍历这个数组,但是函数无法识别它:

function apiSettingsOk($arr) {
    global $reqSettings;

    $length = count($reqSettings);

    echo $length; //returns 0 and not 3
}

如您所见,我尝试使用“全局”,但这也不起作用。 你能帮我解决这个问题吗?

为了完整起见,这里是两个文件;)

文件 1:

$apiArr = array();

if (isset($_POST['api-submit'])) {

    $gateWay =  $_POST['of-sms-gateway'];
    $apiArr['apiUser'] = $_POST['api-user'];
    $apiArr['apiPass'] = $_POST['api-passwd'];
    $apiArr['apiKey'] = $_POST['api-key'];

    //including the gateway file
    include_once('of_sms_gateway_' . $gateWay . '.php');

    if (apiSettingsOk() === true) {
        echo "CORRECT";
    }

}

?>

of_sms_gateway_test.php:

<?php

//set required items
$reqSettings = array(
    "apiUser" => true,
    "apiPass" => true,
    "apiKey" => true,
);

function apiSettingsOk($arr) {
    global $reqSettings;
    $returnVar = true;

    $length = count($reqSettings);

    echo $length;

    return $returnVar;

}
?>

【问题讨论】:

  • apiSettingsOk($arr) 需要参数$arr,那在哪里??如果函数没有参数,那么看:codepad.org/IlC8qtdB

标签: php variables scope global


【解决方案1】:

请将“file1.php”包含到“file2.php”中,这样​​就可以了。

例子:

file1.php

<?php

$array = array(
    "name" => "test"
);

?>

file2.php

<?php

 include_once("file1.php");

 function test()
 {
     global $array;
     echo "<pre>";
     print_r($array);
 }

 test();
?>

这里,你可以看到,它会在file2.php中打印$array。在 file1.php 中声明。

希望它会有所帮助。

【讨论】:

    【解决方案2】:

    你在你的函数中加入了一个你没有提供的参数 $arr。像这样:

    if (apiSettingsOk($reqSettings) === true) {
        echo "CORRECT";
    }
    

    还有功能

    function apiSettingsOk($arr) {
    echo count($arr); //returns 0 and not 3
    }
    

    【讨论】:

      【解决方案3】:

      非常感谢您的帮助。

      使用它我还发现它有助于在第一个文件和第二个文件的函数中将 $reqSettings 声明为全局以执行相同的操作

      file1.php

      <?php
          global $reqSettings;
          $apiArr = array();
      
          if (isset($_POST['api-submit'])) {
      
              $gateWay =  $_POST['of-sms-gateway'];
              $apiArr['apiUser'] = $_POST['api-user'];
              $apiArr['apiPass'] = $_POST['api-passwd'];
              $apiArr['apiKey'] = $_POST['api-key'];
      
              include_once('of_sms_gateway_' . $gateWay . '.php');
      
              if (apiSettingsOk($apiArr) === true) {
      
                  echo "OK";
      
              } else {
                  echo "ERROR";
              }
      
          }
      
      ?>
      

      file2.php

      <?php
      
          $reqSettings = array(
              "apiUser" => true,
              "apiPass" => true,
              "apiKey" => true,
          );
      
          function apiSettingsOk($arr) {
              global $reqSettings;
              $returnVar = true;
      
              $length = count($reqSettings);
              echo $lenght; //now shows 3
      
              return $returnVal;
          }
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-07
        • 1970-01-01
        • 1970-01-01
        • 2012-01-19
        • 2017-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多