【发布时间】: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