0

I have two php files. the first file will include the second one. This all works. However, int the second file I have an array:

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

In a function called in the first file I want to loop through this array, however it is not recognized by the function:

function apiSettingsOk($arr) {
    global $reqSettings;

    $length = count($reqSettings);

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

As you can see I tried using 'global' but this doesn't work either. Can you help me out fixing this issue?

Here are the two files just for completeness sake ;)

file 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;

}
?>
1
  • apiSettingsOk($arr) need parameter $arr, where is that ?? if the function without parrameter, then look: codepad.org/IlC8qtdB Commented May 30, 2013 at 13:36

3 Answers 3

2

Please include "file1.php" to "file2.php", then it will work.

Example :

file1.php

<?php

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

?>

file2.php

<?php

 include_once("file1.php");

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

 test();
?>

Here, you can see, it will print $array in file2.php. which is declared in file1.php.

Hope it will help.

Sign up to request clarification or add additional context in comments.

Comments

1

You have put an argument $arr to your function which you do not supply. Have it like so:

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

And the function

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

Comments

0

Great thanks for the help.

Using that I also found out it helps to declare the $reqSettings as global in the first file and in the function in the second file to do the same so

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;
    }

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.