我有两个 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;
}
?>